Package org.jboss.netty.buffer
Class ChannelBuffers
- java.lang.Object
-
- org.jboss.netty.buffer.ChannelBuffers
-
public final class ChannelBuffers extends Object
Creates a newChannelBuffer
by allocating new space or by wrapping or copying existing byte arrays, byte buffers and a string.Use static import
This classes is intended to be used with Java 5 static import statement:import static org.jboss.netty.buffer.
ChannelBuffers
.*;ChannelBuffer
heapBuffer = buffer(128);ChannelBuffer
directBuffer = directBuffer(256);ChannelBuffer
dynamicBuffer = dynamicBuffer(512);ChannelBuffer
wrappedBuffer = wrappedBuffer(new byte[128], new byte[256]);ChannelBuffer
copiedBuffe r = copiedBuffer(ByteBuffer
.allocate(128));Allocating a new buffer
Three buffer types are provided out of the box.buffer(int)
allocates a new fixed-capacity heap buffer.directBuffer(int)
allocates a new fixed-capacity direct buffer.dynamicBuffer(int)
allocates a new dynamic-capacity heap buffer, whose capacity increases automatically as needed by a write operation.
Creating a wrapped buffer
Wrapped buffer is a buffer which is a view of one or more existing byte arrays and byte buffers. Any changes in the content of the original array or buffer will be visible in the wrapped buffer. Various wrapper methods are provided and their name is allwrappedBuffer()
. You might want to take a look at the methods that accept varargs closely if you want to create a buffer which is composed of more than one array to reduce the number of memory copy.Creating a copied buffer
Copied buffer is a deep copy of one or more existing byte arrays, byte buffers or a string. Unlike a wrapped buffer, there's no shared data between the original data and the copied buffer. Various copy methods are provided and their name is allcopiedBuffer()
. It is also convenient to use this operation to merge multiple buffers into one buffer.Miscellaneous utility methods
This class also provides various utility methods to help implementation of a new buffer type, generation of hex dump and swapping an integer's byte order.
-
-
Field Summary
Fields Modifier and Type Field Description static ByteOrder
BIG_ENDIAN
Big endian byte order.static ChannelBuffer
EMPTY_BUFFER
A buffer whose capacity is0
.static ByteOrder
LITTLE_ENDIAN
Little endian byte order.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ChannelBuffer
buffer(int capacity)
Creates a new big-endian Java heap buffer with the specifiedcapacity
.static ChannelBuffer
buffer(ByteOrder endianness, int capacity)
Creates a new Java heap buffer with the specifiedendianness
andcapacity
.static int
compare(ChannelBuffer bufferA, ChannelBuffer bufferB)
Compares the two specified buffers as described inChannelBuffer.compareTo(ChannelBuffer)
.static ChannelBuffer
copiedBuffer(byte[] array)
Creates a new big-endian buffer whose content is a copy of the specifiedarray
.static ChannelBuffer
copiedBuffer(byte[]... arrays)
Creates a new big-endian buffer whose content is a merged copy of the specifiedarrays
.static ChannelBuffer
copiedBuffer(byte[] array, int offset, int length)
Creates a new big-endian buffer whose content is a copy of the specifiedarray
's sub-region.static ChannelBuffer
copiedBuffer(char[] array, int offset, int length, Charset charset)
Creates a new big-endian buffer whose content is a subregion of the specifiedarray
encoded in the specifiedcharset
.static ChannelBuffer
copiedBuffer(char[] array, Charset charset)
Creates a new big-endian buffer whose content is the specifiedarray
encoded in the specifiedcharset
.static ChannelBuffer
copiedBuffer(CharSequence string, int offset, int length, Charset charset)
Creates a new big-endian buffer whose content is a subregion of the specifiedstring
encoded in the specifiedcharset
.static ChannelBuffer
copiedBuffer(CharSequence string, Charset charset)
Creates a new big-endian buffer whose content is the specifiedstring
encoded in the specifiedcharset
.static ChannelBuffer
copiedBuffer(ByteBuffer buffer)
Creates a new buffer whose content is a copy of the specifiedbuffer
's current slice.static ChannelBuffer
copiedBuffer(ByteBuffer... buffers)
Creates a new buffer whose content is a merged copy of the specifiedbuffers
' slices.static ChannelBuffer
copiedBuffer(ByteOrder endianness, byte[] array)
Creates a new buffer with the specifiedendianness
whose content is a copy of the specifiedarray
.static ChannelBuffer
copiedBuffer(ByteOrder endianness, byte[]... arrays)
Creates a new buffer with the specifiedendianness
whose content is a merged copy of the specifiedarrays
.static ChannelBuffer
copiedBuffer(ByteOrder endianness, byte[] array, int offset, int length)
Creates a new buffer with the specifiedendianness
whose content is a copy of the specifiedarray
's sub-region.static ChannelBuffer
copiedBuffer(ByteOrder endianness, char[] array, int offset, int length, Charset charset)
Creates a new buffer with the specifiedendianness
whose content is a subregion of the specifiedarray
encoded in the specifiedcharset
.static ChannelBuffer
copiedBuffer(ByteOrder endianness, char[] array, Charset charset)
Creates a new buffer with the specifiedendianness
whose content is the specifiedarray
encoded in the specifiedcharset
.static ChannelBuffer
copiedBuffer(ByteOrder endianness, CharSequence string, int offset, int length, Charset charset)
Creates a new buffer with the specifiedendianness
whose content is a subregion of the specifiedstring
encoded in the specifiedcharset
.static ChannelBuffer
copiedBuffer(ByteOrder endianness, CharSequence string, Charset charset)
Creates a new buffer with the specifiedendianness
whose content is the specifiedstring
encoded in the specifiedcharset
.static ChannelBuffer
copiedBuffer(ChannelBuffer buffer)
Creates a new buffer whose content is a copy of the specifiedbuffer
's readable bytes.static ChannelBuffer
copiedBuffer(ChannelBuffer... buffers)
Creates a new buffer whose content is a merged copy of the specifiedbuffers
' readable bytes.static ChannelBuffer
directBuffer(int capacity)
Creates a new big-endian direct buffer with the specifiedcapacity
.static ChannelBuffer
directBuffer(ByteOrder endianness, int capacity)
Creates a new direct buffer with the specifiedendianness
andcapacity
.static ChannelBuffer
dynamicBuffer()
Creates a new big-endian dynamic buffer whose estimated data length is256
bytes.static ChannelBuffer
dynamicBuffer(int estimatedLength)
Creates a new big-endian dynamic buffer with the specified estimated data length.static ChannelBuffer
dynamicBuffer(int estimatedLength, ChannelBufferFactory factory)
Creates a new big-endian dynamic buffer with the specified estimated data length using the specified factory.static ChannelBuffer
dynamicBuffer(ByteOrder endianness, int estimatedLength)
Creates a new dynamic buffer with the specified endianness and the specified estimated data length.static ChannelBuffer
dynamicBuffer(ByteOrder endianness, int estimatedLength, ChannelBufferFactory factory)
Creates a new dynamic buffer with the specified endianness and the specified estimated data length using the specified factory.static ChannelBuffer
dynamicBuffer(ChannelBufferFactory factory)
static boolean
equals(ChannelBuffer bufferA, ChannelBuffer bufferB)
Returnstrue
if and only if the two specified buffers are identical to each other as described inChannelBuffer#equals(Object)
.static int
hashCode(ChannelBuffer buffer)
Calculates the hash code of the specified buffer.static ChannelBuffer
hexDump(String hexString)
Create aChannelBuffer
from the given hex dumpstatic String
hexDump(ChannelBuffer buffer)
Returns a hex dump of the specified buffer's readable bytes.static String
hexDump(ChannelBuffer buffer, int fromIndex, int length)
Returns a hex dump of the specified buffer's sub-region.static int
indexOf(ChannelBuffer buffer, int fromIndex, int toIndex, byte value)
The default implementation ofChannelBuffer.indexOf(int, int, byte)
.static int
indexOf(ChannelBuffer buffer, int fromIndex, int toIndex, ChannelBufferIndexFinder indexFinder)
The default implementation ofChannelBuffer.indexOf(int, int, ChannelBufferIndexFinder)
.static int
swapInt(int value)
Toggles the endianness of the specified 32-bit integer.static long
swapLong(long value)
Toggles the endianness of the specified 64-bit long integer.static int
swapMedium(int value)
Toggles the endianness of the specified 24-bit medium integer.static short
swapShort(short value)
Toggles the endianness of the specified 16-bit short integer.static ChannelBuffer
unmodifiableBuffer(ChannelBuffer buffer)
Creates a read-only buffer which disallows any modification operations on the specifiedbuffer
.static ChannelBuffer
wrappedBuffer(boolean gathering, ByteBuffer... buffers)
Creates a new composite buffer which wraps the slices of the specified NIO buffers without copying them.static ChannelBuffer
wrappedBuffer(boolean gathering, ChannelBuffer... buffers)
Creates a new composite buffer which wraps the readable bytes of the specified buffers without copying them.static ChannelBuffer
wrappedBuffer(byte[] array)
Creates a new big-endian buffer which wraps the specifiedarray
.static ChannelBuffer
wrappedBuffer(byte[]... arrays)
Creates a new big-endian composite buffer which wraps the specified arrays without copying them.static ChannelBuffer
wrappedBuffer(byte[] array, int offset, int length)
Creates a new big-endian buffer which wraps the sub-region of the specifiedarray
.static ChannelBuffer
wrappedBuffer(ByteBuffer buffer)
Creates a new buffer which wraps the specified NIO buffer's current slice.static ChannelBuffer
wrappedBuffer(ByteBuffer... buffers)
Creates a new composite buffer which wraps the slices of the specified NIO buffers without copying them.static ChannelBuffer
wrappedBuffer(ByteOrder endianness, byte[] array)
Creates a new buffer which wraps the specifiedarray
with the specifiedendianness
.static ChannelBuffer
wrappedBuffer(ByteOrder endianness, byte[]... arrays)
Creates a new composite buffer which wraps the specified arrays without copying them.static ChannelBuffer
wrappedBuffer(ByteOrder endianness, byte[] array, int offset, int length)
Creates a new buffer which wraps the sub-region of the specifiedarray
with the specifiedendianness
.static ChannelBuffer
wrappedBuffer(ChannelBuffer buffer)
Creates a new buffer which wraps the specified buffer's readable bytes.static ChannelBuffer
wrappedBuffer(ChannelBuffer... buffers)
Creates a new composite buffer which wraps the readable bytes of the specified buffers without copying them.
-
-
-
Field Detail
-
BIG_ENDIAN
public static final ByteOrder BIG_ENDIAN
Big endian byte order.
-
LITTLE_ENDIAN
public static final ByteOrder LITTLE_ENDIAN
Little endian byte order.
-
EMPTY_BUFFER
public static final ChannelBuffer EMPTY_BUFFER
A buffer whose capacity is0
.
-
-
Method Detail
-
buffer
public static ChannelBuffer buffer(int capacity)
Creates a new big-endian Java heap buffer with the specifiedcapacity
. The new buffer'sreaderIndex
andwriterIndex
are0
.
-
buffer
public static ChannelBuffer buffer(ByteOrder endianness, int capacity)
Creates a new Java heap buffer with the specifiedendianness
andcapacity
. The new buffer'sreaderIndex
andwriterIndex
are0
.
-
directBuffer
public static ChannelBuffer directBuffer(int capacity)
Creates a new big-endian direct buffer with the specifiedcapacity
. The new buffer'sreaderIndex
andwriterIndex
are0
.
-
directBuffer
public static ChannelBuffer directBuffer(ByteOrder endianness, int capacity)
Creates a new direct buffer with the specifiedendianness
andcapacity
. The new buffer'sreaderIndex
andwriterIndex
are0
.
-
dynamicBuffer
public static ChannelBuffer dynamicBuffer()
Creates a new big-endian dynamic buffer whose estimated data length is256
bytes. The new buffer'sreaderIndex
andwriterIndex
are0
.
-
dynamicBuffer
public static ChannelBuffer dynamicBuffer(ChannelBufferFactory factory)
-
dynamicBuffer
public static ChannelBuffer dynamicBuffer(int estimatedLength)
Creates a new big-endian dynamic buffer with the specified estimated data length. More accurate estimation yields less unexpected reallocation overhead. The new buffer'sreaderIndex
andwriterIndex
are0
.
-
dynamicBuffer
public static ChannelBuffer dynamicBuffer(ByteOrder endianness, int estimatedLength)
Creates a new dynamic buffer with the specified endianness and the specified estimated data length. More accurate estimation yields less unexpected reallocation overhead. The new buffer'sreaderIndex
andwriterIndex
are0
.
-
dynamicBuffer
public static ChannelBuffer dynamicBuffer(int estimatedLength, ChannelBufferFactory factory)
Creates a new big-endian dynamic buffer with the specified estimated data length using the specified factory. More accurate estimation yields less unexpected reallocation overhead. The new buffer'sreaderIndex
andwriterIndex
are0
.
-
dynamicBuffer
public static ChannelBuffer dynamicBuffer(ByteOrder endianness, int estimatedLength, ChannelBufferFactory factory)
Creates a new dynamic buffer with the specified endianness and the specified estimated data length using the specified factory. More accurate estimation yields less unexpected reallocation overhead. The new buffer'sreaderIndex
andwriterIndex
are0
.
-
wrappedBuffer
public static ChannelBuffer wrappedBuffer(byte[] array)
Creates a new big-endian buffer which wraps the specifiedarray
. A modification on the specified array's content will be visible to the returned buffer.
-
wrappedBuffer
public static ChannelBuffer wrappedBuffer(ByteOrder endianness, byte[] array)
Creates a new buffer which wraps the specifiedarray
with the specifiedendianness
. A modification on the specified array's content will be visible to the returned buffer.
-
wrappedBuffer
public static ChannelBuffer wrappedBuffer(byte[] array, int offset, int length)
Creates a new big-endian buffer which wraps the sub-region of the specifiedarray
. A modification on the specified array's content will be visible to the returned buffer.
-
wrappedBuffer
public static ChannelBuffer wrappedBuffer(ByteOrder endianness, byte[] array, int offset, int length)
Creates a new buffer which wraps the sub-region of the specifiedarray
with the specifiedendianness
. A modification on the specified array's content will be visible to the returned buffer.
-
wrappedBuffer
public static ChannelBuffer wrappedBuffer(ByteBuffer buffer)
Creates a new buffer which wraps the specified NIO buffer's current slice. A modification on the specified buffer's content will be visible to the returned buffer.
-
wrappedBuffer
public static ChannelBuffer wrappedBuffer(ChannelBuffer buffer)
Creates a new buffer which wraps the specified buffer's readable bytes. A modification on the specified buffer's content will be visible to the returned buffer.
-
wrappedBuffer
public static ChannelBuffer wrappedBuffer(byte[]... arrays)
Creates a new big-endian composite buffer which wraps the specified arrays without copying them. A modification on the specified arrays' content will be visible to the returned buffer.
-
wrappedBuffer
public static ChannelBuffer wrappedBuffer(ByteOrder endianness, byte[]... arrays)
Creates a new composite buffer which wraps the specified arrays without copying them. A modification on the specified arrays' content will be visible to the returned buffer.- Parameters:
endianness
- the endianness of the new buffer
-
wrappedBuffer
public static ChannelBuffer wrappedBuffer(ChannelBuffer... buffers)
Creates a new composite buffer which wraps the readable bytes of the specified buffers without copying them. A modification on the content of the specified buffers will be visible to the returned buffer.- Throws:
IllegalArgumentException
- if the specified buffers' endianness are different from each other
-
wrappedBuffer
public static ChannelBuffer wrappedBuffer(boolean gathering, ChannelBuffer... buffers)
Creates a new composite buffer which wraps the readable bytes of the specified buffers without copying them. A modification on the content of the specified buffers will be visible to the returned buffer. If gathering istrue
then gathering writes will be used when ever possible.- Throws:
IllegalArgumentException
- if the specified buffers' endianness are different from each other
-
wrappedBuffer
public static ChannelBuffer wrappedBuffer(ByteBuffer... buffers)
Creates a new composite buffer which wraps the slices of the specified NIO buffers without copying them. A modification on the content of the specified buffers will be visible to the returned buffer.- Throws:
IllegalArgumentException
- if the specified buffers' endianness are different from each other
-
wrappedBuffer
public static ChannelBuffer wrappedBuffer(boolean gathering, ByteBuffer... buffers)
Creates a new composite buffer which wraps the slices of the specified NIO buffers without copying them. A modification on the content of the specified buffers will be visible to the returned buffer. If gathering istrue
then gathering writes will be used when ever possible.- Throws:
IllegalArgumentException
- if the specified buffers' endianness are different from each other
-
copiedBuffer
public static ChannelBuffer copiedBuffer(byte[] array)
Creates a new big-endian buffer whose content is a copy of the specifiedarray
. The new buffer'sreaderIndex
andwriterIndex
are0
andarray.length
respectively.
-
copiedBuffer
public static ChannelBuffer copiedBuffer(ByteOrder endianness, byte[] array)
Creates a new buffer with the specifiedendianness
whose content is a copy of the specifiedarray
. The new buffer'sreaderIndex
andwriterIndex
are0
andarray.length
respectively.
-
copiedBuffer
public static ChannelBuffer copiedBuffer(byte[] array, int offset, int length)
Creates a new big-endian buffer whose content is a copy of the specifiedarray
's sub-region. The new buffer'sreaderIndex
andwriterIndex
are0
and the specifiedlength
respectively.
-
copiedBuffer
public static ChannelBuffer copiedBuffer(ByteOrder endianness, byte[] array, int offset, int length)
Creates a new buffer with the specifiedendianness
whose content is a copy of the specifiedarray
's sub-region. The new buffer'sreaderIndex
andwriterIndex
are0
and the specifiedlength
respectively.
-
copiedBuffer
public static ChannelBuffer copiedBuffer(ByteBuffer buffer)
Creates a new buffer whose content is a copy of the specifiedbuffer
's current slice. The new buffer'sreaderIndex
andwriterIndex
are0
andbuffer.remaining
respectively.
-
copiedBuffer
public static ChannelBuffer copiedBuffer(ChannelBuffer buffer)
Creates a new buffer whose content is a copy of the specifiedbuffer
's readable bytes. The new buffer'sreaderIndex
andwriterIndex
are0
andbuffer.readableBytes
respectively.
-
copiedBuffer
public static ChannelBuffer copiedBuffer(byte[]... arrays)
Creates a new big-endian buffer whose content is a merged copy of the specifiedarrays
. The new buffer'sreaderIndex
andwriterIndex
are0
and the sum of all arrays'length
respectively.
-
copiedBuffer
public static ChannelBuffer copiedBuffer(ByteOrder endianness, byte[]... arrays)
Creates a new buffer with the specifiedendianness
whose content is a merged copy of the specifiedarrays
. The new buffer'sreaderIndex
andwriterIndex
are0
and the sum of all arrays'length
respectively.
-
copiedBuffer
public static ChannelBuffer copiedBuffer(ChannelBuffer... buffers)
Creates a new buffer whose content is a merged copy of the specifiedbuffers
' readable bytes. The new buffer'sreaderIndex
andwriterIndex
are0
and the sum of all buffers'readableBytes
respectively.- Throws:
IllegalArgumentException
- if the specified buffers' endianness are different from each other
-
copiedBuffer
public static ChannelBuffer copiedBuffer(ByteBuffer... buffers)
Creates a new buffer whose content is a merged copy of the specifiedbuffers
' slices. The new buffer'sreaderIndex
andwriterIndex
are0
and the sum of all buffers'remaining
respectively.- Throws:
IllegalArgumentException
- if the specified buffers' endianness are different from each other
-
copiedBuffer
public static ChannelBuffer copiedBuffer(CharSequence string, Charset charset)
Creates a new big-endian buffer whose content is the specifiedstring
encoded in the specifiedcharset
. The new buffer'sreaderIndex
andwriterIndex
are0
and the length of the encoded string respectively.
-
copiedBuffer
public static ChannelBuffer copiedBuffer(CharSequence string, int offset, int length, Charset charset)
Creates a new big-endian buffer whose content is a subregion of the specifiedstring
encoded in the specifiedcharset
. The new buffer'sreaderIndex
andwriterIndex
are0
and the length of the encoded string respectively.
-
copiedBuffer
public static ChannelBuffer copiedBuffer(ByteOrder endianness, CharSequence string, Charset charset)
Creates a new buffer with the specifiedendianness
whose content is the specifiedstring
encoded in the specifiedcharset
. The new buffer'sreaderIndex
andwriterIndex
are0
and the length of the encoded string respectively.
-
copiedBuffer
public static ChannelBuffer copiedBuffer(ByteOrder endianness, CharSequence string, int offset, int length, Charset charset)
Creates a new buffer with the specifiedendianness
whose content is a subregion of the specifiedstring
encoded in the specifiedcharset
. The new buffer'sreaderIndex
andwriterIndex
are0
and the length of the encoded string respectively.
-
copiedBuffer
public static ChannelBuffer copiedBuffer(char[] array, Charset charset)
Creates a new big-endian buffer whose content is the specifiedarray
encoded in the specifiedcharset
. The new buffer'sreaderIndex
andwriterIndex
are0
and the length of the encoded string respectively.
-
copiedBuffer
public static ChannelBuffer copiedBuffer(char[] array, int offset, int length, Charset charset)
Creates a new big-endian buffer whose content is a subregion of the specifiedarray
encoded in the specifiedcharset
. The new buffer'sreaderIndex
andwriterIndex
are0
and the length of the encoded string respectively.
-
copiedBuffer
public static ChannelBuffer copiedBuffer(ByteOrder endianness, char[] array, Charset charset)
Creates a new buffer with the specifiedendianness
whose content is the specifiedarray
encoded in the specifiedcharset
. The new buffer'sreaderIndex
andwriterIndex
are0
and the length of the encoded string respectively.
-
copiedBuffer
public static ChannelBuffer copiedBuffer(ByteOrder endianness, char[] array, int offset, int length, Charset charset)
Creates a new buffer with the specifiedendianness
whose content is a subregion of the specifiedarray
encoded in the specifiedcharset
. The new buffer'sreaderIndex
andwriterIndex
are0
and the length of the encoded string respectively.
-
unmodifiableBuffer
public static ChannelBuffer unmodifiableBuffer(ChannelBuffer buffer)
Creates a read-only buffer which disallows any modification operations on the specifiedbuffer
. The new buffer has the samereaderIndex
andwriterIndex
with the specifiedbuffer
.
-
hexDump
public static ChannelBuffer hexDump(String hexString)
Create aChannelBuffer
from the given hex dump
-
hexDump
public static String hexDump(ChannelBuffer buffer)
Returns a hex dump of the specified buffer's readable bytes.
-
hexDump
public static String hexDump(ChannelBuffer buffer, int fromIndex, int length)
Returns a hex dump of the specified buffer's sub-region.
-
hashCode
public static int hashCode(ChannelBuffer buffer)
Calculates the hash code of the specified buffer. This method is useful when implementing a new buffer type.
-
equals
public static boolean equals(ChannelBuffer bufferA, ChannelBuffer bufferB)
Returnstrue
if and only if the two specified buffers are identical to each other as described inChannelBuffer#equals(Object)
. This method is useful when implementing a new buffer type.
-
compare
public static int compare(ChannelBuffer bufferA, ChannelBuffer bufferB)
Compares the two specified buffers as described inChannelBuffer.compareTo(ChannelBuffer)
. This method is useful when implementing a new buffer type.
-
indexOf
public static int indexOf(ChannelBuffer buffer, int fromIndex, int toIndex, byte value)
The default implementation ofChannelBuffer.indexOf(int, int, byte)
. This method is useful when implementing a new buffer type.
-
indexOf
public static int indexOf(ChannelBuffer buffer, int fromIndex, int toIndex, ChannelBufferIndexFinder indexFinder)
The default implementation ofChannelBuffer.indexOf(int, int, ChannelBufferIndexFinder)
. This method is useful when implementing a new buffer type.
-
swapShort
public static short swapShort(short value)
Toggles the endianness of the specified 16-bit short integer.
-
swapMedium
public static int swapMedium(int value)
Toggles the endianness of the specified 24-bit medium integer.
-
swapInt
public static int swapInt(int value)
Toggles the endianness of the specified 32-bit integer.
-
swapLong
public static long swapLong(long value)
Toggles the endianness of the specified 64-bit long integer.
-
-