Skip to content

ByteBuf

ByteBuf is a public abstract class found within the Netty framework that implements the ReferenceCounted and Comparable<ByteBuf> interfaces^[600-developer-big-data-netty-netty-ByteBuf.md].

Internal Structure

The buffer maintains data using a tri-partite structure defined by three indices:

  • 0 to readerIndex: This region contains bytes that have already been read and are considered "discardable bytes"^[600-developer-big-data-netty-netty-ByteBuf.md].
  • readerIndex to writerIndex: This region, referred to as CONTENT, contains the readable bytes that have not yet been consumed^[600-developer-big-data-netty-netty-ByteBuf.md].
  • writerIndex to capacity: This region represents "writable bytes," which is the available space for new data^[600-developer-big-data-netty-netty-ByteBuf.md].

Data Access

Data access can be performed using either absolute or relative indexing^[600-developer-big-data-netty-netty-ByteBuf.md].

  • Absolute Access: Methods such as getByte(i) allow reading at a specific index without modifying the readerIndex^[600-developer-big-data-netty-netty-ByteBuf.md].
  • Relative Access: Methods such as readByte() read the current byte and increment the readerIndex^[600-developer-big-data-netty-netty-ByteBuf.md].

Memory Management Operations

Discarding Read Bytes

The discardReadBytes() method is used to reclaim space occupied by data that has already been read^[600-developer-big-data-netty-netty-ByteBuf.md]. This operation moves the readable bytes to the start of the buffer, effectively discarding the space between 0 and readerIndex and increasing the writable capacity^[600-developer-big-data-netty-netty-ByteBuf.md].

Clearing Indexes

The clear() method resets the buffer's internal pointers without necessarily zeroing out the memory content^[600-developer-big-data-netty-netty-ByteBuf.md]. It sets both readerIndex and writerIndex to 0, making the entire capacity available for writing^[600-developer-big-data-netty-netty-ByteBuf.md].

Sources

  • 600-developer-big-data-netty-netty-ByteBuf.md