Top Description Methods
io.netty5.buffer

public Interface Buffer

extends Resource<Buffer>, BufferAccessor
Known Direct Subinterfaces
io.netty5.buffer.CompositeBuffer
Known Direct Implementers
io.netty5.buffer.BufferStub, io.netty5.buffer.internal.AdaptableBuffer
Imports
io.netty5.buffer.ComponentIterator.Next, io.netty5.buffer.internal.InternalBufferUtils, io.netty5.util.Resource, java.io.IOException, java.nio.ByteBuffer, .ByteOrder, java.nio.channels.FileChannel, .GatheringByteChannel, .ReadableByteChannel, .ScatteringByteChannel, .WritableByteChannel, java.nio.charset.Charset

A life cycled buffer of memory, with separate reader and writer offsets.

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.

Creating a buffer

Buffers are created by allocators, and their allocate family of methods. A number of standard allocators exist, and are available through static methods on the BufferAllocator interface.

Buffer life cycle

The buffer has a life cycle, where it is allocated, used, and deallocated. When the buffer is initially allocated, a pairing 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.

Thread-safety

Buffers are not thread-safe.

Accessing data

Data access methods fall into two classes:
  1. Access that are based on, and updates, the read or write offset positions.
  2. Access that take offsets as arguments, and do not update read or write offset positions.
A buffer contains two mutable offset positions: one for reading and one for writing. These positions use zero-based indexing, such that the first byte of data in the buffer is placed at offset 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

Byte Order

Buffers are always big endian, and this cannot be changed. Usages that need to get, set, read, or write, little-endian values will have to flip the byte order of the values they read and write.

Splitting buffers

The 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.

Buffers as constants

Sometimes, the same bit of data will be processed or transmitted over and over again. In such cases, it can be tempting to allocate and fill a buffer once, and then reuse it. Such reuse must be done carefully, however, to avoid a number of bugs. The BufferAllocator has a BufferAllocator#constBufferSupplier(byte[]) method that solves this, and prevents these bugs from occurring.

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

The offset, relative to the current readerOffset(), of the found value, or -1 if none was found.
bytesBefore
(byte
The byte value to search for.
needle
)

Get the number of readable bytes, until the given needle is found in this buffer.

public int

Returns:

The offset, relative to the current readerOffset(), of the found value, or -1 if none was found.
bytesBefore
(Buffer
The buffer value to search for.
needle
)

Get the number of readable bytes, until the given needle is found in this buffer.

public int

Returns:

The capacity in bytes.
capacity
()

The capacity of this buffer, that is, the maximum number of bytes it can contain.

public Buffer

Returns:

This buffer instance.
compact
()

Discards the read bytes, and moves the buffer contents to the beginning of the buffer.

public default Buffer

Returns:

A new buffer instance, with independent readerOffset() and writerOffset(), that contains a copy of the readable region of this buffer.
copy
()

Returns a copy of this buffer's readable bytes.

public default Buffer

Returns:

A new buffer instance, with independent readerOffset() and writerOffset(), that contains a copy of the given region of this buffer.
copy
(int
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 independent readerOffset() and writerOffset(), that contains a copy of the given region of this buffer.
copy
(boolean
The desired readOnly() state of the returned buffer.
readOnly
)

Returns a copy of this buffer's readable bytes, with the given read-only setting.

public Buffer

Returns:

A new buffer instance, with independent readerOffset() and writerOffset(), that contains a copy of the given region of this buffer.
copy
(int
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() state of the returned buffer.
readOnly
)

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 destPos index in the dest array.
srcPos
,
byte[]
The destination byte array.
dest
,
int
The index into the dest array from where the copying should start.
destPos
,
int
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 destPos index in the dest ByteBuffer.
srcPos
,
ByteBuffer
The destination byte buffer.
dest
,
int
The index into the dest ByteBuffer from where the copying should start.
destPos
,
int
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 destPos index in the dest buffer.
srcPos
,
Buffer
The destination buffer.
dest
,
int
The index into the dest buffer from where the copying should start.
destPos
,
int
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.
countComponents
()

Get the number of components in this buffer.

public int

Returns:

The number of readable components in this buffer.
countReadableComponents
()

Get the number of "components" in this buffer, that are readable.

public int

Returns:

The number of writable components in this buffer.
countWritableComponents
()

Get the number of "components" in this buffer, that are writable.

public default Buffer

Returns:

This buffer instance.
ensureWritable
(int
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.
ensureWritable
(int
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
true if the method is allowed to modify the reader offset and writer offset, otherwise false.
allowCompaction
)

Ensures that this buffer has at least the given number of bytes of available space for writing.

public Buffer

Returns:

This Buffer.
fill
(byte
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 BufferComponent interface, and the ability to progress the iteration via the Next#next() method.
T extends BufferComponent & ComponentIterator.Next
>
ComponentIterator<T>

Returns:

A component iterator of readable components.
forEachComponent
()

Create a component iterator for all components in this buffer.

public Buffer

Returns:

This buffer instance.
implicitCapacityLimit
(int
The maximum size this buffers capacity will implicitly grow to via write* methods.
limit
)

Set an upper limit to the implicit capacity growth.

public int

Returns:

the limit.
implicitCapacityLimit
()

Returns the implicit capacity limit of the buffer.

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.
isDirect
()

Queries if this buffer is backed by native memory, or not.

public Buffer

Returns:

This buffer instance.
makeReadOnly
()

Makes this buffer read-only.

public ByteCursor

Returns:

A ByteCursor for iterating the readable bytes of this buffer.
openCursor
()

Opens a cursor to iterate the readable bytes of this buffer.

public ByteCursor

Returns:

A ByteCursor for the given stretch of bytes of this buffer.
openCursor
(int
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:

A ByteCursor for the readable bytes of this buffer.
openReverseCursor
()

Opens a cursor to iterate the readable bytes of this buffer, in reverse.

public ByteCursor

Returns:

A ByteCursor for the given stretch of bytes of this buffer.
openReverseCursor
(int
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 (writerOffset() - readerOffset()).

public default Buffer

Returns:

This buffer.
readBytes
(ByteBuffer
The ByteBuffer to write into.
destination
)

Read from this buffer, into the destination ByteBuffer This updates the read offset of this buffer and also the position of the destination ByteBuffer.

public default Buffer

Returns:

This buffer.
readBytes
(byte[]
The byte array to write into.
destination
,
int
Position in the destination to where bytes should be written from this buffer.
destPos
,
int
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.
readCharSequence
(int
of CharSequence to read.
length
,
Charset
of the bytes to be read.
charset
)

Reads a CharSequence of the passed length using the passed Charset.

public int

Returns:

The current reader offset.
readerOffset
()

Get the current reader offset.

public Buffer

Returns:

This Buffer.
readerOffset
(int
The reader offset to set.
offset
)

Set the reader offset.

public boolean

Returns:

true if this buffer is read-only, false otherwise.
readOnly
()

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.
readSplit
(int length)

Splits the buffer into two, at length number of bytes from the current reader offset position.

public default Buffer

Returns:

This buffer instance.
resetOffsets
()

Resets the read offset and the write offset on this buffer to zero, and return this buffer.

public default Buffer

Returns:

This buffer instance.
skipReadableBytes
(int
to accumulate.
delta
)

Move the reader offset forward by the given delta.

public default Buffer

Returns:

This buffer instance.
skipWritableBytes
(int
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.
split
()

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.
split
(int
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 splitOffset.

public default String

Returns:

Buffer's readable bytes as a string.
toString
(Charset
used for decoding.
charset
)

Decodes this buffer's readable bytes into a string with the specified Charset.

public int

Returns:

The actual number of bytes read, possibly zero, or -1 if the end-of-stream has been reached.
transferFrom
(FileChannel
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.
transferFrom
(ReadableByteChannel
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.
transferTo
(WritableByteChannel
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 (capacity() - writerOffset()).

public default Buffer

Returns:

This buffer.
writeBytes
(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.
writeBytes
(byte[]
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.
writeBytes
(byte[]
The byte array to read from.
source
,
int
Position in the source from where bytes should be written to this buffer.
srcPos
,
int
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.
writeBytes
(ByteBuffer
The ByteBuffer to read from.
source
)

Writes into this buffer from the source ByteBuffer.

public default Buffer

Returns:

This buffer.
writeCharSequence
(CharSequence
CharSequence to read from.
source
,
Charset
Charset to use for writing.
charset
)

Writes into this buffer, all the bytes from the given source using the passed charset.

public int

Returns:

The current writer offset.
writerOffset
()

Get the current writer offset.

public Buffer

Returns:

This Buffer.
writerOffset
(int
The writer offset to set.
offset
)

Set the writer offset.

public default Buffer

Returns:

A new buffer with independent and exclusive ownership over the previously read and readable bytes from this buffer.
writeSplit
(int length)

Splits the buffer into two, at length number of bytes from the current writer offset position.

Inherited from io.netty5.buffer.BufferAccessor:
getBooleangetBytegetChargetDoublegetFloatgetIntgetLonggetMediumgetShortgetUnsignedBytegetUnsignedIntgetUnsignedMediumgetUnsignedShortreadBooleanreadBytereadCharreadDoublereadFloatreadIntreadLongreadMediumreadShortreadUnsignedBytereadUnsignedIntreadUnsignedMediumreadUnsignedShortsetBooleansetBytesetCharsetDoublesetFloatsetIntsetLongsetMediumsetShortsetUnsignedBytesetUnsignedIntsetUnsignedMediumsetUnsignedShortwriteBooleanwriteBytewriteCharwriteDoublewriteFloatwriteIntwriteLongwriteMediumwriteShortwriteUnsignedBytewriteUnsignedIntwriteUnsignedMediumwriteUnsignedShort
Inherited from io.netty5.util.Resource:
closedisposeisAccessibleisAccessiblesendtouchtouch