BufferUtils

Buffer utility methods. <p> The standard JVM {@link 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: </p> <p> <pre> buffer.clear(); channel.write(buffer); </pre> <p> Which looks as if it should write no data, but in fact writes the buffer worth of garbage. </p> <p> The BufferUtils 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. </p> <p> Thus this class provides alternate implementations of {@link #allocate(int)}, {@link #allocateDirect(int)} and {@link #clear(ByteBuffer)} that leave the buffer in flush mode. Thus the following tests will pass: </p> <p> <pre> ByteBuffer buffer = BufferUtils.allocate(1024); assert (buffer.remaining() == 0); BufferUtils.clear(buffer); assert (buffer.remaining() == 0); </pre> <p> If the BufferUtils methods {@link #fill(ByteBuffer, byte[], int, int)}, {@link #append(ByteBuffer, byte[], int, int)} or {@link #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: </p> <p> <pre> int pos = BufferUtils.flipToFill(buffer); try { buffer.put(data); } finally { flipToFlush(buffer, pos); } </pre> <p> The flipToFill method will effectively clear the buffer if it is empty and will compact the buffer if there is no space. </p>

class BufferUtils {}

Members

Static functions

allocate
ByteBuffer allocate(size_t 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.

allocateDirect
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
void append(ByteBuffer to, byte[] b, int off, int len)

Append bytes to a buffer.

append
void append(ByteBuffer to, byte b)

Appends a byte to a buffer

append
int append(ByteBuffer to, ByteBuffer b)

Appends a buffer to a buffer

clear
void clear(ByteBuffer buffer)

Clear the buffer to be empty in flush mode. The position and limit are set to 0;

clearToFill
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.

clone
ByteBuffer clone(ByteBuffer buf)
Undocumented in source. Be warned that the author may not have intended to support it.
compact
bool compact(ByteBuffer buffer)

Compact the buffer

ensureCapacity
ByteBuffer ensureCapacity(ByteBuffer buffer, size_t capacity)
Undocumented in source. Be warned that the author may not have intended to support it.
fill
int fill(ByteBuffer to, byte[] b, int off, int len)

Like append, but does not throw {@link BufferOverflowException}

flipPutFlip
int flipPutFlip(ByteBuffer from, ByteBuffer to)

Put data from one buffer into another, avoiding over/under flows

flipToFill
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 {@link #clearToFill(ByteBuffer)}. If there is no unused space to fill, a {@link ByteBuffer#compact()} is done to attempt to create space. <p> This method is used as a replacement to {@link ByteBuffer#compact()}.

flipToFlush
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. <p> This method is used as a replacement of {@link Buffer#flip()}.

hasContent
bool hasContent(ByteBuffer buf)

Check for a non null and non empty buffer.

isEmpty
bool isEmpty(ByteBuffer buf)

Check for an empty or null buffer.

isFull
bool isFull(ByteBuffer buf)

Check for a non null and full buffer.

isPrefix
bool isPrefix(ByteBuffer prefix, ByteBuffer buffer)
Undocumented in source. Be warned that the author may not have intended to support it.
length
int length(ByteBuffer buffer)

Get remaining from null checked buffer

normalizeBufferSize
int normalizeBufferSize(int capacity)

The capacity modulo 1024 is 0

put
int put(ByteBuffer from, ByteBuffer to)

Put data from one buffer into another, avoiding over/under flows

putCRLF
void putCRLF(ByteBuffer buffer)
Undocumented in source. Be warned that the author may not have intended to support it.
putDecInt
void putDecInt(ByteBuffer buffer, int n)
Undocumented in source. Be warned that the author may not have intended to support it.
putDecLong
void putDecLong(ByteBuffer buffer, long n)
Undocumented in source. Be warned that the author may not have intended to support it.
putHexInt
void putHexInt(ByteBuffer buffer, int n)
Undocumented in source. Be warned that the author may not have intended to support it.
remaining
long remaining(ByteBuffer[] byteBuffers)
Undocumented in source. Be warned that the author may not have intended to support it.
space
int space(ByteBuffer buffer)

Get the space from the limit to the capacity

takeInt
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
byte[] toArray(ByteBuffer buffer, bool canDuplicate)

Convert a ByteBuffer to a byte array.

toBuffer
ByteBuffer toBuffer(int value)
Undocumented in source. Be warned that the author may not have intended to support it.
toBuffer
ByteBuffer toBuffer(long value)
Undocumented in source. Be warned that the author may not have intended to support it.
toBuffer
ByteBuffer toBuffer(string s)
Undocumented in source. Be warned that the author may not have intended to support it.
toBuffer
ByteBuffer toBuffer(byte[] array)

Create a new ByteBuffer using provided byte array.

toBuffer
ByteBuffer toBuffer(byte[] array, int offset, int length)

Create a new ByteBuffer using the provided byte array.

toDetailString
string toDetailString(ByteBuffer[] buffer)
Undocumented in source. Be warned that the author may not have intended to support it.
toDetailString
string toDetailString(ByteBuffer buffer)

Convert Buffer to a detail debug string of pointers and content

toDirectBuffer
ByteBuffer toDirectBuffer(string s)
Undocumented in source. Be warned that the author may not have intended to support it.
toHeapBuffer
ByteBuffer toHeapBuffer(ByteBuffer buf)
Undocumented in source. Be warned that the author may not have intended to support it.
toHexString
string toHexString(ByteBuffer buffer)

Convert buffer to a Hex string.

toHexSummary
string toHexSummary(ByteBuffer buffer)

Convert buffer to a Hex Summary string.

toIDString
string toIDString(ByteBuffer buffer)

Convert Buffer to string ID independent of content

toInt
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
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
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

toString
string toString(ByteBuffer buffer)

Convert the buffer to an ISO-8859-1 string

toString
string toString(ByteBuffer buffer, int position, int length)

Convert a partial buffer to a string.

toSummaryString
string toSummaryString(ByteBuffer buffer)
Undocumented in source. Be warned that the author may not have intended to support it.
wrap
ByteBuffer wrap(byte[] array, int offset, int length)

Wraps a byte array into a buffer.

wrap
ByteBuffer wrap(byte[] array)

Wraps a byte array into a buffer.

writeTo
void writeTo(ByteBuffer buffer, Appender!(byte[]) ot)
Undocumented in source. Be warned that the author may not have intended to support it.

Static variables

EMPTY_BUFFER
ByteBuffer EMPTY_BUFFER;
Undocumented in source.
EMPTY_BYTE_BUFFER_ARRAY
ByteBuffer[] EMPTY_BYTE_BUFFER_ARRAY;
Undocumented in source.

Variables

DIGIT
enum byte[] DIGIT;
Undocumented in source.
MINUS
enum byte MINUS;
Undocumented in source.
SPACE
enum byte SPACE;
Undocumented in source.
TEMP_BUFFER_SIZE
enum int TEMP_BUFFER_SIZE;
Undocumented in source.

Meta