Class BufferUtil
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.
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic ByteBuffer
allocate
(int capacity) Allocate ByteBuffer in flush mode.static ByteBuffer
allocate
(int capacity, boolean direct) Allocates a ByteBuffer in flush mode.static ByteBuffer
allocateDirect
(int capacity) Allocate ByteBuffer in flush mode.static void
append
(ByteBuffer to, byte b) Appends a byte to a bufferstatic void
append
(ByteBuffer to, byte[] b) Append bytes to a buffer.static void
append
(ByteBuffer to, byte[] b, int off, int len) Append bytes to a buffer.static void
append
(ByteBuffer to, String s) Append a string to a buffer.static int
append
(ByteBuffer to, ByteBuffer b) Appends a buffer to a bufferstatic void
clear
(ByteBuffer buffer) Clears the buffer to be empty in flush mode.static void
clearToFill
(ByteBuffer buffer) Clear the buffer to be empty in fill mode.static boolean
compact
(ByteBuffer buffer) Compact the bufferstatic ByteBuffer
copy
(ByteBuffer buffer) Deep copy of a bufferstatic ByteBuffer
ensureCapacity
(ByteBuffer buffer, int capacity) static int
fill
(ByteBuffer to, byte[] b, int off, int len) Like append, but does not throwBufferOverflowException
static int
flipToFill
(ByteBuffer buffer) Flip the buffer to fill mode.static void
flipToFlush
(ByteBuffer buffer, int position) Flip the buffer to Flush mode.static boolean
hasContent
(ByteBuffer buf) Check for a non null and non empty buffer.static boolean
isEmpty
(ByteBuffer buf) Check for an empty or null buffer.static boolean
isEmpty
(ByteBuffer[] buf) Check for an empty or null buffers.static boolean
isFull
(ByteBuffer buf) Check for a non null and full buffer.static boolean
isPrefix
(ByteBuffer prefix, ByteBuffer buffer) static boolean
static int
length
(ByteBuffer buffer) Get remaining from null checked bufferstatic int
put
(ByteBuffer from, ByteBuffer to) Put data from one buffer into another, avoiding over/under flowsstatic void
putCRLF
(ByteBuffer buffer) static void
putDecInt
(ByteBuffer buffer, int n) static void
putDecLong
(ByteBuffer buffer, long n) static void
putHexInt
(ByteBuffer buffer, int n) static void
putIntLittleEndian
(ByteBuffer buffer, int value) Put an integer little endianstatic void
readFrom
(File file, ByteBuffer buffer) static void
readFrom
(InputStream is, int needed, ByteBuffer buffer) static int
readFrom
(InputStream is, ByteBuffer buffer) static int
readFrom
(ReadableByteChannel readableByteChannel, ByteBuffer byteBuffer) Read content from aReadableByteChannel
into a buffer.static void
readFrom
(Path path, ByteBuffer buffer) static long
remaining
(ByteBuffer... buf) Get the remaining bytes in 0 or more buffers.static void
reset
(ByteBuffer buffer) Resets the buffer's endianness toByteOrder.BIG_ENDIAN
and clears the buffer to be empty in flush mode.static int
space
(ByteBuffer buffer) Get the space from the limit to the capacitystatic int
takeInt
(ByteBuffer buffer) Convert buffer to an integer.static byte[]
toArray
(ByteBuffer buffer) Convert a ByteBuffer to a byte array.static ByteBuffer
toBuffer
(byte[] array) Create a new ByteBuffer using provided byte array.static ByteBuffer
toBuffer
(byte[] array, int offset, int length) Create a new ByteBuffer using the provided byte array.static ByteBuffer
toBuffer
(int value) static ByteBuffer
toBuffer
(long value) static ByteBuffer
static ByteBuffer
static ByteBuffer
static String
toDetailString
(ByteBuffer buffer) Convert Buffer to a detail debug string of pointers and contentstatic String
toDetailString
(ByteBuffer[] buffer) static ByteBuffer
static ByteBuffer
toDirectBuffer
(String s, Charset charset) static String
toHexString
(ByteBuffer buffer) Convert buffer to a Hex String.static String
toHexSummary
(ByteBuffer buffer) Convert buffer to a Hex Summary String.static String
toIDString
(ByteBuffer buffer) Convert Buffer to string ID independent of contentstatic int
toInt
(ByteBuffer buffer) Convert buffer to an integer.static int
toInt
(ByteBuffer buffer, int position, int length) Convert buffer to an integer.static long
toLong
(ByteBuffer buffer) Convert buffer to an long.static ByteBuffer
toMappedBuffer
(Path path) static ByteBuffer
toMappedBuffer
(Path filePath, long pos, long len) static ByteBuffer
toMappedBuffer
(Resource resource) static ByteBuffer
toMappedBuffer
(Resource resource, long pos, long len) static String
toString
(ByteBuffer buffer) Convert the buffer to an ISO-8859-1 Stringstatic String
toString
(ByteBuffer buffer, int position, int length, Charset charset) Convert a partial buffer to a String.static String
toString
(ByteBuffer buffer, Charset charset) Convert buffer to a String with specified Charsetstatic String
toSummaryString
(ByteBuffer buffer) static String
toUTF8String
(ByteBuffer buffer) Convert the buffer to an UTF-8 Stringstatic void
writeTo
(ByteBuffer buffer, OutputStream out)
-
Field Details
-
EMPTY_BYTES
public static final byte[] EMPTY_BYTES -
EMPTY_BUFFER
-
-
Constructor Details
-
BufferUtil
public BufferUtil()
-
-
Method Details
-
allocate
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.- Parameters:
capacity
- capacity of the allocated ByteBuffer- Returns:
- Buffer
-
allocateDirect
Allocate ByteBuffer in flush mode. The position and limit will both be zero, indicating that the buffer is empty and in flush mode.- Parameters:
capacity
- capacity of the allocated ByteBuffer- Returns:
- Buffer
-
allocate
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.- Parameters:
capacity
- capacity of the allocated ByteBufferdirect
- whether the ByteBuffer is direct- Returns:
- the newly allocated ByteBuffer
-
copy
Deep copy of a buffer- Parameters:
buffer
- The buffer to copy- Returns:
- A copy of the buffer
-
reset
Resets the buffer's endianness toByteOrder.BIG_ENDIAN
and clears the buffer to be empty in flush mode. The position and limit are set to 0.- Parameters:
buffer
- the buffer to reset.
-
clear
Clears the buffer to be empty in flush mode. The position and limit are set to 0.- Parameters:
buffer
- the buffer to clear.
-
clearToFill
Clear the buffer to be empty in fill mode. The position is set to 0 and the limit is set to the capacity.- Parameters:
buffer
- The buffer to clear.
-
flipToFill
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 effectivelyclearToFill(ByteBuffer)
. If there is no unused space to fill, aByteBuffer.compact()
is done to attempt to create space.This method is used as a replacement to
ByteBuffer.compact()
.- Parameters:
buffer
- The buffer to flip- Returns:
- The position of the valid data before the flipped position. This value should be
passed to a subsequent call to
flipToFlush(ByteBuffer, int)
-
flipToFlush
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
Buffer.flip()
.- Parameters:
buffer
- the buffer to be flippedposition
- The position of valid data to flip to. This should be the return value of the previous call toflipToFill(ByteBuffer)
-
putIntLittleEndian
Put an integer little endian- Parameters:
buffer
- The buffer to put tovalue
- The value to put.
-
toArray
Convert a ByteBuffer to a byte array.- Parameters:
buffer
- The buffer to convert in flush mode. The buffer is not altered.- Returns:
- An array of bytes duplicated from the buffer.
-
isTheEmptyBuffer
- Parameters:
buf
- the buffer to check- Returns:
- true if buf is equal to EMPTY_BUFFER
-
isEmpty
Check for an empty or null buffer.- Parameters:
buf
- the buffer to check- Returns:
- true if the buffer is null or empty.
-
isEmpty
Check for an empty or null buffers.- Parameters:
buf
- the buffer to check- Returns:
- true if the buffer is null or empty.
-
remaining
Get the remaining bytes in 0 or more buffers.- Parameters:
buf
- the buffers to check- Returns:
- number of bytes remaining in all buffers.
-
hasContent
Check for a non null and non empty buffer.- Parameters:
buf
- the buffer to check- Returns:
- true if the buffer is not null and not empty.
-
isFull
Check for a non null and full buffer.- Parameters:
buf
- the buffer to check- Returns:
- true if the buffer is not null and the limit equals the capacity.
-
length
Get remaining from null checked buffer- Parameters:
buffer
- The buffer to get the remaining from, in flush mode.- Returns:
- 0 if the buffer is null, else the bytes remaining in the buffer.
-
space
Get the space from the limit to the capacity- Parameters:
buffer
- the buffer to get the space from- Returns:
- space
-
compact
Compact the buffer- Parameters:
buffer
- the buffer to compact- Returns:
- true if the compact made a full buffer have space
-
put
Put data from one buffer into another, avoiding over/under flows- Parameters:
from
- Buffer to take bytes from in flush modeto
- Buffer to put bytes to in fill mode.- Returns:
- number of bytes moved
-
append
Append bytes to a buffer.- Parameters:
to
- Buffer is flush modeb
- bytes to appendoff
- offset into bytelen
- length to append- Throws:
BufferOverflowException
- if unable to append buffer due to space limits
-
append
Append bytes to a buffer.- Parameters:
to
- Buffer is flush modeb
- bytes to append- Throws:
BufferOverflowException
- if unable to append buffer due to space limits
-
append
Append a string to a buffer.- Parameters:
to
- Buffer is flush modes
- String to append as UTF8- Throws:
BufferOverflowException
- if unable to append buffer due to space limits
-
append
Appends a byte to a buffer- Parameters:
to
- Buffer is flush modeb
- byte to append- Throws:
BufferOverflowException
- if unable to append buffer due to space limits
-
append
Appends a buffer to a buffer- Parameters:
to
- Buffer is flush modeb
- buffer to append- Returns:
- The position of the valid data before the flipped position.
-
fill
Like append, but does not throwBufferOverflowException
- Parameters:
to
- Buffer The buffer to fill to. The buffer will be flipped to fill mode and then flipped back to flush mode.b
- bytes The bytes to filloff
- offset into byteslen
- length to fill- Returns:
- the number of bytes taken from the buffer.
-
readFrom
- Throws:
IOException
-
readFrom
- Throws:
IOException
-
readFrom
public static int readFrom(ReadableByteChannel readableByteChannel, ByteBuffer byteBuffer) throws IOException Read content from aReadableByteChannel
into a buffer. This may spin ifReadableByteChannel.read(ByteBuffer)
returns 0 in which case this will callThread.onSpinWait()
.- Parameters:
readableByteChannel
- the channel to read from.byteBuffer
- the buffer to read into.- Returns:
- the number of bytes read into the buffer.
- Throws:
IOException
- if an I/O error occurs.
-
readFrom
- Throws:
IOException
-
readFrom
- Throws:
IOException
-
writeTo
- Throws:
IOException
-
toString
Convert the buffer to an ISO-8859-1 String- Parameters:
buffer
- The buffer to convert in flush mode. The buffer is unchanged- Returns:
- The buffer as a string.
-
toString
Convert buffer to a String with specified Charset- Parameters:
buffer
- The buffer to convert in flush mode. The buffer is unchangedcharset
- TheCharset
to use to convert the bytes- Returns:
- The buffer as a string.
-
toString
Convert a partial buffer to a String.- Parameters:
buffer
- the buffer to convertposition
- The position in the buffer to start the string fromlength
- The length of the buffercharset
- TheCharset
to use to convert the bytes- Returns:
- The buffer as a string.
-
toUTF8String
Convert the buffer to an UTF-8 String- Parameters:
buffer
- The buffer to convert in flush mode. The buffer is unchanged- Returns:
- The buffer as a string.
-
toInt
Convert buffer to an integer. Parses up to the first non-numeric character. If no number is found an IllegalArgumentException is thrown- Parameters:
buffer
- A buffer containing an integer in flush mode. The position is not changed.- Returns:
- an int
-
toInt
Convert buffer to an integer. Parses up to the first non-numeric character. If no number is found an IllegalArgumentException is thrown- Parameters:
buffer
- A buffer containing an integer in flush mode. The position is not changed.position
- the position in the buffer to start reading fromlength
- the length of the buffer to use for conversion- Returns:
- an int of the buffer bytes
-
takeInt
Convert buffer to an integer. Parses up to the first non-numeric character. If no number is found an IllegalArgumentException is thrown- Parameters:
buffer
- A buffer containing an integer in flush mode. The position is updated.- Returns:
- an int
-
toLong
Convert buffer to an long. Parses up to the first non-numeric character. If no number is found an IllegalArgumentException is thrown- Parameters:
buffer
- A buffer containing an integer in flush mode. The position is not changed.- Returns:
- an int
-
putHexInt
-
putDecInt
-
putDecLong
-
toBuffer
-
toBuffer
-
toBuffer
-
toBuffer
-
toBuffer
Create a new ByteBuffer using provided byte array.- Parameters:
array
- the byte array to back buffer with.- Returns:
- ByteBuffer with provided byte array, in flush mode
-
toBuffer
Create a new ByteBuffer using the provided byte array.- Parameters:
array
- the byte array to use.offset
- the offset within the byte array to use fromlength
- the length in bytes of the array to use- Returns:
- ByteBuffer with provided byte array, in flush mode
-
toBuffer
- Throws:
IOException
-
toDirectBuffer
-
toDirectBuffer
-
toMappedBuffer
- Throws:
IOException
-
toMappedBuffer
- Throws:
IOException
-
toMappedBuffer
- Throws:
IOException
-
toMappedBuffer
- Throws:
IOException
-
toSummaryString
-
toDetailString
-
toDetailString
Convert Buffer to a detail debug string of pointers and content- Parameters:
buffer
- the buffer to generate a detail string from- Returns:
- A string showing the pointers and content of the buffer
-
toIDString
Convert Buffer to string ID independent of content- Parameters:
buffer
- the buffet to generate a string ID from- Returns:
- A string showing the buffer ID
-
toHexSummary
Convert buffer to a Hex Summary String.- Parameters:
buffer
- the buffer to generate a hex byte summary from- Returns:
- A string showing a summary of the content in hex
-
toHexString
Convert buffer to a Hex String.- Parameters:
buffer
- the buffer to generate a hex byte summary from- Returns:
- A hex string
-
putCRLF
-
isPrefix
-
ensureCapacity
-