public class BufferUtil
extends java.lang.Object
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 | Description |
---|---|---|
static java.nio.ByteBuffer |
EMPTY_BUFFER |
Constructor | Description |
---|---|
BufferUtil() |
Modifier and Type | Method | Description |
---|---|---|
static java.nio.ByteBuffer |
allocate(int capacity) |
Allocate ByteBuffer in flush mode.
|
static java.nio.ByteBuffer |
allocateDirect(int capacity) |
Allocate ByteBuffer in flush mode.
|
static void |
append(java.nio.ByteBuffer to,
byte b) |
Appends a byte to a buffer
|
static void |
append(java.nio.ByteBuffer to,
byte[] b,
int off,
int len) |
Append bytes to a buffer.
|
static int |
append(java.nio.ByteBuffer to,
java.nio.ByteBuffer b) |
Appends a buffer to a buffer
|
static void |
clear(java.nio.ByteBuffer buffer) |
Clear the buffer to be empty in flush mode.
|
static void |
clearToFill(java.nio.ByteBuffer buffer) |
Clear the buffer to be empty in fill mode.
|
static boolean |
compact(java.nio.ByteBuffer buffer) |
Compact the buffer
|
static java.nio.ByteBuffer |
ensureCapacity(java.nio.ByteBuffer buffer,
int capacity) |
|
static int |
fill(java.nio.ByteBuffer to,
byte[] b,
int off,
int len) |
Like append, but does not throw
BufferOverflowException |
static int |
flipPutFlip(java.nio.ByteBuffer from,
java.nio.ByteBuffer to) |
Deprecated.
|
static int |
flipToFill(java.nio.ByteBuffer buffer) |
Flip the buffer to fill mode.
|
static void |
flipToFlush(java.nio.ByteBuffer buffer,
int position) |
Flip the buffer to Flush mode.
|
static boolean |
hasContent(java.nio.ByteBuffer buf) |
Check for a non null and non empty buffer.
|
static boolean |
isEmpty(java.nio.ByteBuffer buf) |
Check for an empty or null buffer.
|
static boolean |
isFull(java.nio.ByteBuffer buf) |
Check for a non null and full buffer.
|
static boolean |
isMappedBuffer(java.nio.ByteBuffer buffer) |
|
static boolean |
isPrefix(java.nio.ByteBuffer prefix,
java.nio.ByteBuffer buffer) |
|
static boolean |
isTheEmptyBuffer(java.nio.ByteBuffer buf) |
|
static int |
length(java.nio.ByteBuffer buffer) |
Get remaining from null checked buffer
|
static int |
put(java.nio.ByteBuffer from,
java.nio.ByteBuffer to) |
Put data from one buffer into another, avoiding over/under flows
|
static void |
putCRLF(java.nio.ByteBuffer buffer) |
|
static void |
putDecInt(java.nio.ByteBuffer buffer,
int n) |
|
static void |
putDecLong(java.nio.ByteBuffer buffer,
long n) |
|
static void |
putHexInt(java.nio.ByteBuffer buffer,
int n) |
|
static void |
readFrom(java.io.File file,
java.nio.ByteBuffer buffer) |
|
static void |
readFrom(java.io.InputStream is,
int needed,
java.nio.ByteBuffer buffer) |
|
static int |
space(java.nio.ByteBuffer buffer) |
Get the space from the limit to the capacity
|
static int |
takeInt(java.nio.ByteBuffer buffer) |
Convert buffer to an integer.
|
static byte[] |
toArray(java.nio.ByteBuffer buffer) |
Convert a ByteBuffer to a byte array.
|
static java.nio.ByteBuffer |
toBuffer(byte[] array) |
Create a new ByteBuffer using provided byte array.
|
static java.nio.ByteBuffer |
toBuffer(byte[] array,
int offset,
int length) |
Create a new ByteBuffer using the provided byte array.
|
static java.nio.ByteBuffer |
toBuffer(int value) |
|
static java.nio.ByteBuffer |
toBuffer(long value) |
|
static java.nio.ByteBuffer |
toBuffer(java.lang.String s) |
|
static java.nio.ByteBuffer |
toBuffer(java.lang.String s,
java.nio.charset.Charset charset) |
|
static java.nio.ByteBuffer |
toBuffer(Resource resource,
boolean direct) |
|
static java.lang.String |
toDetailString(java.nio.ByteBuffer buffer) |
Convert Buffer to a detail debug string of pointers and content
|
static java.lang.String |
toDetailString(java.nio.ByteBuffer[] buffer) |
|
static java.nio.ByteBuffer |
toDirectBuffer(java.lang.String s) |
|
static java.nio.ByteBuffer |
toDirectBuffer(java.lang.String s,
java.nio.charset.Charset charset) |
|
static java.lang.String |
toHexString(java.nio.ByteBuffer buffer) |
Convert buffer to a Hex String.
|
static java.lang.String |
toHexSummary(java.nio.ByteBuffer buffer) |
Convert buffer to a Hex Summary String.
|
static java.lang.String |
toIDString(java.nio.ByteBuffer buffer) |
Convert Buffer to string ID independent of content
|
static int |
toInt(java.nio.ByteBuffer buffer) |
Convert buffer to an integer.
|
static int |
toInt(java.nio.ByteBuffer buffer,
int position,
int length) |
Convert buffer to an integer.
|
static long |
toLong(java.nio.ByteBuffer buffer) |
Convert buffer to an long.
|
static java.nio.ByteBuffer |
toMappedBuffer(java.io.File file) |
|
static java.lang.String |
toString(java.nio.ByteBuffer buffer) |
Convert the buffer to an ISO-8859-1 String
|
static java.lang.String |
toString(java.nio.ByteBuffer buffer,
int position,
int length,
java.nio.charset.Charset charset) |
Convert a partial buffer to a String.
|
static java.lang.String |
toString(java.nio.ByteBuffer buffer,
java.nio.charset.Charset charset) |
Convert the buffer to an ISO-8859-1 String
|
static java.lang.String |
toSummaryString(java.nio.ByteBuffer buffer) |
|
static java.lang.String |
toUTF8String(java.nio.ByteBuffer buffer) |
Convert the buffer to an UTF-8 String
|
static void |
writeTo(java.nio.ByteBuffer buffer,
java.io.OutputStream out) |
public static java.nio.ByteBuffer allocate(int capacity)
capacity
- capacity of the allocated ByteBufferpublic static java.nio.ByteBuffer allocateDirect(int capacity)
capacity
- capacity of the allocated ByteBufferpublic static void clear(java.nio.ByteBuffer buffer)
buffer
- The buffer to clear.public static void clearToFill(java.nio.ByteBuffer buffer)
buffer
- The buffer to clear.public static int flipToFill(java.nio.ByteBuffer buffer)
clearToFill(ByteBuffer)
.
If there is no unused space to fill, a ByteBuffer.compact()
is done to attempt
to create space.
This method is used as a replacement to ByteBuffer.compact()
.
buffer
- The buffer to flipflipToFlush(ByteBuffer, int)
public static void flipToFlush(java.nio.ByteBuffer buffer, int position)
This method is used as a replacement of Buffer.flip()
.
buffer
- the buffer to be flippedposition
- The position of valid data to flip to. This should
be the return value of the previous call to flipToFill(ByteBuffer)
public static byte[] toArray(java.nio.ByteBuffer buffer)
buffer
- The buffer to convert in flush mode. The buffer is not altered.public static boolean isTheEmptyBuffer(java.nio.ByteBuffer buf)
buf
- the buffer to checkpublic static boolean isEmpty(java.nio.ByteBuffer buf)
buf
- the buffer to checkpublic static boolean hasContent(java.nio.ByteBuffer buf)
buf
- the buffer to checkpublic static boolean isFull(java.nio.ByteBuffer buf)
buf
- the buffer to checkpublic static int length(java.nio.ByteBuffer buffer)
buffer
- The buffer to get the remaining from, in flush mode.public static int space(java.nio.ByteBuffer buffer)
buffer
- the buffer to get the space frompublic static boolean compact(java.nio.ByteBuffer buffer)
buffer
- the buffer to compactpublic static int put(java.nio.ByteBuffer from, java.nio.ByteBuffer to)
from
- Buffer to take bytes from in flush modeto
- Buffer to put bytes to in fill mode.public static int flipPutFlip(java.nio.ByteBuffer from, java.nio.ByteBuffer to)
append(ByteBuffer, ByteBuffer)
from
- Buffer to take bytes from in flush modeto
- Buffer to put bytes to in flush mode. The buffer is flipToFill before the put and flipToFlush after.public static void append(java.nio.ByteBuffer to, byte[] b, int off, int len) throws java.nio.BufferOverflowException
to
- Buffer is flush modeb
- bytes to appendoff
- offset into bytelen
- length to appendjava.nio.BufferOverflowException
- if unable to append buffer due to space limitspublic static void append(java.nio.ByteBuffer to, byte b)
to
- Buffer is flush modeb
- byte to appendpublic static int append(java.nio.ByteBuffer to, java.nio.ByteBuffer b)
to
- Buffer is flush modeb
- buffer to appendpublic static int fill(java.nio.ByteBuffer to, byte[] b, int off, int len)
BufferOverflowException
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 fillpublic static void readFrom(java.io.File file, java.nio.ByteBuffer buffer) throws java.io.IOException
java.io.IOException
public static void readFrom(java.io.InputStream is, int needed, java.nio.ByteBuffer buffer) throws java.io.IOException
java.io.IOException
public static void writeTo(java.nio.ByteBuffer buffer, java.io.OutputStream out) throws java.io.IOException
java.io.IOException
public static java.lang.String toString(java.nio.ByteBuffer buffer)
buffer
- The buffer to convert in flush mode. The buffer is unchangedpublic static java.lang.String toUTF8String(java.nio.ByteBuffer buffer)
buffer
- The buffer to convert in flush mode. The buffer is unchangedpublic static java.lang.String toString(java.nio.ByteBuffer buffer, java.nio.charset.Charset charset)
buffer
- The buffer to convert in flush mode. The buffer is unchangedcharset
- The Charset
to use to convert the bytespublic static java.lang.String toString(java.nio.ByteBuffer buffer, int position, int length, java.nio.charset.Charset charset)
buffer
- the buffer to convertposition
- The position in the buffer to start the string fromlength
- The length of the buffercharset
- The Charset
to use to convert the bytespublic static int toInt(java.nio.ByteBuffer buffer)
buffer
- A buffer containing an integer in flush mode. The position is not changed.public static int toInt(java.nio.ByteBuffer buffer, int position, int length)
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 conversionpublic static int takeInt(java.nio.ByteBuffer buffer)
buffer
- A buffer containing an integer in flush mode. The position is updated.public static long toLong(java.nio.ByteBuffer buffer)
buffer
- A buffer containing an integer in flush mode. The position is not changed.public static void putHexInt(java.nio.ByteBuffer buffer, int n)
public static void putDecInt(java.nio.ByteBuffer buffer, int n)
public static void putDecLong(java.nio.ByteBuffer buffer, long n)
public static java.nio.ByteBuffer toBuffer(int value)
public static java.nio.ByteBuffer toBuffer(long value)
public static java.nio.ByteBuffer toBuffer(java.lang.String s)
public static java.nio.ByteBuffer toBuffer(java.lang.String s, java.nio.charset.Charset charset)
public static java.nio.ByteBuffer toBuffer(byte[] array)
array
- the byte array to back buffer with.public static java.nio.ByteBuffer toBuffer(byte[] array, int offset, int length)
array
- the byte array to use.offset
- the offset within the byte array to use fromlength
- the length in bytes of the array to usepublic static java.nio.ByteBuffer toDirectBuffer(java.lang.String s)
public static java.nio.ByteBuffer toDirectBuffer(java.lang.String s, java.nio.charset.Charset charset)
public static java.nio.ByteBuffer toMappedBuffer(java.io.File file) throws java.io.IOException
java.io.IOException
public static boolean isMappedBuffer(java.nio.ByteBuffer buffer)
public static java.nio.ByteBuffer toBuffer(Resource resource, boolean direct) throws java.io.IOException
java.io.IOException
public static java.lang.String toSummaryString(java.nio.ByteBuffer buffer)
public static java.lang.String toDetailString(java.nio.ByteBuffer[] buffer)
public static java.lang.String toIDString(java.nio.ByteBuffer buffer)
buffer
- the buffet to generate a string ID frompublic static java.lang.String toDetailString(java.nio.ByteBuffer buffer)
buffer
- the buffer to generate a detail string frompublic static java.lang.String toHexSummary(java.nio.ByteBuffer buffer)
buffer
- the buffer to generate a hex byte summary frompublic static java.lang.String toHexString(java.nio.ByteBuffer buffer)
buffer
- the buffer to generate a hex byte summary frompublic static void putCRLF(java.nio.ByteBuffer buffer)
public static boolean isPrefix(java.nio.ByteBuffer prefix, java.nio.ByteBuffer buffer)
public static java.nio.ByteBuffer ensureCapacity(java.nio.ByteBuffer buffer, int capacity)
Copyright © 1995–2018 Webtide. All rights reserved.