Class CodecSupport
java.lang.Object
org.apache.shiro.lang.codec.CodecSupport
Base abstract class that provides useful encoding and decoding operations, especially for character data.
- Since:
- 0.9
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringShiro's default preferred character encoding, equal toUTF-8. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanReturnstrueif the specified object can be easily converted to bytes by instances of this class,falseotherwise.protected byte[]Default implementation throws a CodecException immediately since it can't infer how to convert the Object to a byte array.protected StringDefault implementation merely returnsobjectArgument.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[]Converts the specified character array into a byte array using the specified character encoding.protected byte[]protected byte[]toBytes(InputStream in) Converts the specifiedInputStreaminto a byte array.protected byte[]Converts the specified Object into a byte array.static byte[]Converts the specified source argument to a byte array with Shiro'sPREFERRED_ENCODING.static byte[]Converts the specified source to a byte array via the specified encoding, throwing aCodecExceptionif the encoding fails.static char[]toChars(byte[] bytes) Returns the specified byte array as a character array using thePREFERRED_ENCODING.static char[]Converts the specified byte array to a character array using the specified character encoding.static StringtoString(byte[] bytes) Converts the specified byte array to a String using thePREFERRED_ENCODING.static StringConverts the specified byte array to a String using the specified character encoding.protected StringConverts the specified Object into a String.
-
Field Details
-
PREFERRED_ENCODING
Shiro's default preferred character encoding, equal toUTF-8.- See Also:
-
-
Constructor Details
-
CodecSupport
public CodecSupport()
-
-
Method Details
-
toBytes
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 thetoBytes(String, String)method with a a wrapping String andPREFERRED_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
Converts the specified character array into a byte array using the specified character encoding. This is a convenience method equivalent to calling thetoBytes(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 arrayencoding- 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
Converts the specified source argument to a byte array with Shiro'sPREFERRED_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
Converts the specified source to a byte array via the specified encoding, throwing aCodecExceptionif 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
Converts the specified byte array to a String using thePREFERRED_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
Converts the specified byte array to a String using the specified character encoding. This implementation does the same thing asnew, but will wrap anyString(byte[], encoding)UnsupportedEncodingExceptionwith a nicer runtimeCodecException, 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 Stringencoding- 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
Returns the specified byte array as a character array using thePREFERRED_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
Converts the specified byte array to a character array using the specified character encoding. Effectively callstoString(bytes,encoding).toCharArray();- Parameters:
bytes- the byte array to convert to a Stringencoding- 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
Returnstrueif the specified object can be easily converted to bytes by instances of this class,falseotherwise. The default implementation returnstrueIFF the specified object is an instance of one of the following types:byte[]char[]ByteSourceStringFile
InputStream- Parameters:
o- the object to test to see if it can be easily converted to a byte array- Returns:
trueif the specified object can be easily converted to bytes by instances of this class,falseotherwise.- Since:
- 1.0
-
toBytes
Converts the specified Object into a byte array. If the argument is abyte[],char[],ByteSource,String,File, orInputStream, it will be converted automatically and returned.} If the argument is anything other than these types, it is passed to theobjectToBytesmethod which must be overridden by subclasses.- Parameters:
object- the Object to convert into a byte array- Returns:
- a byte array representation of the Object argument.
-
toString
Converts the specified Object into a String. If the argument is abyte[]orchar[]it will be converted to a String using thePREFERRED_ENCODING. If a String, it will be returned as is. If the argument is anything other than these three types, it is passed to theobjectToStringmethod.- Parameters:
o- the Object to convert into a byte array- Returns:
- a byte array representation of the Object argument.
-
toBytes
-
toBytes
Converts the specifiedInputStreaminto a byte array.- Parameters:
in- the InputStream to convert to a byte array- Returns:
- the bytes of the input stream
- Throws:
IllegalArgumentException- if theInputStreamargument isnull.CodecException- if there is any problem reading from theInputStream.- Since:
- 1.0
-
objectToBytes
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 thetoBytes(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
Default implementation merely returnsobjectArgument.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.
-