A buffer is a logically sequential stretch of memory with a certain capacity, an offset for writing, and an offset for reading. Buffers may be composed of multiple components, where each component is a guaranteed contiguous chunk of memory.
allocate family of methods.
A number of standard allocators exist, and are available through static methods on the BufferAllocator
interface.
close() call will deallocate it.
If a buffer is sent elsewhere, the close method on the given instance
will become a no-op.
The buffer can be thought of as a view onto memory, and calling send() on the buffer will effectively close
that view, and recreate it upon reception at its destination.
readX or writeX.getX or setX.0,
and the last byte in the buffer is at offset capacity - 1.
The readerOffset() is the offset into the buffer from which the next read will take place,
and is initially zero.
The reader offset must always be less than or equal to the writerOffset().
The writerOffset() is likewise the offset into the buffer where the next write will take place.
The writer offset is also initially zero, and must be less than or equal to the capacity.
This carves the buffer into three regions, as demonstrated by this diagram:
+-------------------+------------------+------------------+
| discardable bytes | readable bytes | writable bytes |
| | (CONTENT) | |
+-------------------+------------------+------------------+
| | | |
0 <= readerOffset <= writerOffset <= capacity
split() method breaks a buffer into two.
The two buffers will share the underlying memory, but their regions will not overlap, ensuring that the memory is
safely shared between the two.
Splitting a buffer is useful for when you want to hand over a region of a buffer to some other, perhaps unknown, piece of code, and relinquish your ownership of that buffer region in the process. Examples include aggregating messages into an accumulator buffer, and sending messages down the pipeline for further processing, as split buffer regions, once their data has been received in its entirety. If you instead wish to temporarily share a region of a buffer, you will have to pass offset and length along with the buffer, or you will have to make a copy of the region.
BufferAllocator has a BufferAllocator#constBufferSupplier(byte[]) method that solves this, and
prevents these bugs from occurring.
| Modifier and Type | Method and Description |
|---|---|
| public int | Returns: The offset, relative to the currentreaderOffset(), of the found value, or -1 if none
was found.The byte value to search for. needle)Get the number of readable bytes, until the given |
| public int | Returns: The offset, relative to the currentreaderOffset(), of the found value, or -1 if none
was found.The buffer value to search for. needle)Get the number of readable bytes, until the given |
| public int | Returns: The capacity in bytes.The capacity of this buffer, that is, the maximum number of bytes it can contain. |
| public Buffer | Returns: This buffer instance.Discards the read bytes, and moves the buffer contents to the beginning of the buffer. |
| public default Buffer | Returns: A new buffer instance, with independentreaderOffset() and writerOffset(),
that contains a copy of the readable region of this buffer.Returns a copy of this buffer's readable bytes. |
| public default Buffer | Returns: A new buffer instance, with independentreaderOffset() and writerOffset(),
that contains a copy of the given region of this buffer.The offset where copying should start from. This is the offset of the first byte copied. offset, int The number of bytes to copy, and the capacity of the returned buffer. length)Returns a copy of the given region of this buffer. |
| public default Buffer | Returns: A new buffer instance, with independentreaderOffset() and writerOffset(),
that contains a copy of the given region of this buffer.The desired readOnly)readOnly() state of the returned buffer.Returns a copy of this buffer's readable bytes, with the given read-only setting. |
| public Buffer | Returns: A new buffer instance, with independentreaderOffset() and writerOffset(),
that contains a copy of the given region of this buffer.The offset where copying should start from. This is the offset of the first byte copied. offset, int The number of bytes to copy, and the capacity of the returned buffer. length, boolean The desired readOnly)readOnly() state of the returned buffer.Returns a copy of the given region of this buffer. |
| public void | copyInto(int
The byte offset into this buffer from where the copying should start; the byte at this offset in
this buffer will be copied to the srcPos, byte[] destPos index in the dest array.The destination byte array. dest, int The index into the destPos, int dest array from where the copying should start.The number of bytes to copy. length)Copies the given length of data from this buffer into the given destination array, beginning at the given source position in this buffer, and the given destination position in the destination array. |
| public void | copyInto(int
The byte offset into this buffer from where the copying should start; the byte at this offset in
this buffer will be copied to the srcPos, ByteBuffer destPos index in the dest ByteBuffer.The destination byte buffer. dest, int The index into the destPos, int dest ByteBuffer from where the copying should start.The number of bytes to copy. length)Copies the given length of data from this buffer into the given destination byte buffer, beginning at the given source position in this buffer, and the given destination position in the destination byte buffer. |
| public void | copyInto(int
The byte offset into this buffer from where the copying should start; the byte at this offset in
this buffer will be copied to the srcPos, Buffer destPos index in the dest buffer.The destination buffer. dest, int The index into the destPos, int dest buffer from where the copying should start.The number of bytes to copy. length)Copies the given length of data from this buffer into the given destination buffer, beginning at the given source position in this buffer, and the given destination position in the destination buffer. |
| public int | Returns: The number of components in this buffer.Get the number of components in this buffer. |
| public int | Returns: The number of readable components in this buffer.Get the number of "components" in this buffer, that are readable. |
| public int | Returns: The number of writable components in this buffer.Get the number of "components" in this buffer, that are writable. |
| public default Buffer | Returns: This buffer instance.The requested number of bytes of space that should be available for writing. size)Ensures that this buffer has at least the given number of bytes of available space for writing. |
| public Buffer | Returns: This buffer instance.The requested number of bytes of space that should be available for writing. size, int The minimum number of bytes to grow by. If it is determined that memory should be allocated
and copied, make sure that the new memory allocation is bigger than the old one by at least
this many bytes. This way, the buffer can grow by more than what is immediately necessary,
thus amortising the costs of allocating and copying. minimumGrowth, boolean allowCompaction)Ensures that this buffer has at least the given number of bytes of available space for writing. |
| public Buffer | Returns: This Buffer.The byte value to write at every offset in the buffer. value)Fills the buffer with the given byte value. |
| public < An intersection type that presents both the T extends BufferComponent & ComponentIterator.BufferComponent interface,
and the ability to progress the iteration via the Next#next() method. | Returns: A component iterator of readable components.Create a component iterator for all components in this buffer. |
| public Buffer | Returns: This buffer instance.The maximum size this buffers capacity will implicitly grow to via limit)write* methods.Set an upper limit to the implicit capacity growth. |
| public int | |
| public boolean | Returns: true if this buffer is backed by native, off-heap, memory. Otherwise, false, if this
buffer is backed by on-heap memory.Queries if this buffer is backed by native memory, or not. |
| public Buffer | |
| public ByteCursor | Returns: AByteCursor for iterating the readable bytes of this buffer.Opens a cursor to iterate the readable bytes of this buffer. |
| public ByteCursor | Returns: AByteCursor for the given stretch of bytes of this buffer.The offset into the buffer where iteration should start.
The first byte read from the iterator will be the byte at this offset. fromOffset, int The number of bytes to iterate. length)Opens a cursor to iterate the given number bytes of this buffer, starting at the given offset. |
| public default ByteCursor | Returns: AByteCursor for the readable bytes of this buffer.Opens a cursor to iterate the readable bytes of this buffer, in reverse. |
| public ByteCursor | Returns: AByteCursor for the given stretch of bytes of this buffer.The offset into the buffer where iteration should start.
The first byte read from the iterator will be the byte at this offset. fromOffset, int The number of bytes to iterate. length)Opens a cursor to iterate the given number bytes of this buffer, in reverse, starting at the given offset. |
| public default int | readableBytes()
Returns the number of readable bytes which is equal to |
| public default Buffer | Returns: This buffer.The destination)ByteBuffer to write into.Read from this buffer, into the destination |
| public default Buffer | Returns: This buffer.The byte array to write into. destination, int Position in the destPos, int destination to where bytes should be written from this buffer.The number of bytes to copy. length)Read from this buffer, into the destination array, the given number of bytes. |
| public default CharSequence | Returns: CharSequence read from this buffer.of length, Charset CharSequence to read.of the bytes to be read. charset)Reads a |
| public int | |
| public Buffer | |
| public boolean | Returns: true if this buffer is read-only, false otherwise.Queries if this buffer is read-only or not. |
| public default Buffer | Returns: A new buffer with independent and exclusive ownership over the previously read and readable bytes from this buffer.Splits the buffer into two, at |
| public default Buffer | Returns: This buffer instance.Resets the read offset and the write offset on this buffer to zero, and return this buffer. |
| public default Buffer | Returns: This buffer instance.to accumulate. delta)Move the reader offset forward by the given delta. |
| public default Buffer | Returns: This buffer instance.to accumulate. delta)Move the writer offset to ahead by the given delta. |
| public default Buffer | Returns: A new buffer with independent and exclusive ownership over the previously read and readable bytes from this buffer.Splits the buffer into two, at the write offset position. |
| public Buffer | Returns: A new buffer with independent and exclusive ownership over the bytes from the beginning to the given offset of this buffer.The offset into this buffer where it should be split. After the split, the data at this offset
will be at offset zero in this buffer. splitOffset)Splits the buffer into two, at the given |
| public default String | |
| public int | Returns: The actual number of bytes read, possibly zero, or -1 if the end-of-stream has been reached.The channel to read from. channel, long The file position. position, int The maximum number of bytes to read. length)Read from the given channel starting from the given position and write to this buffer. |
| public int | Returns: The actual number of bytes read, possibly zero, or -1 if the end-of-stream has been reached.The channel to read from. channel, int The maximum number of bytes to read. length)Read from the given channel and write to this buffer. |
| public int | Returns: The actual number of bytes written, possibly zero.The channel to write to. channel, int The maximum number of bytes to write. length)Read from this buffer and write to the given channel. |
| public default int | writableBytes()
Returns the number of writable bytes which is equal to |
| public default Buffer | Returns: This buffer.The buffer to read from. source)Writes into this buffer, all the readable bytes from the given buffer. |
| public default Buffer | Returns: This buffer.The byte array to read from. source)Writes into this buffer, all the bytes from the given byte array. |
| public default Buffer | Returns: This buffer.The byte array to read from. source, int Position in the srcPos, int source from where bytes should be written to this buffer.The number of bytes to copy. length)Writes into this buffer, the given number of bytes from the byte array. |
| public default Buffer | Returns: This buffer.The source)ByteBuffer to read from.Writes into this buffer from the source |
| public default Buffer | Returns: This buffer.CharSequence to read from.Charset to use for writing.Writes into this buffer, all the bytes from the given |
| public int | |
| public Buffer | |
| public default Buffer | Returns: A new buffer with independent and exclusive ownership over the previously read and readable bytes from this buffer.Splits the buffer into two, at |