The standard JVM ByteBuffer can exist in two modes: In fill mode the valid
data is between 0 and pos; In flush mode the valid data is between the pos and the limit.
The various ByteBuffer methods assume a mode and some of them will switch or enforce a mode:
Allocate and clear set fill mode; flip and compact switch modes; read and write assume fill
and flush modes. This duality can result in confusing code such as:
buffer.clear();
channel.write(buffer);
Which looks as if it should write no data, but in fact writes the buffer worth of garbage.
The BufferUtil class provides a set of utilities that operate on the convention that ByteBuffers will always be left, passed in an API or returned from a method in the flush mode - ie with valid data between the pos and limit. This convention is adopted so as to avoid confusion as to what state a buffer is in and to avoid excessive copying of data that can result with the usage of compress.
Thus this class provides alternate implementations of allocate(int),
allocateDirect(int) and clear(ByteBuffer) that leave the buffer
in flush mode. Thus the following tests will pass:
ByteBuffer buffer = BufferUtil.allocate(1024);
assert(buffer.remaining()==0);
BufferUtil.clear(buffer);
assert(buffer.remaining()==0);
If the BufferUtil methods fill(ByteBuffer, byte[], int, int),
append(ByteBuffer, byte[], int, int) or put(ByteBuffer, ByteBuffer) are used,
then the caller does not need to explicitly switch the buffer to fill mode.
If the caller wishes to use other ByteBuffer bases libraries to fill a buffer,
then they can use explicit calls of #flipToFill(ByteBuffer) and #flipToFlush(ByteBuffer, int)
to change modes. Note because this convention attempts to avoid the copies of compact, the position
is not set to zero on each fill cycle and so its value must be remembered:
int pos = BufferUtil.flipToFill(buffer);
try
{
buffer.put(data);
}
finally
{
flipToFlush(buffer, pos);
}
The flipToFill method will effectively clear the buffer if it is empty and will compact the buffer if there is no space.
| Modifier and Type | Field and Description |
|---|---|
| private static final int[] | |
| private static final long[] | |
| pack-priv static final byte[] | |
| public static final ByteBuffer | |
| private static final int[] | |
| private static final Logger | |
| pack-priv static final byte | |
| pack-priv static final byte | |
| pack-priv static final int |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public static ByteBuffer | Returns: Buffercapacity of the allocated ByteBuffer capacity)Allocate ByteBuffer in flush mode. |
| public static ByteBuffer | Returns: the newly allocated ByteBuffercapacity of the allocated ByteBuffer capacity, boolean whether the ByteBuffer is direct direct)Allocates a ByteBuffer in flush mode. |
| public static ByteBuffer | Returns: Buffercapacity of the allocated ByteBuffer capacity)Allocate ByteBuffer in flush mode. |
| public static void | append(ByteBuffer
Buffer is flush mode to, byte[] bytes to append b, int offset into byte off, int length to append len)Append bytes to a buffer. |
| public static void | |
| public static void | append(ByteBuffer
Buffer is flush mode to, String String to append as UTF8 s)Append a string to a buffer. |
| public static void | |
| public static int | Returns: The position of the valid data before the flipped position.Buffer is flush mode to, ByteBuffer buffer to append b)Appends a buffer to a buffer |
| private static void | |
| private static void | |
| public static void | |
| public static void | |
| public static boolean | Returns: true if the compact made a full buffer have spacethe buffer to compact buffer)Compact the buffer |
| public static ByteBuffer | |
| public static ByteBuffer | |
| public static int | Returns: the number of bytes taken from the buffer.Buffer The buffer to fill to. The buffer will be flipped to fill mode and then flipped back to flush mode. to, byte[] bytes The bytes to fill b, int offset into bytes off, int length to fill len)Like append, but does not throw |
| public static int | Returns: The position of the valid data before the flipped position. This value should be passed to a subsequent call toflipToFlush(ByteBuffer, int)The buffer to flip buffer)Flip the buffer to fill mode. |
| public static void | flipToFlush(ByteBuffer
the buffer to be flipped buffer, int The position of valid data to flip to. This should
be the return value of the previous call to position)flipToFill(ByteBuffer)Flip the buffer to Flush mode. |
| public static boolean | Returns: true if the buffer is not null and not empty.the buffer to check buf)Check for a non null and non empty buffer. |
| private static void | |
| public static boolean | Returns: true if the buffer is null or empty.the buffer to check buf)Check for an empty or null buffer. |
| public static boolean | Returns: true if the buffer is null or empty.the buffer to check buf)Check for an empty or null buffers. |
| public static boolean | Returns: true if the buffer is not null and the limit equals the capacity.the buffer to check buf)Check for a non null and full buffer. |
| public static boolean | |
| public static boolean | |
| public static int | Returns: 0 if the buffer is null, else the bytes remaining in the buffer.The buffer to get the remaining from, in flush mode. buffer)Get remaining from null checked buffer |
| public static int | Returns: number of bytes movedBuffer to take bytes from in flush mode from, ByteBuffer Buffer to put bytes to in fill mode. to)Put data from one buffer into another, avoiding over/under flows |
| public static void | |
| public static void | |
| public static void | |
| public static void | |
| public static void | putIntLittleEndian(ByteBuffer
The buffer to put to buffer, int The value to put. value)Put an integer little endian |
| public static void | |
| public static void | |
| public static int | Returns: the number of bytes read into the buffer.the channel to read from. readableByteChannel, ByteBuffer the buffer to read into. byteBuffer)Read content from a |
| public static void | |
| public static int | |
| public static long | Returns: number of bytes remaining in all buffers.the buffers to check buf)Get the remaining bytes in 0 or more buffers. |
| public static void | reset(ByteBuffer
the buffer to reset. buffer)Resets the buffer's endianness to |
| public static int | Returns: spacethe buffer to get the space from buffer)Get the space from the limit to the capacity |
| public static int | Returns: an intA buffer containing an integer in flush mode. The position is updated. buffer)Convert buffer to an integer. |
| public static byte[] | Returns: An array of bytes duplicated from the buffer.The buffer to convert in flush mode. The buffer is not altered. buffer)Convert a ByteBuffer to a byte array. |
| public static ByteBuffer | |
| public static ByteBuffer | |
| public static ByteBuffer | |
| public static ByteBuffer | |
| public static ByteBuffer | Returns: ByteBuffer with provided byte array, in flush modethe byte array to back buffer with. array)Create a new ByteBuffer using provided byte array. |
| public static ByteBuffer | Returns: ByteBuffer with provided byte array, in flush modethe byte array to use. array, int the offset within the byte array to use from offset, int the length in bytes of the array to use length)Create a new ByteBuffer using the provided byte array. |
| public static ByteBuffer | |
| public static String | |
| public static String | Returns: A string showing the pointers and content of the bufferthe buffer to generate a detail string from buffer)Convert Buffer to a detail debug string of pointers and content |
| public static ByteBuffer | |
| public static ByteBuffer | |
| public static String | Returns: A hex stringthe buffer to generate a hex byte summary from buffer)Convert buffer to a Hex String. |
| public static String | Returns: A string showing a summary of the content in hexthe buffer to generate a hex byte summary from buffer)Convert buffer to a Hex Summary String. |
| public static String | Returns: A string showing the buffer IDthe buffet to generate a string ID from buffer)Convert Buffer to string ID independent of content |
| public static int | Returns: an intA buffer containing an integer in flush mode. The position is not changed. buffer)Convert buffer to an integer. |
| public static int | Returns: an int of the buffer bytesA buffer containing an integer in flush mode. The position is not changed. buffer, int the position in the buffer to start reading from position, int the length of the buffer to use for conversion length)Convert buffer to an integer. |
| public static long | Returns: an intA buffer containing an integer in flush mode. The position is not changed. buffer)Convert buffer to an long. |
| public static ByteBuffer | |
| public static ByteBuffer | |
| public static ByteBuffer | |
| public static ByteBuffer | |
| public static String | Returns: The buffer as a string.The buffer to convert in flush mode. The buffer is unchanged buffer)Convert the buffer to an ISO-8859-1 String |
| public static String | Returns: The buffer as a string.The buffer to convert in flush mode. The buffer is unchanged buffer, Charset The charset)Charset to use to convert the bytesConvert the buffer to an ISO-8859-1 String |
| public static String | Returns: The buffer as a string.the buffer to convert buffer, int The position in the buffer to start the string from position, int The length of the buffer length, Charset The charset)Charset to use to convert the bytesConvert a partial buffer to a String. |
| public static String | |
| public static String | Returns: The buffer as a string.The buffer to convert in flush mode. The buffer is unchanged buffer)Convert the buffer to an UTF-8 String |
| public static void |
| decDivisors | back to summary |
|---|---|
| private static final int[] decDivisors | |
| decDivisorsL | back to summary |
|---|---|
| private static final long[] decDivisorsL | |
| DIGIT | back to summary |
|---|---|
| pack-priv static final byte[] DIGIT | |
| EMPTY_BUFFER | back to summary |
|---|---|
| public static final ByteBuffer EMPTY_BUFFER | |
| hexDivisors | back to summary |
|---|---|
| private static final int[] hexDivisors | |
| LOG | back to summary |
|---|---|
| private static final Logger LOG | |
| MINUS | back to summary |
|---|---|
| pack-priv static final byte MINUS | |
| SPACE | back to summary |
|---|---|
| pack-priv static final byte SPACE | |
| TEMP_BUFFER_SIZE | back to summary |
|---|---|
| pack-priv static final int TEMP_BUFFER_SIZE | |
| BufferUtil | back to summary |
|---|---|
| public BufferUtil() | |
| allocate | back to summary |
|---|---|
| public static ByteBuffer allocate(int capacity) Allocate ByteBuffer in flush mode. The position and limit will both be zero, indicating that the buffer is empty and must be flipped before any data is put to it.
| |
| allocate | back to summary |
|---|---|
| public static ByteBuffer allocate(int capacity, boolean direct) Allocates a ByteBuffer in flush mode. The position and limit will both be zero, indicating that the buffer is empty and must be flipped before any data is put to it.
| |
| allocateDirect | back to summary |
|---|---|
| public static ByteBuffer allocateDirect(int capacity) Allocate ByteBuffer in flush mode. The position and limit will both be zero, indicating that the buffer is empty and in flush mode.
| |
| append | back to summary |
|---|---|
| public static void append(ByteBuffer to, byte[] b, int off, int len) throws BufferOverflowException Append bytes to a buffer.
| |
| append | back to summary |
|---|---|
| public static void append(ByteBuffer to, byte[] b) throws BufferOverflowException Append bytes to a buffer.
| |
| append | back to summary |
|---|---|
| public static void append(ByteBuffer to, String s) throws BufferOverflowException Append a string to a buffer.
| |
| append | back to summary |
|---|---|
| public static void append(ByteBuffer to, byte b) Appends a byte to a buffer
| |
| append | back to summary |
|---|---|
| public static int append(ByteBuffer to, ByteBuffer b) Appends a buffer to a buffer
| |
| appendContentChar | back to summary |
|---|---|
| private static void appendContentChar(StringBuilder buf, byte b) | |
| appendDebugString | back to summary |
|---|---|
| private static void appendDebugString(StringBuilder buf, ByteBuffer buffer) | |
| clear | back to summary |
|---|---|
| public static void clear(ByteBuffer buffer) Clears the buffer to be empty in flush mode. The position and limit are set to 0.
| |
| clearToFill | back to summary |
|---|---|
| public static void clearToFill(ByteBuffer buffer) Clear the buffer to be empty in fill mode. The position is set to 0 and the limit is set to the capacity.
| |
| compact | back to summary |
|---|---|
| public static boolean compact(ByteBuffer buffer) Compact the buffer
| |
| copy | back to summary |
|---|---|
| public static ByteBuffer copy(ByteBuffer buffer) Deep copy of a buffer
| |
| ensureCapacity | back to summary |
|---|---|
| public static ByteBuffer ensureCapacity(ByteBuffer buffer, int capacity) | |
| fill | back to summary |
|---|---|
| public static int fill(ByteBuffer to, byte[] b, int off, int len) Like append, but does not throw
| |
| flipToFill | back to summary |
|---|---|
| public static int flipToFill(ByteBuffer buffer) Flip the buffer to fill mode.
The position is set to the first unused position in the buffer
(the old limit) and the limit is set to the capacity.
If the buffer is empty, then this call is effectively
This method is used as a replacement to
| |
| flipToFlush | back to summary |
|---|---|
| public static void flipToFlush(ByteBuffer buffer, int position) Flip the buffer to Flush mode. The limit is set to the first unused byte(the old position) and the position is set to the passed position.
This method is used as a replacement of
| |
| hasContent | back to summary |
|---|---|
| public static boolean hasContent(ByteBuffer buf) Check for a non null and non empty buffer.
| |
| idString | back to summary |
|---|---|
| private static void idString(ByteBuffer buffer, StringBuilder out) Convert Buffer to string ID independent of content | |
| isEmpty | back to summary |
|---|---|
| public static boolean isEmpty(ByteBuffer buf) Check for an empty or null buffer.
| |
| isEmpty | back to summary |
|---|---|
| public static boolean isEmpty(ByteBuffer[] buf) Check for an empty or null buffers.
| |
| isFull | back to summary |
|---|---|
| public static boolean isFull(ByteBuffer buf) Check for a non null and full buffer.
| |
| isPrefix | back to summary |
|---|---|
| public static boolean isPrefix(ByteBuffer prefix, ByteBuffer buffer) | |
| isTheEmptyBuffer | back to summary |
|---|---|
public static boolean isTheEmptyBuffer(ByteBuffer buf)
| |
| length | back to summary |
|---|---|
| public static int length(ByteBuffer buffer) Get remaining from null checked buffer
| |
| put | back to summary |
|---|---|
| public static int put(ByteBuffer from, ByteBuffer to) Put data from one buffer into another, avoiding over/under flows
| |
| putCRLF | back to summary |
|---|---|
| public static void putCRLF(ByteBuffer buffer) | |
| putDecInt | back to summary |
|---|---|
| public static void putDecInt(ByteBuffer buffer, int n) | |
| putDecLong | back to summary |
|---|---|
| public static void putDecLong(ByteBuffer buffer, long n) | |
| putHexInt | back to summary |
|---|---|
| public static void putHexInt(ByteBuffer buffer, int n) | |
| putIntLittleEndian | back to summary |
|---|---|
| public static void putIntLittleEndian(ByteBuffer buffer, int value) Put an integer little endian
| |
| readFrom | back to summary |
|---|---|
| public static void readFrom(File file, ByteBuffer buffer) throws IOException | |
| readFrom | back to summary |
|---|---|
| public static void readFrom(Path path, ByteBuffer buffer) throws IOException | |
| readFrom | back to summary |
|---|---|
| public static int readFrom(ReadableByteChannel readableByteChannel, ByteBuffer byteBuffer) throws IOException Read content from a
| |
| readFrom | back to summary |
|---|---|
| public static void readFrom(InputStream is, int needed, ByteBuffer buffer) throws IOException | |
| readFrom | back to summary |
|---|---|
| public static int readFrom(InputStream is, ByteBuffer buffer) throws IOException | |
| remaining | back to summary |
|---|---|
| public static long remaining(ByteBuffer... buf) Get the remaining bytes in 0 or more buffers.
| |
| reset | back to summary |
|---|---|
| public static void reset(ByteBuffer buffer) Resets the buffer's endianness to
| |
| space | back to summary |
|---|---|
| public static int space(ByteBuffer buffer) Get the space from the limit to the capacity
| |
| takeInt | back to summary |
|---|---|
| public static int takeInt(ByteBuffer buffer) Convert buffer to an integer. Parses up to the first non-numeric character. If no number is found an IllegalArgumentException is thrown
| |
| toArray | back to summary |
|---|---|
| public static byte[] toArray(ByteBuffer buffer) Convert a ByteBuffer to a byte array.
| |
| toBuffer | back to summary |
|---|---|
| public static ByteBuffer toBuffer(int value) | |
| toBuffer | back to summary |
|---|---|
| public static ByteBuffer toBuffer(long value) | |
| toBuffer | back to summary |
|---|---|
| public static ByteBuffer toBuffer(String s) | |
| toBuffer | back to summary |
|---|---|
| public static ByteBuffer toBuffer(String s, Charset charset) | |
| toBuffer | back to summary |
|---|---|
| public static ByteBuffer toBuffer(byte[] array) Create a new ByteBuffer using provided byte array.
| |
| toBuffer | back to summary |
|---|---|
| public static ByteBuffer toBuffer(byte[] array, int offset, int length) Create a new ByteBuffer using the provided byte array.
| |
| toBuffer | back to summary |
|---|---|
| public static ByteBuffer toBuffer(Resource resource, boolean direct) throws IOException | |
| toDetailString | back to summary |
|---|---|
| public static String toDetailString(ByteBuffer[] buffer) | |
| toDetailString | back to summary |
|---|---|
| public static String toDetailString(ByteBuffer buffer) Convert Buffer to a detail debug string of pointers and content
| |
| toDirectBuffer | back to summary |
|---|---|
| public static ByteBuffer toDirectBuffer(String s) | |
| toDirectBuffer | back to summary |
|---|---|
| public static ByteBuffer toDirectBuffer(String s, Charset charset) | |
| toHexString | back to summary |
|---|---|
| public static String toHexString(ByteBuffer buffer) Convert buffer to a Hex String.
| |
| toHexSummary | back to summary |
|---|---|
| public static String toHexSummary(ByteBuffer buffer) Convert buffer to a Hex Summary String.
| |
| toIDString | back to summary |
|---|---|
| public static String toIDString(ByteBuffer buffer) Convert Buffer to string ID independent of content
| |
| toInt | back to summary |
|---|---|
| public static int toInt(ByteBuffer buffer) Convert buffer to an integer. Parses up to the first non-numeric character. If no number is found an IllegalArgumentException is thrown
| |
| toInt | back to summary |
|---|---|
| public static int toInt(ByteBuffer buffer, int position, int length) Convert buffer to an integer. Parses up to the first non-numeric character. If no number is found an IllegalArgumentException is thrown
| |
| toLong | back to summary |
|---|---|
| public static long toLong(ByteBuffer buffer) Convert buffer to an long. Parses up to the first non-numeric character. If no number is found an IllegalArgumentException is thrown
| |
| toMappedBuffer | back to summary |
|---|---|
| public static ByteBuffer toMappedBuffer(Path path) throws IOException | |
| toMappedBuffer | back to summary |
|---|---|
| public static ByteBuffer toMappedBuffer(Path filePath, long pos, long len) throws IOException | |
| toMappedBuffer | back to summary |
|---|---|
| public static ByteBuffer toMappedBuffer(Resource resource) throws IOException | |
| toMappedBuffer | back to summary |
|---|---|
| public static ByteBuffer toMappedBuffer(Resource resource, long pos, long len) throws IOException | |
| toString | back to summary |
|---|---|
| public static String toString(ByteBuffer buffer) Convert the buffer to an ISO-8859-1 String
| |
| toString | back to summary |
|---|---|
| public static String toString(ByteBuffer buffer, Charset charset) Convert the buffer to an ISO-8859-1 String
| |
| toString | back to summary |
|---|---|
| public static String toString(ByteBuffer buffer, int position, int length, Charset charset) Convert a partial buffer to a String.
| |
| toSummaryString | back to summary |
|---|---|
| public static String toSummaryString(ByteBuffer buffer) | |
| toUTF8String | back to summary |
|---|---|
| public static String toUTF8String(ByteBuffer buffer) Convert the buffer to an UTF-8 String
| |
| writeTo | back to summary |
|---|---|
| public static void writeTo(ByteBuffer buffer, OutputStream out) throws IOException | |