org.apache.shiro.codec
Class CodecSupport

java.lang.Object
  extended by org.apache.shiro.codec.CodecSupport
Direct Known Subclasses:
AbstractHash, SimpleCredentialsMatcher

public abstract class CodecSupport
extends Object

Base abstract class that provides useful encoding and decoding operations, especially for character data.

Since:
0.9

Field Summary
static String PREFERRED_ENCODING
          Shiro's default preferred character encoding, equal to UTF-8.
 
Constructor Summary
CodecSupport()
           
 
Method Summary
protected  boolean isByteSource(Object o)
          Returns true if the specified object can be easily converted to bytes by instances of this class, false otherwise.
protected  byte[] objectToBytes(Object o)
          Default implementation throws a CodecException immediately since it can't infer how to convert the Object to a byte array.
protected  String objectToString(Object o)
          Default implementation merely returns objectArgument.toString().
static byte[] toBytes(char[] chars)
          Converts the specified character array to a byte array using the Shiro's preferred encoding (UTF-8).
static byte[] toBytes(char[] chars, String encoding)
          Converts the specified character array into a byte array using the specified character encoding.
protected  byte[] toBytes(File file)
           
protected  byte[] toBytes(InputStream in)
          Converts the specified InputStream into a byte array.
protected  byte[] toBytes(Object o)
          Converts the specified Object into a byte array.
static byte[] toBytes(String source)
          Converts the specified source argument to a byte array with Shiro's PREFERRED_ENCODING.
static byte[] toBytes(String source, String encoding)
          Converts the specified source to a byte array via the specified encoding, throwing a CodecException if the encoding fails.
static char[] toChars(byte[] bytes)
          Returns the specified byte array as a character array using the PREFERRED_ENCODING.
static char[] toChars(byte[] bytes, String encoding)
          Converts the specified byte array to a character array using the specified character encoding.
static String toString(byte[] bytes)
          Converts the specified byte array to a String using the PREFERRED_ENCODING.
static String toString(byte[] bytes, String encoding)
          Converts the specified byte array to a String using the specified character encoding.
protected  String toString(Object o)
          Converts the specified Object into a String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PREFERRED_ENCODING

public static final String PREFERRED_ENCODING
Shiro's default preferred character encoding, equal to UTF-8.

See Also:
Constant Field Values
Constructor Detail

CodecSupport

public CodecSupport()
Method Detail

toBytes

public static byte[] toBytes(char[] chars)
Converts the specified character array to a byte array using the Shiro's preferred encoding (UTF-8).

This is a convenience method equivalent to calling the toBytes(String,String) method with a a wrapping String and PREFERRED_ENCODING, i.e.

toBytes( new String(chars), PREFERRED_ENCODING );

Parameters:
chars - the character array to be converted to a byte array.
Returns:
the byte array of the UTF-8 encoded character array.

toBytes

public static byte[] toBytes(char[] chars,
                             String encoding)
                      throws CodecException
Converts the specified character array into a byte array using the specified character encoding.

This is a convenience method equivalent to calling the toBytes(String,String) method with a a wrapping String and the specified encoding, i.e.

toBytes( new String(chars), encoding );

Parameters:
chars - the character array to be converted to a byte array
encoding - the character encoding to use to when converting to bytes.
Returns:
the bytes of the specified character array under the specified encoding.
Throws:
CodecException - if the JVM does not support the specified encoding.

toBytes

public static byte[] toBytes(String source)
Converts the specified source argument to a byte array with Shiro's PREFERRED_ENCODING.

Parameters:
source - the string to convert to a byte array.
Returns:
the bytes representing the specified string under the PREFERRED_ENCODING.
See Also:
toBytes(String, String)

toBytes

public static byte[] toBytes(String source,
                             String encoding)
                      throws CodecException
Converts the specified source to a byte array via the specified encoding, throwing a CodecException if the encoding fails.

Parameters:
source - the source string to convert to a byte array.
encoding - the encoding to use to use.
Returns:
the byte array of the specified source with the given encoding.
Throws:
CodecException - if the JVM does not support the specified encoding.

toString

public static String toString(byte[] bytes)
Converts the specified byte array to a String using the PREFERRED_ENCODING.

Parameters:
bytes - the byte array to turn into a String.
Returns:
the specified byte array as an encoded String (PREFERRED_ENCODING).
See Also:
toString(byte[], String)

toString

public static String toString(byte[] bytes,
                              String encoding)
                       throws CodecException
Converts the specified byte array to a String using the specified character encoding. This implementation does the same thing as new String(byte[], encoding), but will wrap any UnsupportedEncodingException with a nicer runtime CodecException, allowing you to decide whether or not you want to catch the exception or let it propagate.

Parameters:
bytes - the byte array to convert to a String
encoding - the character encoding used to encode the String.
Returns:
the specified byte array as an encoded String
Throws:
CodecException - if the JVM does not support the specified encoding.

toChars

public static char[] toChars(byte[] bytes)
Returns the specified byte array as a character array using the PREFERRED_ENCODING.

Parameters:
bytes - the byte array to convert to a char array
Returns:
the specified byte array encoded as a character array (PREFERRED_ENCODING).
See Also:
toChars(byte[], String)

toChars

public static char[] toChars(byte[] bytes,
                             String encoding)
                      throws CodecException
Converts the specified byte array to a character array using the specified character encoding.

Effectively calls toString(bytes,encoding).toCharArray();

Parameters:
bytes - the byte array to convert to a String
encoding - the character encoding used to encode the bytes.
Returns:
the specified byte array as an encoded char array
Throws:
CodecException - if the JVM does not support the specified encoding.

isByteSource

protected boolean isByteSource(Object o)
Returns true if the specified object can be easily converted to bytes by instances of this class, false otherwise.

The default implementation returns true IFF the specified object is an instance of one of the following types:

Parameters:
o - the object to test to see if it can be easily converted to a byte array
Returns:
true if the specified object can be easily converted to bytes by instances of this class, false otherwise.
Since:
1.0

toBytes

protected byte[] toBytes(Object o)
Converts the specified Object into a byte array.

If the argument is a byte[], char[], ByteSource, String, File, or InputStream, it will be converted automatically and returned.}

If the argument is anything other than these types, it is passed to the objectToBytes method which must be overridden by subclasses.

Parameters:
o - the Object to convert into a byte array
Returns:
a byte array representation of the Object argument.

toString

protected String toString(Object o)
Converts the specified Object into a String.

If the argument is a byte[] or char[] it will be converted to a String using the PREFERRED_ENCODING. If a String, it will be returned as is.

If the argument is anything other than these three types, it is passed to the objectToString method.

Parameters:
o - the Object to convert into a byte array
Returns:
a byte array representation of the Object argument.

toBytes

protected byte[] toBytes(File file)

toBytes

protected byte[] toBytes(InputStream in)
Converts the specified InputStream into a byte array.

Parameters:
in - the InputStream to convert to a byte array
Returns:
the bytes of the input stream
Throws:
IllegalArgumentException - if the InputStream argument is null.
CodecException - if there is any problem reading from the InputStream.
Since:
1.0

objectToBytes

protected byte[] objectToBytes(Object o)
Default implementation throws a CodecException immediately since it can't infer how to convert the Object to a byte array. This method must be overridden by subclasses if anything other than the three default types (listed in the toBytes(Object) JavaDoc) are to be converted to a byte array.

Parameters:
o - the Object to convert to a byte array.
Returns:
a byte array representation of the Object argument.

objectToString

protected String objectToString(Object o)
Default implementation merely returns objectArgument.toString(). Subclasses can override this method for different mechanisms of converting an object to a String.

Parameters:
o - the Object to convert to a byte array.
Returns:
a String representation of the Object argument.


Copyright © 2004-2012 The Apache Software Foundation. All Rights Reserved.