Class Overview
Provides support for converting byte sequences to Strings and back again.
The resulting Strings preserve the original byte sequences' sort order.
The Strings are constructed using a Base 8000h encoding of the original
binary data - each char of an encoded String represents a 15-bit chunk
from the byte sequence. Base 8000h was chosen because it allows for all
lower 15 bits of char to be used without restriction; the surrogate range
[U+D8000-U+DFFF] does not represent valid chars, and would require
complicated handling to avoid them and allow use of char's high bit.
Although unset bits are used as padding in the final char, the original
byte sequence could contain trailing bytes with no set bits (null bytes):
padding is indistinguishable from valid information. To overcome this
problem, a char is appended, indicating the number of encoded bytes in the
final content char.
This class's operations are defined over CharBuffers and ByteBuffers, to
allow for wrapped arrays to be reused, reducing memory allocation costs for
repeated operations. Note that this class calls array() and arrayOffset()
on the CharBuffers and ByteBuffers it uses, so only wrapped arrays may be
used. This class interprets the arrayOffset() and limit() values returned by
its input buffers as beginning and end+1 positions on the wrapped array,
respectively; similarly, on the output buffer, arrayOffset() is the first
position written to, and limit() is set to one past the final output array
position.
Summary
Public Methods |
static
void
|
decode(CharBuffer input, ByteBuffer output)
Decodes the input char sequence into the output byte sequence.
|
static
ByteBuffer
|
decode(CharBuffer input)
|
static
CharBuffer
|
encode(ByteBuffer input)
Encodes the input byte sequence.
|
static
void
|
encode(ByteBuffer input, CharBuffer output)
Encodes the input byte sequence into the output char sequence.
|
static
int
|
getDecodedLength(CharBuffer encoded)
Returns the number of bytes required to decode the given char sequence.
|
static
int
|
getEncodedLength(ByteBuffer original)
Returns the number of chars required to encode the given byte sequence.
|
[Expand]
Inherited Methods |
From class
java.lang.Object
Object
|
clone()
|
boolean
|
equals(Object arg0)
|
void
|
finalize()
|
final
Class<?>
|
getClass()
|
int
|
hashCode()
|
final
void
|
notify()
|
final
void
|
notifyAll()
|
String
|
toString()
|
final
void
|
wait()
|
final
void
|
wait(long arg0, int arg1)
|
final
void
|
wait(long arg0)
|
|
Public Methods
public
static
void
decode
(CharBuffer input, ByteBuffer output)
Decodes the input char sequence into the output byte sequence. Before
calling this method, ensure that the output ByteBuffer has sufficient
capacity by calling getDecodedLength(java.nio.CharBuffer)
.
Parameters
input
| The char sequence to decode |
output
| Where the byte sequence decoding result will go. The limit
is set to one past the position of the final char. |
public
static
ByteBuffer
decode
(CharBuffer input)
Parameters
input
| The char sequence to decode |
Returns
- A byte sequence containing the decoding result. The limit
is set to one past the position of the final char.
public
static
CharBuffer
encode
(ByteBuffer input)
Encodes the input byte sequence.
Parameters
input
| The byte sequence to encode |
Returns
- A char sequence containing the encoding result. The limit is set
to one past the position of the final char.
public
static
void
encode
(ByteBuffer input, CharBuffer output)
Encodes the input byte sequence into the output char sequence. Before
calling this method, ensure that the output CharBuffer has sufficient
capacity by calling getEncodedLength(java.nio.ByteBuffer)
.
Parameters
input
| The byte sequence to encode |
output
| Where the char sequence encoding result will go. The limit
is set to one past the position of the final char. |
public
static
int
getDecodedLength
(CharBuffer encoded)
Returns the number of bytes required to decode the given char sequence.
Parameters
encoded
| The char sequence to be encoded. Must be backed by an array. |
Returns
- The number of bytes required to decode the given char sequence
public
static
int
getEncodedLength
(ByteBuffer original)
Returns the number of chars required to encode the given byte sequence.
Parameters
original
| The byte sequence to be encoded. Must be backed by an array. |
Returns
- The number of chars required to encode the given byte sequence