public abstract class CodecSupport extends Object
Modifier and Type | Field and Description |
---|---|
static String |
PREFERRED_ENCODING
Shiro's default preferred character encoding, equal to
UTF-8 . |
Constructor and Description |
---|
CodecSupport() |
Modifier and Type | Method and Description |
---|---|
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.
|
public static final String PREFERRED_ENCODING
UTF-8
.public CodecSupport()
public static byte[] toBytes(char[] chars)
toBytes(String,String)
method with a
a wrapping String and PREFERRED_ENCODING
, i.e.
toBytes( new String(chars), PREFERRED_ENCODING
);
chars
- the character array to be converted to a byte array.public static byte[] toBytes(char[] chars, String encoding) throws CodecException
toBytes(String,String)
method with a
a wrapping String and the specified encoding, i.e.
toBytes( new String(chars), encoding );
chars
- the character array to be converted to a byte arrayencoding
- the character encoding to use to when converting to bytes.CodecException
- if the JVM does not support the specified encoding.public static byte[] toBytes(String source)
PREFERRED_ENCODING
.source
- the string to convert to a byte array.PREFERRED_ENCODING
.toBytes(String, String)
public static byte[] toBytes(String source, String encoding) throws CodecException
CodecException
if the encoding fails.source
- the source string to convert to a byte array.encoding
- the encoding to use to use.CodecException
- if the JVM does not support the specified encoding.public static String toString(byte[] bytes)
PREFERRED_ENCODING
.bytes
- the byte array to turn into a String.PREFERRED_ENCODING
).toString(byte[], String)
public static String toString(byte[] bytes, String encoding) throws CodecException
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.bytes
- the byte array to convert to a Stringencoding
- the character encoding used to encode the String.CodecException
- if the JVM does not support the specified encoding.public static char[] toChars(byte[] bytes)
PREFERRED_ENCODING
.bytes
- the byte array to convert to a char arrayPREFERRED_ENCODING
).toChars(byte[], String)
public static char[] toChars(byte[] bytes, String encoding) throws CodecException
toString(bytes,encoding)
.toCharArray()
;
bytes
- the byte array to convert to a Stringencoding
- the character encoding used to encode the bytes.CodecException
- if the JVM does not support the specified encoding.protected boolean isByteSource(Object o)
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:
byte[]
char[]
ByteSource
String
File
InputStream
o
- the object to test to see if it can be easily converted to a byte arraytrue
if the specified object can be easily converted to bytes by instances of this class,
false
otherwise.protected byte[] toBytes(Object o)
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.o
- the Object to convert into a byte arrayprotected String toString(Object o)
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.o
- the Object to convert into a byte arrayprotected byte[] toBytes(InputStream in)
InputStream
into a byte array.in
- the InputStream to convert to a byte arrayIllegalArgumentException
- if the InputStream
argument is null
.CodecException
- if there is any problem reading from the InputStream
.protected byte[] objectToBytes(Object o)
toBytes(Object)
JavaDoc) are to be converted to a byte array.o
- the Object to convert to a byte array.protected String objectToString(Object o)
objectArgument.toString()
. Subclasses can override this
method for different mechanisms of converting an object to a String.o
- the Object to convert to a byte array.Copyright © 2004-2016 The Apache Software Foundation. All Rights Reserved.