blob: e0d55b91fad7c18ca47fcccd22e2a19b661fad62 [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>SimpleByteSource.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 :: All (aggregate jar)</a> &gt; <a href="../index.html" class="el_bundle">shiro-lang</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.util</a> &gt; <span class="el_source">SimpleByteSource.java</span></div><h1>SimpleByteSource.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 org.apache.shiro.codec.Base64;
import org.apache.shiro.codec.CodecSupport;
import org.apache.shiro.codec.Hex;
import java.io.File;
import java.io.InputStream;
import java.util.Arrays;
/**
* Very simple {@link ByteSource ByteSource} implementation that maintains an internal {@code byte[]} array and uses the
* {@link Hex Hex} and {@link Base64 Base64} codec classes to support the
* {@link #toHex() toHex()} and {@link #toBase64() toBase64()} implementations.
* &lt;p/&gt;
* The constructors on this class accept the following implicit byte-backed data types and will convert them to
* a byte-array automatically:
* &lt;ul&gt;
* &lt;li&gt;byte[]&lt;/li&gt;
* &lt;li&gt;char[]&lt;/li&gt;
* &lt;li&gt;String&lt;/li&gt;
* &lt;li&gt;{@link ByteSource ByteSource}&lt;/li&gt;
* &lt;li&gt;{@link File File}&lt;/li&gt;
* &lt;li&gt;{@link InputStream InputStream}&lt;/li&gt;
* &lt;/ul&gt;
*
* @since 1.0
*/
public class SimpleByteSource implements ByteSource {
private final byte[] bytes;
private String cachedHex;
private String cachedBase64;
<span class="fc" id="L53"> public SimpleByteSource(byte[] bytes) {</span>
<span class="fc" id="L54"> this.bytes = bytes;</span>
<span class="fc" id="L55"> }</span>
/**
* Creates an instance by converting the characters to a byte array (assumes UTF-8 encoding).
*
* @param chars the source characters to use to create the underlying byte array.
* @since 1.1
*/
<span class="nc" id="L63"> public SimpleByteSource(char[] chars) {</span>
<span class="nc" id="L64"> this.bytes = CodecSupport.toBytes(chars);</span>
<span class="nc" id="L65"> }</span>
/**
* Creates an instance by converting the String to a byte array (assumes UTF-8 encoding).
*
* @param string the source string to convert to a byte array (assumes UTF-8 encoding).
* @since 1.1
*/
<span class="fc" id="L73"> public SimpleByteSource(String string) {</span>
<span class="fc" id="L74"> this.bytes = CodecSupport.toBytes(string);</span>
<span class="fc" id="L75"> }</span>
/**
* Creates an instance using the sources bytes directly - it does not create a copy of the
* argument's byte array.
*
* @param source the source to use to populate the underlying byte array.
* @since 1.1
*/
<span class="nc" id="L84"> public SimpleByteSource(ByteSource source) {</span>
<span class="nc" id="L85"> this.bytes = source.getBytes();</span>
<span class="nc" id="L86"> }</span>
/**
* Creates an instance by converting the file to a byte array.
*
* @param file the file from which to acquire bytes.
* @since 1.1
*/
<span class="nc" id="L94"> public SimpleByteSource(File file) {</span>
<span class="nc" id="L95"> this.bytes = new BytesHelper().getBytes(file);</span>
<span class="nc" id="L96"> }</span>
/**
* Creates an instance by converting the stream to a byte array.
*
* @param stream the stream from which to acquire bytes.
* @since 1.1
*/
<span class="nc" id="L104"> public SimpleByteSource(InputStream stream) {</span>
<span class="nc" id="L105"> this.bytes = new BytesHelper().getBytes(stream);</span>
<span class="nc" id="L106"> }</span>
/**
* Returns {@code true} if the specified object is a recognized data type that can be easily converted to
* bytes by instances of this class, {@code false} otherwise.
* &lt;p/&gt;
* This implementation returns {@code true} IFF the specified object is an instance of one of the following
* types:
* &lt;ul&gt;
* &lt;li&gt;{@code byte[]}&lt;/li&gt;
* &lt;li&gt;{@code char[]}&lt;/li&gt;
* &lt;li&gt;{@link ByteSource}&lt;/li&gt;
* &lt;li&gt;{@link String}&lt;/li&gt;
* &lt;li&gt;{@link File}&lt;/li&gt;
* &lt;/li&gt;{@link InputStream}&lt;/li&gt;
* &lt;/ul&gt;
*
* @param o the object to test to see if it can be easily converted to bytes by instances of this class.
* @return {@code true} if the specified object can be easily converted to bytes by instances of this class,
* {@code false} otherwise.
* @since 1.2
*/
public static boolean isCompatible(Object o) {
<span class="pc bpc" id="L129" title="8 of 12 branches missed."> return o instanceof byte[] || o instanceof char[] || o instanceof String ||</span>
o instanceof ByteSource || o instanceof File || o instanceof InputStream;
}
public byte[] getBytes() {
<span class="fc" id="L134"> return this.bytes;</span>
}
public boolean isEmpty() {
<span class="fc bfc" id="L138" title="All 4 branches covered."> return this.bytes == null || this.bytes.length == 0;</span>
}
public String toHex() {
<span class="nc bnc" id="L142" title="All 2 branches missed."> if ( this.cachedHex == null ) {</span>
<span class="nc" id="L143"> this.cachedHex = Hex.encodeToString(getBytes());</span>
}
<span class="nc" id="L145"> return this.cachedHex;</span>
}
public String toBase64() {
<span class="fc bfc" id="L149" title="All 2 branches covered."> if ( this.cachedBase64 == null ) {</span>
<span class="fc" id="L150"> this.cachedBase64 = Base64.encodeToString(getBytes());</span>
}
<span class="fc" id="L152"> return this.cachedBase64;</span>
}
public String toString() {
<span class="nc" id="L156"> return toBase64();</span>
}
public int hashCode() {
<span class="nc bnc" id="L160" title="All 4 branches missed."> if (this.bytes == null || this.bytes.length == 0) {</span>
<span class="nc" id="L161"> return 0;</span>
}
<span class="nc" id="L163"> return Arrays.hashCode(this.bytes);</span>
}
public boolean equals(Object o) {
<span class="fc bfc" id="L167" title="All 2 branches covered."> if (o == this) {</span>
<span class="fc" id="L168"> return true;</span>
}
<span class="pc bpc" id="L170" title="1 of 2 branches missed."> if (o instanceof ByteSource) {</span>
<span class="fc" id="L171"> ByteSource bs = (ByteSource) o;</span>
<span class="fc" id="L172"> return Arrays.equals(getBytes(), bs.getBytes());</span>
}
<span class="nc" id="L174"> return false;</span>
}
//will probably be removed in Shiro 2.0. See SHIRO-203:
//https://issues.apache.org/jira/browse/SHIRO-203
<span class="nc" id="L179"> private static final class BytesHelper extends CodecSupport {</span>
public byte[] getBytes(File file) {
<span class="nc" id="L181"> return toBytes(file);</span>
}
public byte[] getBytes(InputStream stream) {
<span class="nc" id="L185"> return toBytes(stream);</span>
}
}
}
</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>