public class BufferUtil extends 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 emtpy and will compact the buffer if there is no space.
Modifier and Type | Field and Description |
---|---|
static ByteBuffer |
EMPTY_BUFFER |
Constructor and Description |
---|
BufferUtil() |
Modifier and Type | Method and Description |
---|---|
static ByteBuffer |
allocate(int capacity)
Allocate 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 buffer
|
static void |
append(ByteBuffer to,
byte[] b,
int off,
int len)
Append bytes to a buffer.
|
static int |
append(ByteBuffer to,
ByteBuffer b)
Appends a byte to a buffer
|
static void |
clear(ByteBuffer buffer)
Clear 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 buffer
|
static ByteBuffer |
ensureCapacity(ByteBuffer buffer,
int capacity) |
static int |
fill(ByteBuffer to,
byte[] b,
int off,
int len)
Like append, but does not throw
BufferOverflowException |
static int |
flipPutFlip(ByteBuffer from,
ByteBuffer to)
Deprecated.
|
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 |
isFull(ByteBuffer buf)
Check for a non null and full buffer.
|
static boolean |
isPrefix(ByteBuffer prefix,
ByteBuffer buffer) |
static int |
length(ByteBuffer buffer)
Get remaining from null checked buffer
|
static int |
put(ByteBuffer from,
ByteBuffer to)
Put data from one buffer into another, avoiding over/under flows
|
static 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 |
readFrom(File file,
ByteBuffer buffer) |
static void |
readFrom(InputStream is,
int needed,
ByteBuffer buffer) |
static int |
space(ByteBuffer buffer)
Get the space from the limit to the capacity
|
static 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 |
toBuffer(Resource resource,
boolean direct) |
static ByteBuffer |
toBuffer(String s) |
static ByteBuffer |
toBuffer(String s,
Charset charset) |
static String |
toDebugString(ByteBuffer buffer)
Convert buffer to a Debug String.
|
static String |
toDetailString(ByteBuffer buffer)
Convert Buffer to a detail debug string of pointers and content
|
static String |
toDetailString(ByteBuffer[] buffer) |
static ByteBuffer |
toDirectBuffer(String s) |
static ByteBuffer |
toDirectBuffer(String s,
Charset charset) |
static int |
toInt(ByteBuffer buffer)
Convert buffer to an integer.
|
static long |
toLong(ByteBuffer buffer)
Convert buffer to an long.
|
static ByteBuffer |
toMappedBuffer(File file) |
static String |
toString(ByteBuffer buffer)
Convert the buffer to an ISO-8859-1 String
|
static String |
toString(ByteBuffer buffer,
Charset charset)
Convert the buffer to an ISO-8859-1 String
|
static String |
toString(ByteBuffer buffer,
int position,
int length,
Charset charset)
Convert a partial buffer to an ISO-8859-1 String
|
static String |
toSummaryString(ByteBuffer buffer) |
static String |
toUTF8String(ByteBuffer buffer)
Convert the buffer to an UTF-8 String
|
static void |
writeTo(ByteBuffer buffer,
OutputStream out) |
public static final ByteBuffer EMPTY_BUFFER
public static ByteBuffer allocate(int capacity)
capacity
- capacity of the allocated ByteBufferpublic static ByteBuffer allocateDirect(int capacity)
capacity
- capacity of the allocated ByteBufferpublic static void clear(ByteBuffer buffer)
buffer
- The buffer to clear.public static void clearToFill(ByteBuffer buffer)
buffer
- The buffer to clear.public static int flipToFill(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(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(ByteBuffer buffer)
buffer
- The buffer to convert in flush mode. The buffer is not altered.public static boolean isEmpty(ByteBuffer buf)
buf
- the buffer to checkpublic static boolean hasContent(ByteBuffer buf)
buf
- the buffer to checkpublic static boolean isFull(ByteBuffer buf)
buf
- the buffer to checkpublic static int length(ByteBuffer buffer)
buffer
- The buffer to get the remaining from, in flush mode.public static int space(ByteBuffer buffer)
buffer
- the buffer to get the space frompublic static boolean compact(ByteBuffer buffer)
buffer
- the buffer to compactpublic static int put(ByteBuffer from, ByteBuffer to)
from
- Buffer to take bytes from in flush modeto
- Buffer to put bytes to in fill mode.public static int flipPutFlip(ByteBuffer from, 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(ByteBuffer to, byte[] b, int off, int len) throws BufferOverflowException
to
- Buffer is flush modeb
- bytes to appendoff
- offset into bytelen
- length to appendBufferOverflowException
public static void append(ByteBuffer to, byte b)
to
- Buffer is flush modeb
- byte to appendpublic static int append(ByteBuffer to, ByteBuffer b)
to
- Buffer is flush modeb
- bytes to appendpublic static int fill(ByteBuffer to, byte[] b, int off, int len)
BufferOverflowException
to
- Buffer is flush modeb
- bytes to filloff
- offset into bytelen
- length to fillpublic static void readFrom(File file, ByteBuffer buffer) throws IOException
IOException
public static void readFrom(InputStream is, int needed, ByteBuffer buffer) throws IOException
IOException
public static void writeTo(ByteBuffer buffer, OutputStream out) throws IOException
IOException
public static String toString(ByteBuffer buffer)
buffer
- The buffer to convert in flush mode. The buffer is unchangedpublic static String toUTF8String(ByteBuffer buffer)
buffer
- The buffer to convert in flush mode. The buffer is unchangedpublic static String toString(ByteBuffer buffer, Charset charset)
buffer
- The buffer to convert in flush mode. The buffer is unchangedcharset
- The Charset
to use to convert the bytespublic static String toString(ByteBuffer buffer, int position, int length, Charset charset)
buffer
- The buffer to convert in flush mode. The buffer is unchangedcharset
- The Charset
to use to convert the bytespublic static int toInt(ByteBuffer buffer)
buffer
- A buffer containing an integer in flush mode. The position is not changed.public static int takeInt(ByteBuffer buffer)
buffer
- A buffer containing an integer in flush mode. The position is updated.public static long toLong(ByteBuffer buffer)
buffer
- A buffer containing an integer in flush mode. The position is not changed.public static void putHexInt(ByteBuffer buffer, int n)
public static void putDecInt(ByteBuffer buffer, int n)
public static void putDecLong(ByteBuffer buffer, long n)
public static ByteBuffer toBuffer(int value)
public static ByteBuffer toBuffer(long value)
public static ByteBuffer toBuffer(String s)
public static ByteBuffer toDirectBuffer(String s)
public static ByteBuffer toBuffer(String s, Charset charset)
public static ByteBuffer toDirectBuffer(String s, Charset charset)
public static ByteBuffer toBuffer(byte[] array)
array
- the byte array to back buffer with.public static 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 ByteBuffer toMappedBuffer(File file) throws IOException
IOException
public static ByteBuffer toBuffer(Resource resource, boolean direct) throws IOException
IOException
public static String toSummaryString(ByteBuffer buffer)
public static String toDetailString(ByteBuffer[] buffer)
public static String toDetailString(ByteBuffer buffer)
buffer
- public static String toDebugString(ByteBuffer buffer)
buffer
- public static void putCRLF(ByteBuffer buffer)
public static boolean isPrefix(ByteBuffer prefix, ByteBuffer buffer)
public static ByteBuffer ensureCapacity(ByteBuffer buffer, int capacity)
Copyright © 1995-2015 Webtide. All Rights Reserved.