kipedia.org/wiki/Index_(information_technology)#Array_element_identifier">zero-based indexing. It means the index of the first byte is always {@code 0} and the index ofthe last byte is always {@link #capacity() capacity - 1}. For example, to iterate all bytes of a buffer, you can do the following, regardless of its internal implementation:
ChannelBuffer buffer = ...; for (int i = 0; i < buffer.capacity(); i ++) { byte b = array.getByte(i); System.out.println((char) b); }
Sequential Access Indexing
{@link ChannelBuffer} provides two pointer variables to support sequentialread and write operations - {@link #readerIndex() readerIndex} for a readoperation and {@link #writerIndex() writerIndex} for a write operationrespectively. The following diagram shows how a buffer is segmented into three areas by the two pointers:
+-------------------+------------------+------------------+ | discardable bytes | readable bytes | writable bytes | | | (CONTENT) | | +-------------------+------------------+------------------+ | | | | 0 <= readerIndex <= writerIndex <= capacity
Readable bytes (the actual content)
This segment is where the actual data is stored. Any operation whose name starts with {@code read} or {@code skip} will get or skip the data at thecurrent {@link #readerIndex() readerIndex} and increase it by the number ofread bytes. If the argument of the read operation is also a {@link ChannelBuffer} and no start index is specified, the specifiedbuffer's {@link #readerIndex() readerIndex} is increased together.
If there's not enough content left, {@link IndexOutOfBoundsException} israised. The default value of newly allocated, wrapped or copied buffer's {@link #readerIndex() readerIndex} is {@code 0}.
// Iterates the readable bytes of a buffer. ChannelBuffer buffer = ...; while (buffer.readable()) { System.out.println(buffer.readByte()); }
Writable bytes
This segment is a undefined space which needs to be filled. Any operation whose name ends with {@code write} will write the data at the current{@link #writerIndex() writerIndex} and increase it by the number of writtenbytes. If the argument of the write operation is also a {@link ChannelBuffer}, and no start index is specified, the specified buffer's {@link #readerIndex() readerIndex} is increased together.
If there's not enough writable bytes left, {@link IndexOutOfBoundsException}is raised. The default value of newly allocated buffer's {@link #writerIndex() writerIndex} is {@code 0}. The default value of wrapped or copied buffer's {@link #writerIndex() writerIndex} is the{@link #capacity() capacity} of the buffer.
// Fills the writable bytes of a buffer with random integers. ChannelBuffer buffer = ...; while (buffer.writableBytes() >= 4) { buffer.writeInt(random.nextInt()); }
Discardable bytes
This segment contains the bytes which were read already by a read operation. Initially, the size of this segment is {@code 0}, but its size increases up to the {@link #writerIndex() writerIndex} as read operations are executed.The read bytes can be discarded by calling {@link #discardReadBytes()} toreclaim unused area as depicted by the following diagram:
BEFORE discardReadBytes() +-------------------+------------------+------------------+ | discardable bytes | readable bytes | writable bytes | +-------------------+------------------+------------------+ | | | | 0 <= readerIndex <= writerIndex <= capacity AFTER discardReadBytes() +------------------+--------------------------------------+ | readable bytes | writable bytes (got more space) | +------------------+--------------------------------------+ | | | readerIndex (0) <= writerIndex (decreased) <= capacity
Clearing the buffer indexes
You can set both {@link #readerIndex() readerIndex} and{@link #writerIndex() writerIndex} to {@code 0} by calling {@link #clear()}. It does not clear the buffer content (e.g. filling with {@code 0}) but just clears the two pointers. Please also note that the semantic of this operation is different from {@link ByteBuffer#clear()}.
BEFORE clear() +-------------------+------------------+------------------+ | discardable bytes | readable bytes | writable bytes | +-------------------+------------------+------------------+ | | | | 0 <= readerIndex <= writerIndex <= capacity AFTER clear() +---------------------------------------------------------+ | writable bytes (got more space) | +---------------------------------------------------------+ | | 0 = readerIndex = writerIndex <= capacity
Search operations
Various {@code indexOf()} methods help you locate an index of a value whichmeets a certain criteria. Complicated dynamic sequential search can be done with {@link ChannelBufferIndexFinder} as well as simple static single bytesearch.
Mark and reset
There are two marker indexes in every buffer. One is for storing {@link #readerIndex() readerIndex} and the other is for storing{@link #writerIndex() writerIndex}. You can always reposition one of the two indexes by calling a reset method. It works in a similar fashion to the mark and reset methods in {@link InputStream} except that there's no{@code readlimit}.
Derived buffers
You can create a view of an existing buffer by calling either {@link #duplicate()}, {@link #slice()} or {@link #slice(int,int)}. A derived buffer will have an independent {@link #readerIndex() readerIndex}, {@link #writerIndex() writerIndex} and marker indexes, while it sharesother internal data representation, just like a NIO buffer does.
In case a completely fresh copy of an existing buffer is required, please call {@link #copy()} method instead.
Conversion to existing JDK types
NIO Buffers
Various {@link #toByteBuffer()} and {@link #toByteBuffers()} methods converta {@link ChannelBuffer} into one or more NIO buffers. These methods avoidbuffer allocation and memory copy whenever possible, but there's no guarantee that memory copy will not be involved or that an explicit memory copy will be involved.
Strings
Various {@link #toString(String)} methods convert a {@link ChannelBuffer}into a {@link String}. Plesae note that {@link #toString()} is not aconversion method.
I/O Streams
Please refer to {@link ChannelBufferInputStream} and{@link ChannelBufferOutputStream}.
@author The Netty Project (netty-dev@lists.jboss.org)
@author Trustin Lee (tlee@redhat.com)
@version $Rev: 1017 $, $Date: 2009-03-12 15:40:36 +0900 (Thu, 12 Mar 2009) $
@apiviz.landmark