public abstract class ByteBuffers extends Object
ByteBuffer class.| Constructor and Description |
|---|
ByteBuffers() |
| Modifier and Type | Method and Description |
|---|---|
static ByteBuffer |
asReadOnlyBuffer(ByteBuffer source)
Return a ByteBuffer that is a new read-only buffer that shares the
content of
source and has the same byte order, but maintains a
distinct position, mark and limit. |
static ByteBuffer |
clone(ByteBuffer buffer)
Return a clone of
buffer that has a copy of all its
content and the same position and limit. |
static void |
copyAndRewindSource(ByteBuffer source,
ByteBuffer destination)
Transfer the bytes from
source to destination and resets
source so that its position remains unchanged. |
static ByteBuffer |
decodeFromHex(String hex)
Decode the
hexadeciaml string and return the resulting binary
data. |
static String |
encodeAsHex(ByteBuffer bytes)
Encode the
bytes as a hexadecimal string. |
static String |
encodeAsHexString(ByteBuffer buffer)
Encode the remaining bytes in as
ByteBuffer as a hex string and
maintain the current position. |
static ByteBuffer |
expand(ByteBuffer destination,
ByteBuffer source)
Copy the remaining bytes in the
source buffer to the
destination, expanding if necessary in
order to accommodate the bytes from source. |
static ByteBuffer |
expandInt(ByteBuffer destination,
int value)
Put the
value into destination, expanding if necessary in
order to accommodate the new bytes. |
static ByteBuffer |
fromString(String string)
Return a byte buffer that has the UTF-8 encoding for
string. |
static ByteBuffer |
get(ByteBuffer buffer,
int length)
Return a ByteBuffer that has a copy of
length bytes from
buffer starting from the current position. |
static boolean |
getBoolean(ByteBuffer buffer)
Relative get method.
|
static <T extends Enum<?>> |
getEnum(ByteBuffer buffer,
Class<T> clazz)
Relative get method.
|
static ByteBuffer |
getRemaining(ByteBuffer buffer)
Return a ByteBuffer that has a copy of all the remaining bytes from
buffer starting from the current position. |
static String |
getString(ByteBuffer buffer)
Relative get method.
|
static String |
getString(ByteBuffer buffer,
Charset charset)
Relative get method.
|
static ByteBuffer |
nullByteBuffer()
Return a ByteBuffer that contains a single null byte.
|
static void |
putString(String source,
ByteBuffer destination)
Put the UTF-8 encoding for the
source string into the
destination byte buffer and increment the position by the length
of the strings byte sequence. |
static ByteBuffer |
rewind(ByteBuffer buffer)
|
static ByteBuffer |
slice(ByteBuffer buffer,
int length)
Return a new ByteBuffer whose content is a shared subsequence of the
content in
buffer starting at the current position to
current position + length (non-inclusive). |
static ByteBuffer |
slice(ByteBuffer buffer,
int position,
int length)
Return a new ByteBuffer whose content is a shared subsequence of the
content in
buffer starting at position to
position + length (non-inclusive). |
static byte[] |
toByteArray(ByteBuffer buffer)
Return a byte array with the content of
buffer. |
static CharBuffer |
toCharBuffer(ByteBuffer buffer)
Return a UTF-8
CharBuffer representation of the bytes in the
buffer. |
static CharBuffer |
toCharBuffer(ByteBuffer buffer,
Charset charset)
|
public static ByteBuffer asReadOnlyBuffer(ByteBuffer source)
source and has the same byte order, but maintains a
distinct position, mark and limit.source - public static ByteBuffer clone(ByteBuffer buffer)
buffer that has a copy of all its
content and the same position and limit. Unlike the
ByteBuffer.slice() method, the returned clone
does not share its content with buffer, so
subsequent operations to buffer or its clone will be
completely independent and won't affect the other.buffer - bufferpublic static void copyAndRewindSource(ByteBuffer source, ByteBuffer destination)
source to destination and resets
source so that its position remains unchanged. The position of
the destination is incremented by the number of bytes that are
transferred.source - destination - public static ByteBuffer decodeFromHex(String hex)
hexadeciaml string and return the resulting binary
data.hex - public static String encodeAsHex(ByteBuffer bytes)
bytes as a hexadecimal string.bytes - public static String encodeAsHexString(ByteBuffer buffer)
ByteBuffer as a hex string and
maintain the current position.buffer - public static ByteBuffer expand(ByteBuffer destination, ByteBuffer source)
source buffer to the
destination, expanding if necessary in
order to accommodate the bytes from source.
NOTE: This method may modify the limit for the
destination buffer.
destination - the buffer into which the source is copiedsource - the buffer that is copied into the destinationdestination with the
source bytes copiedpublic static ByteBuffer expandInt(ByteBuffer destination, int value)
value into destination, expanding if necessary in
order to accommodate the new bytes.
NOTE: This method may modify the limit for the
destination buffer.
destination - the buffer into which the source is copiedvalue - the value to add to the destinationdestination with the
value bytes copiedpublic static ByteBuffer fromString(String string)
string. This
method uses some optimization techniques and is the preferable way to
convert strings to byte buffers than doing so manually.string - string data.public static ByteBuffer get(ByteBuffer buffer, int length)
length bytes from
buffer starting from the current position. This method will
advance the position of the source buffer.buffer - length - length bytes from bufferpublic static boolean getBoolean(ByteBuffer buffer)
buffer as a boolean, and then increments the position.buffer - public static <T extends Enum<?>> T getEnum(ByteBuffer buffer, Class<T> clazz)
buffer and then increments the position by four.buffer - clazz - public static ByteBuffer getRemaining(ByteBuffer buffer)
buffer starting from the current position. This method will
advance the position of the source buffer.buffer - the source bufferbufferpublic static String getString(ByteBuffer buffer)
buffer.buffer - public static String getString(ByteBuffer buffer, Charset charset)
charset encoded string at
the current position in buffer.buffer - charset - public static ByteBuffer nullByteBuffer()
public static void putString(String source, ByteBuffer destination)
source string into the
destination byte buffer and increment the position by the length
of the strings byte sequence. This method uses some optimization
techniques and is the preferable way to add strings to byte buffers than
doing so manually.source - destination - public static ByteBuffer rewind(ByteBuffer buffer)
buffer - bufferpublic static ByteBuffer slice(ByteBuffer buffer, int length)
buffer starting at the current position to
current position + length (non-inclusive). Invoking this method
has the same affect as doing the following:
buffer.mark(); int oldLimit = buffer.limit(); buffer.limit(buffer.position() + length); ByteBuffer slice = buffer.slice(); buffer.reset(); buffer.limit(oldLimit);
buffer - length - ByteBuffer.slice()public static ByteBuffer slice(ByteBuffer buffer, int position, int length)
buffer starting at position to
position + length (non-inclusive). Invoking this method
has the same affect as doing the following:
buffer.mark(); int oldLimit = buffer.limit(); buffer.position(position); buffer.limit(position + length); ByteBuffer slice = buffer.slice(); buffer.reset(); buffer.limit(oldLimit);
buffer - position - length - ByteBuffer.slice()public static byte[] toByteArray(ByteBuffer buffer)
buffer. This method
returns the byte array that backs buffer if one exists, otherwise
it creates a new byte array with the content between the current position
of buffer and its limit.buffer - bufferpublic static CharBuffer toCharBuffer(ByteBuffer buffer)
CharBuffer representation of the bytes in the
buffer.buffer - public static CharBuffer toCharBuffer(ByteBuffer buffer, Charset charset)
buffer - charset -