blob: 92210731d4e5fe498af3d849179fe4b91a98417b [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>ByteSource.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">Apache Shiro :: Jar Bundle</a> &gt; <a href="../index.html" class="el_bundle">shiro-core</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.util</a> &gt; <span class="el_source">ByteSource.java</span></div><h1>ByteSource.java</h1><pre class="source lang-java linenums">/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* &quot;License&quot;); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.shiro.util;
import java.io.File;
import java.io.InputStream;
/**
* A {@code ByteSource} wraps a byte array and provides additional encoding operations. Most users will find the
* {@link Util} inner class sufficient to construct ByteSource instances.
*
* @since 1.0
*/
public interface ByteSource {
/**
* Returns the wrapped byte array.
*
* @return the wrapped byte array.
*/
byte[] getBytes();
/**
* Returns the &lt;a href=&quot;http://en.wikipedia.org/wiki/Hexadecimal&quot;&gt;Hex&lt;/a&gt;-formatted String representation of the
* underlying wrapped byte array.
*
* @return the &lt;a href=&quot;http://en.wikipedia.org/wiki/Hexadecimal&quot;&gt;Hex&lt;/a&gt;-formatted String representation of the
* underlying wrapped byte array.
*/
String toHex();
/**
* Returns the &lt;a href=&quot;http://en.wikipedia.org/wiki/Base64&quot;&gt;Base 64&lt;/a&gt;-formatted String representation of the
* underlying wrapped byte array.
*
* @return the &lt;a href=&quot;http://en.wikipedia.org/wiki/Base64&quot;&gt;Base 64&lt;/a&gt;-formatted String representation of the
* underlying wrapped byte array.
*/
String toBase64();
/**
* Returns {@code true} if the underlying wrapped byte array is null or empty (zero length), {@code false}
* otherwise.
*
* @return {@code true} if the underlying wrapped byte array is null or empty (zero length), {@code false}
* otherwise.
* @since 1.2
*/
boolean isEmpty();
/**
* Utility class that can construct ByteSource instances. This is slightly nicer than needing to know the
* {@code ByteSource} implementation class to use.
*
* @since 1.2
*/
<span class="nc" id="L73"> public static final class Util {</span>
/**
* Returns a new {@code ByteSource} instance representing the specified byte array.
*
* @param bytes the bytes to represent as a {@code ByteSource} instance.
* @return a new {@code ByteSource} instance representing the specified byte array.
*/
public static ByteSource bytes(byte[] bytes) {
<span class="fc" id="L82"> return new SimpleByteSource(bytes);</span>
}
/**
* Returns a new {@code ByteSource} instance representing the specified character array's bytes. The byte
* array is obtained assuming {@code UTF-8} encoding.
*
* @param chars the character array to represent as a {@code ByteSource} instance.
* @return a new {@code ByteSource} instance representing the specified character array's bytes.
*/
public static ByteSource bytes(char[] chars) {
<span class="nc" id="L93"> return new SimpleByteSource(chars);</span>
}
/**
* Returns a new {@code ByteSource} instance representing the specified string's bytes. The byte
* array is obtained assuming {@code UTF-8} encoding.
*
* @param string the string to represent as a {@code ByteSource} instance.
* @return a new {@code ByteSource} instance representing the specified string's bytes.
*/
public static ByteSource bytes(String string) {
<span class="fc" id="L104"> return new SimpleByteSource(string);</span>
}
/**
* Returns a new {@code ByteSource} instance representing the specified ByteSource.
*
* @param source the ByteSource to represent as a new {@code ByteSource} instance.
* @return a new {@code ByteSource} instance representing the specified ByteSource.
*/
public static ByteSource bytes(ByteSource source) {
<span class="nc" id="L114"> return new SimpleByteSource(source);</span>
}
/**
* Returns a new {@code ByteSource} instance representing the specified File's bytes.
*
* @param file the file to represent as a {@code ByteSource} instance.
* @return a new {@code ByteSource} instance representing the specified File's bytes.
*/
public static ByteSource bytes(File file) {
<span class="nc" id="L124"> return new SimpleByteSource(file);</span>
}
/**
* Returns a new {@code ByteSource} instance representing the specified InputStream's bytes.
*
* @param stream the InputStream to represent as a {@code ByteSource} instance.
* @return a new {@code ByteSource} instance representing the specified InputStream's bytes.
*/
public static ByteSource bytes(InputStream stream) {
<span class="nc" id="L134"> return new SimpleByteSource(stream);</span>
}
/**
* Returns {@code true} if the specified object can be easily represented as a {@code ByteSource} using
* the {@link ByteSource.Util}'s default heuristics, {@code false} otherwise.
* &lt;p/&gt;
* This implementation merely returns {@link SimpleByteSource}.{@link SimpleByteSource#isCompatible(Object) isCompatible(source)}.
*
* @param source the object to test to see if it can be easily converted to ByteSource instances using default
* heuristics.
* @return {@code true} if the specified object can be easily represented as a {@code ByteSource} using
* the {@link ByteSource.Util}'s default heuristics, {@code false} otherwise.
*/
public static boolean isCompatible(Object source) {
<span class="fc" id="L149"> return SimpleByteSource.isCompatible(source);</span>
}
/**
* Returns a {@code ByteSource} instance representing the specified byte source argument. If the argument
* &lt;em&gt;cannot&lt;/em&gt; be easily converted to bytes (as is indicated by the {@link #isCompatible(Object)} JavaDoc),
* this method will throw an {@link IllegalArgumentException}.
*
* @param source the byte-backed instance that should be represented as a {@code ByteSource} instance.
* @return a {@code ByteSource} instance representing the specified byte source argument.
* @throws IllegalArgumentException if the argument &lt;em&gt;cannot&lt;/em&gt; be easily converted to bytes
* (as indicated by the {@link #isCompatible(Object)} JavaDoc)
*/
public static ByteSource bytes(Object source) throws IllegalArgumentException {
<span class="fc bfc" id="L163" title="All 2 branches covered."> if (source == null) {</span>
<span class="fc" id="L164"> return null;</span>
}
<span class="pc bpc" id="L166" title="1 of 2 branches missed."> if (!isCompatible(source)) {</span>
<span class="nc" id="L167"> String msg = &quot;Unable to heuristically acquire bytes for object of type [&quot; +</span>
<span class="nc" id="L168"> source.getClass().getName() + &quot;]. If this type is indeed a byte-backed data type, you might &quot; +</span>
&quot;want to write your own ByteSource implementation to extract its bytes explicitly.&quot;;
<span class="nc" id="L170"> throw new IllegalArgumentException(msg);</span>
}
<span class="fc bfc" id="L172" title="All 2 branches covered."> if (source instanceof byte[]) {</span>
<span class="fc" id="L173"> return bytes((byte[]) source);</span>
<span class="pc bpc" id="L174" title="1 of 2 branches missed."> } else if (source instanceof ByteSource) {</span>
<span class="nc" id="L175"> return (ByteSource) source;</span>
<span class="pc bpc" id="L176" title="1 of 2 branches missed."> } else if (source instanceof char[]) {</span>
<span class="nc" id="L177"> return bytes((char[]) source);</span>
<span class="pc bpc" id="L178" title="1 of 2 branches missed."> } else if (source instanceof String) {</span>
<span class="fc" id="L179"> return bytes((String) source);</span>
<span class="nc bnc" id="L180" title="All 2 branches missed."> } else if (source instanceof File) {</span>
<span class="nc" id="L181"> return bytes((File) source);</span>
<span class="nc bnc" id="L182" title="All 2 branches missed."> } else if (source instanceof InputStream) {</span>
<span class="nc" id="L183"> return bytes((InputStream) source);</span>
} else {
<span class="nc" id="L185"> throw new IllegalStateException(&quot;Encountered unexpected byte source. This is a bug - please notify &quot; +</span>
&quot;the Shiro developer list asap (the isCompatible implementation does not reflect this &quot; +
&quot;method's implementation).&quot;);
}
}
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.7.201606060606</span></div></body></html>