Absolute vs relative ByteBuf access¶
In Netty's ByteBuf class, data can be retrieved from the buffer using two distinct modes: absolute access and relative access.^[600-developer__big-data__netty__netty-ByteBuf.md]
The distinction lies in how the operation interacts with the buffer's internal state, specifically the readerIndex.
Absolute Access¶
Absolute access refers to reading data based on a specific index position without modifying the buffer's internal pointers^[600-developer__big-data__netty__netty-ByteBuf.md].
When using methods such as getByte(i), the value at the specified index is retrieved directly^[600-developer__big-data__netty__netty-ByteBuf.md].
In this mode, the readerIndex remains unchanged during the operation^[600-developer__big-data__netty__netty-ByteBuf.md].
This is useful for random access or when inspecting data without consuming it.
ByteBuf buffer = ...;
for (int i = 0; i < buffer.capacity(); i ++) {
// Access by index; readerIndex does not move
byte b = buffer.getByte(i);
System.out.println((char) b);
}
Relative Access¶
Relative access refers to reading data sequentially based on the current position of the internal pointers^[600-developer__big-data__netty__netty-ByteBuf.md].
When using methods such as readByte(), the data is read starting from the current readerIndex^[600-developer__big-data__netty__netty-ByteBuf.md].
Crucially, after the data is read, the readerIndex advances (increases) to the next available byte^[600-developer__big-data__netty__netty-ByteBuf.md].
This mode is typically used for processing a stream of data where the order of consumption matters.
ByteBuf buffer = ...;
while (buffer.isReadable()) {
// readerIndex moves forward with each call
System.out.println(buffer.readByte());
}
Related Concepts¶
- ByteBuf
- [[Discardable bytes]]
Sources¶
^[600-developer__big-data__netty__netty-ByteBuf.md]