public class BufferUtil extends Object
ByteBuffer
's in a more flexible way.
The standard Buffer.flip()
assumes that once flipped to flush a buffer,
that it will be completely emptied before being cleared ready to be filled again.
The flipToFill(ByteBuffer)
and flipToFlush(ByteBuffer, int)
methods provided here
do not assume that the buffer is empty and will preserve content when flipped.
ByteBuffers can be considered in one of two modes: Flush mode where valid content is contained between
position and limit which is consumed by advancing the position; and Fill mode where empty space is between
the position and limit, which is filled by advancing the position. In fill mode, there may be valid data
in the buffer before the position and the start of this data is given by the return value of flipToFill(ByteBuffer)
A typical pattern for using the buffers in this style is:
ByteBuffer buf = BufferUtil.allocate(4096); while(in.isOpen()) { int pos=BufferUtil.flipToFill(buf); if (in.read(buf)<0) break; BufferUtil.flipToFlush(buf,pos); out.write(buf); }
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) |
static void |
append(ByteBuffer to,
byte[] b,
int off,
int len) |
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 int |
flipPutFlip(ByteBuffer from,
ByteBuffer to)
Put data from one buffer into another, avoiding over/under flows
|
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 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(File file) |
static ByteBuffer |
toBuffer(int value) |
static ByteBuffer |
toBuffer(long value) |
static ByteBuffer |
toBuffer(String s) |
static ByteBuffer |
toBuffer(String s,
Charset charset) |
static String |
toDetailString(ByteBuffer buffer) |
static String |
toDetailString(ByteBuffer[] buffer) |
static int |
toInt(ByteBuffer buffer)
Convert buffer to an integer.
|
static long |
toLong(ByteBuffer buffer)
Convert buffer to an long.
|
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
- public static ByteBuffer allocateDirect(int capacity)
capacity
- public 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
- public static boolean compact(ByteBuffer buffer)
buffer
- public 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)
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)
public static void append(ByteBuffer to, byte b)
public 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 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 toBuffer(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 toBuffer(File file) throws IOException
IOException
public static String toSummaryString(ByteBuffer buffer)
public static String toDetailString(ByteBuffer[] buffer)
public static String toDetailString(ByteBuffer buffer)
public static void putCRLF(ByteBuffer buffer)
public static boolean isPrefix(ByteBuffer prefix, ByteBuffer buffer)
Copyright © 1995-2012 Mort Bay Consulting. All Rights Reserved.