Class ChannelBuffers
java.lang.Object
org.jboss.netty.buffer.ChannelBuffers
Creates a new
ChannelBuffer 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.*;ChannelBufferheapBuffer = buffer(128);ChannelBufferdirectBuffer = directBuffer(256);ChannelBufferdynamicBuffer = dynamicBuffer(512);ChannelBufferwrappedBuffer = wrappedBuffer(new byte[128], new byte[256]);ChannelBuffercopiedBuffe 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
FieldsModifier and TypeFieldDescriptionstatic final ByteOrderBig endian byte order.static final ChannelBufferA buffer whose capacity is0.private static final char[]static final ByteOrderLittle endian byte order. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ChannelBufferbuffer(int capacity) Creates a new big-endian Java heap buffer with the specifiedcapacity.static ChannelBufferCreates a new Java heap buffer with the specifiedendiannessandcapacity.static intcompare(ChannelBuffer bufferA, ChannelBuffer bufferB) Compares the two specified buffers as described inChannelBuffer.compareTo(ChannelBuffer).private static ChannelBuffercompositeBuffer(ByteOrder endianness, List<ChannelBuffer> components, boolean gathering) static ChannelBuffercopiedBuffer(byte[] array) Creates a new big-endian buffer whose content is a copy of the specifiedarray.static ChannelBuffercopiedBuffer(byte[]... arrays) Creates a new big-endian buffer whose content is a merged copy of the specifiedarrays.static ChannelBuffercopiedBuffer(byte[] array, int offset, int length) Creates a new big-endian buffer whose content is a copy of the specifiedarray's sub-region.static ChannelBuffercopiedBuffer(char[] array, int offset, int length, Charset charset) Creates a new big-endian buffer whose content is a subregion of the specifiedarrayencoded in the specifiedcharset.static ChannelBuffercopiedBuffer(char[] array, Charset charset) Creates a new big-endian buffer whose content is the specifiedarrayencoded in the specifiedcharset.static ChannelBuffercopiedBuffer(CharSequence string, int offset, int length, Charset charset) Creates a new big-endian buffer whose content is a subregion of the specifiedstringencoded in the specifiedcharset.static ChannelBuffercopiedBuffer(CharSequence string, Charset charset) Creates a new big-endian buffer whose content is the specifiedstringencoded in the specifiedcharset.static ChannelBuffercopiedBuffer(ByteBuffer buffer) Creates a new buffer whose content is a copy of the specifiedbuffer's current slice.static ChannelBuffercopiedBuffer(ByteBuffer... buffers) Creates a new buffer whose content is a merged copy of the specifiedbuffers' slices.static ChannelBuffercopiedBuffer(ByteOrder endianness, byte[] array) Creates a new buffer with the specifiedendiannesswhose content is a copy of the specifiedarray.static ChannelBuffercopiedBuffer(ByteOrder endianness, byte[]... arrays) Creates a new buffer with the specifiedendiannesswhose content is a merged copy of the specifiedarrays.static ChannelBuffercopiedBuffer(ByteOrder endianness, byte[] array, int offset, int length) Creates a new buffer with the specifiedendiannesswhose content is a copy of the specifiedarray's sub-region.static ChannelBuffercopiedBuffer(ByteOrder endianness, char[] array, int offset, int length, Charset charset) Creates a new buffer with the specifiedendiannesswhose content is a subregion of the specifiedarrayencoded in the specifiedcharset.static ChannelBuffercopiedBuffer(ByteOrder endianness, char[] array, Charset charset) Creates a new buffer with the specifiedendiannesswhose content is the specifiedarrayencoded in the specifiedcharset.static ChannelBuffercopiedBuffer(ByteOrder endianness, CharSequence string, int offset, int length, Charset charset) Creates a new buffer with the specifiedendiannesswhose content is a subregion of the specifiedstringencoded in the specifiedcharset.static ChannelBuffercopiedBuffer(ByteOrder endianness, CharSequence string, Charset charset) Creates a new buffer with the specifiedendiannesswhose content is the specifiedstringencoded in the specifiedcharset.private static ChannelBuffercopiedBuffer(ByteOrder endianness, CharBuffer buffer, Charset charset) static ChannelBuffercopiedBuffer(ChannelBuffer buffer) Creates a new buffer whose content is a copy of the specifiedbuffer's readable bytes.static ChannelBuffercopiedBuffer(ChannelBuffer... buffers) Creates a new buffer whose content is a merged copy of the specifiedbuffers' readable bytes.(package private) static StringdecodeString(ByteBuffer src, Charset charset) static ChannelBufferdirectBuffer(int capacity) Creates a new big-endian direct buffer with the specifiedcapacity.static ChannelBufferdirectBuffer(ByteOrder endianness, int capacity) Creates a new direct buffer with the specifiedendiannessandcapacity.static ChannelBufferCreates a new big-endian dynamic buffer whose estimated data length is256bytes.static ChannelBufferdynamicBuffer(int estimatedLength) Creates a new big-endian dynamic buffer with the specified estimated data length.static ChannelBufferdynamicBuffer(int estimatedLength, ChannelBufferFactory factory) Creates a new big-endian dynamic buffer with the specified estimated data length using the specified factory.static ChannelBufferdynamicBuffer(ByteOrder endianness, int estimatedLength) Creates a new dynamic buffer with the specified endianness and the specified estimated data length.static ChannelBufferdynamicBuffer(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 ChannelBufferdynamicBuffer(ChannelBufferFactory factory) (package private) static ByteBufferencodeString(CharBuffer src, Charset charset) static booleanequals(ChannelBuffer bufferA, ChannelBuffer bufferB) Returnstrueif and only if the two specified buffers are identical to each other as described inChannelBuffer#equals(Object).private static intfirstIndexOf(ChannelBuffer buffer, int fromIndex, int toIndex, byte value) private static intfirstIndexOf(ChannelBuffer buffer, int fromIndex, int toIndex, ChannelBufferIndexFinder indexFinder) static inthashCode(ChannelBuffer buffer) Calculates the hash code of the specified buffer.static ChannelBufferCreate aChannelBufferfrom the given hex dumpstatic StringhexDump(ChannelBuffer buffer) Returns a hex dump of the specified buffer's readable bytes.static StringhexDump(ChannelBuffer buffer, int fromIndex, int length) Returns a hex dump of the specified buffer's sub-region.static intindexOf(ChannelBuffer buffer, int fromIndex, int toIndex, byte value) The default implementation ofChannelBuffer.indexOf(int, int, byte).static intindexOf(ChannelBuffer buffer, int fromIndex, int toIndex, ChannelBufferIndexFinder indexFinder) The default implementation ofChannelBuffer.indexOf(int, int, ChannelBufferIndexFinder).private static intlastIndexOf(ChannelBuffer buffer, int fromIndex, int toIndex, byte value) private static intlastIndexOf(ChannelBuffer buffer, int fromIndex, int toIndex, ChannelBufferIndexFinder indexFinder) static intswapInt(int value) Toggles the endianness of the specified 32-bit integer.static longswapLong(long value) Toggles the endianness of the specified 64-bit long integer.static intswapMedium(int value) Toggles the endianness of the specified 24-bit medium integer.static shortswapShort(short value) Toggles the endianness of the specified 16-bit short integer.static ChannelBufferunmodifiableBuffer(ChannelBuffer buffer) Creates a read-only buffer which disallows any modification operations on the specifiedbuffer.static ChannelBufferwrappedBuffer(boolean gathering, ByteBuffer... buffers) Creates a new composite buffer which wraps the slices of the specified NIO buffers without copying them.static ChannelBufferwrappedBuffer(boolean gathering, ChannelBuffer... buffers) Creates a new composite buffer which wraps the readable bytes of the specified buffers without copying them.static ChannelBufferwrappedBuffer(byte[] array) Creates a new big-endian buffer which wraps the specifiedarray.static ChannelBufferwrappedBuffer(byte[]... arrays) Creates a new big-endian composite buffer which wraps the specified arrays without copying them.static ChannelBufferwrappedBuffer(byte[] array, int offset, int length) Creates a new big-endian buffer which wraps the sub-region of the specifiedarray.static ChannelBufferwrappedBuffer(ByteBuffer buffer) Creates a new buffer which wraps the specified NIO buffer's current slice.static ChannelBufferwrappedBuffer(ByteBuffer... buffers) Creates a new composite buffer which wraps the slices of the specified NIO buffers without copying them.static ChannelBufferwrappedBuffer(ByteOrder endianness, byte[] array) Creates a new buffer which wraps the specifiedarraywith the specifiedendianness.static ChannelBufferwrappedBuffer(ByteOrder endianness, byte[]... arrays) Creates a new composite buffer which wraps the specified arrays without copying them.static ChannelBufferwrappedBuffer(ByteOrder endianness, byte[] array, int offset, int length) Creates a new buffer which wraps the sub-region of the specifiedarraywith the specifiedendianness.static ChannelBufferwrappedBuffer(ChannelBuffer buffer) Creates a new buffer which wraps the specified buffer's readable bytes.static ChannelBufferwrappedBuffer(ChannelBuffer... buffers) Creates a new composite buffer which wraps the readable bytes of the specified buffers without copying them.
-
Field Details
-
BIG_ENDIAN
Big endian byte order. -
LITTLE_ENDIAN
Little endian byte order. -
EMPTY_BUFFER
A buffer whose capacity is0. -
HEXDUMP_TABLE
private static final char[] HEXDUMP_TABLE
-
-
Constructor Details
-
ChannelBuffers
private ChannelBuffers()
-
-
Method Details
-
buffer
Creates a new big-endian Java heap buffer with the specifiedcapacity. The new buffer'sreaderIndexandwriterIndexare0. -
buffer
Creates a new Java heap buffer with the specifiedendiannessandcapacity. The new buffer'sreaderIndexandwriterIndexare0. -
directBuffer
Creates a new big-endian direct buffer with the specifiedcapacity. The new buffer'sreaderIndexandwriterIndexare0. -
directBuffer
Creates a new direct buffer with the specifiedendiannessandcapacity. The new buffer'sreaderIndexandwriterIndexare0. -
dynamicBuffer
Creates a new big-endian dynamic buffer whose estimated data length is256bytes. The new buffer'sreaderIndexandwriterIndexare0. -
dynamicBuffer
-
dynamicBuffer
Creates a new big-endian dynamic buffer with the specified estimated data length. More accurate estimation yields less unexpected reallocation overhead. The new buffer'sreaderIndexandwriterIndexare0. -
dynamicBuffer
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'sreaderIndexandwriterIndexare0. -
dynamicBuffer
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'sreaderIndexandwriterIndexare0. -
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'sreaderIndexandwriterIndexare0. -
wrappedBuffer
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
Creates a new buffer which wraps the specifiedarraywith the specifiedendianness. A modification on the specified array's content will be visible to the returned buffer. -
wrappedBuffer
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 specifiedarraywith the specifiedendianness. A modification on the specified array's content will be visible to the returned buffer. -
wrappedBuffer
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
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
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
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
-
compositeBuffer
private static ChannelBuffer compositeBuffer(ByteOrder endianness, List<ChannelBuffer> components, boolean gathering) -
wrappedBuffer
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
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 istruethen gathering writes will be used when ever possible.- Throws:
IllegalArgumentException- if the specified buffers' endianness are different from each other
-
wrappedBuffer
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
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 istruethen gathering writes will be used when ever possible.- Throws:
IllegalArgumentException- if the specified buffers' endianness are different from each other
-
copiedBuffer
Creates a new big-endian buffer whose content is a copy of the specifiedarray. The new buffer'sreaderIndexandwriterIndexare0andarray.lengthrespectively. -
copiedBuffer
Creates a new buffer with the specifiedendiannesswhose content is a copy of the specifiedarray. The new buffer'sreaderIndexandwriterIndexare0andarray.lengthrespectively. -
copiedBuffer
Creates a new big-endian buffer whose content is a copy of the specifiedarray's sub-region. The new buffer'sreaderIndexandwriterIndexare0and the specifiedlengthrespectively. -
copiedBuffer
public static ChannelBuffer copiedBuffer(ByteOrder endianness, byte[] array, int offset, int length) Creates a new buffer with the specifiedendiannesswhose content is a copy of the specifiedarray's sub-region. The new buffer'sreaderIndexandwriterIndexare0and the specifiedlengthrespectively. -
copiedBuffer
Creates a new buffer whose content is a copy of the specifiedbuffer's current slice. The new buffer'sreaderIndexandwriterIndexare0andbuffer.remainingrespectively. -
copiedBuffer
Creates a new buffer whose content is a copy of the specifiedbuffer's readable bytes. The new buffer'sreaderIndexandwriterIndexare0andbuffer.readableBytesrespectively. -
copiedBuffer
Creates a new big-endian buffer whose content is a merged copy of the specifiedarrays. The new buffer'sreaderIndexandwriterIndexare0and the sum of all arrays'lengthrespectively. -
copiedBuffer
Creates a new buffer with the specifiedendiannesswhose content is a merged copy of the specifiedarrays. The new buffer'sreaderIndexandwriterIndexare0and the sum of all arrays'lengthrespectively. -
copiedBuffer
Creates a new buffer whose content is a merged copy of the specifiedbuffers' readable bytes. The new buffer'sreaderIndexandwriterIndexare0and the sum of all buffers'readableBytesrespectively.- Throws:
IllegalArgumentException- if the specified buffers' endianness are different from each other
-
copiedBuffer
Creates a new buffer whose content is a merged copy of the specifiedbuffers' slices. The new buffer'sreaderIndexandwriterIndexare0and the sum of all buffers'remainingrespectively.- Throws:
IllegalArgumentException- if the specified buffers' endianness are different from each other
-
copiedBuffer
Creates a new big-endian buffer whose content is the specifiedstringencoded in the specifiedcharset. The new buffer'sreaderIndexandwriterIndexare0and 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 specifiedstringencoded in the specifiedcharset. The new buffer'sreaderIndexandwriterIndexare0and the length of the encoded string respectively. -
copiedBuffer
public static ChannelBuffer copiedBuffer(ByteOrder endianness, CharSequence string, Charset charset) Creates a new buffer with the specifiedendiannesswhose content is the specifiedstringencoded in the specifiedcharset. The new buffer'sreaderIndexandwriterIndexare0and 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 specifiedendiannesswhose content is a subregion of the specifiedstringencoded in the specifiedcharset. The new buffer'sreaderIndexandwriterIndexare0and the length of the encoded string respectively. -
copiedBuffer
Creates a new big-endian buffer whose content is the specifiedarrayencoded in the specifiedcharset. The new buffer'sreaderIndexandwriterIndexare0and the length of the encoded string respectively. -
copiedBuffer
Creates a new big-endian buffer whose content is a subregion of the specifiedarrayencoded in the specifiedcharset. The new buffer'sreaderIndexandwriterIndexare0and the length of the encoded string respectively. -
copiedBuffer
Creates a new buffer with the specifiedendiannesswhose content is the specifiedarrayencoded in the specifiedcharset. The new buffer'sreaderIndexandwriterIndexare0and 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 specifiedendiannesswhose content is a subregion of the specifiedarrayencoded in the specifiedcharset. The new buffer'sreaderIndexandwriterIndexare0and the length of the encoded string respectively. -
copiedBuffer
-
unmodifiableBuffer
Creates a read-only buffer which disallows any modification operations on the specifiedbuffer. The new buffer has the samereaderIndexandwriterIndexwith the specifiedbuffer. -
hexDump
Create aChannelBufferfrom the given hex dump -
hexDump
Returns a hex dump of the specified buffer's readable bytes. -
hexDump
Returns a hex dump of the specified buffer's sub-region. -
hashCode
Calculates the hash code of the specified buffer. This method is useful when implementing a new buffer type. -
equals
Returnstrueif 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
Compares the two specified buffers as described inChannelBuffer.compareTo(ChannelBuffer). This method is useful when implementing a new buffer type. -
indexOf
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. -
firstIndexOf
-
lastIndexOf
-
firstIndexOf
private static int firstIndexOf(ChannelBuffer buffer, int fromIndex, int toIndex, ChannelBufferIndexFinder indexFinder) -
lastIndexOf
private static int lastIndexOf(ChannelBuffer buffer, int fromIndex, int toIndex, ChannelBufferIndexFinder indexFinder) -
encodeString
-
decodeString
-