Skip to content

Absolute vs relative buffer access

In buffer implementations like Netty's ByteBuf, data access operations are categorized based on whether they modify the buffer's internal pointers. The two primary modes are absolute access and relative access^[600-developer__big-data__netty__netty-ByteBuf.md].

Absolute Access

Absolute access involves reading or writing data at a specific index without modifying the buffer's readerIndex. When using methods such as getByte(i), the operation targets the exact memory location provided^[600-developer__big-data__netty__netty-ByteBuf.md]. This allows for random data access within the buffer's capacity, as the read position remains static during the operation^[600-developer__big-data__netty__netty-ByteBuf.md].

Relative Access

Relative access involves reading or writing data based on the current value of the internal pointers (readerIndex or writerIndex). When a method like readByte() is called, it reads the byte at the current readerIndex and immediately increments the index by 1^[600-developer__big-data__netty__netty-ByteBuf.md]. This mode is typically used for sequential processing, where the code iterates through the "CONTENT" (readable bytes) until the buffer is no longer readable^[600-developer__big-data__netty__netty-ByteBuf.md].

Comparison

The key distinction lies in side effects: absolute access is non-destructive to the buffer's state pointers, whereas relative access advances the pointers to track progress^[600-developer__big-data__netty__netty-ByteBuf.md].

  • ByteBuf
  • [[ByteBuf pointer]] (readerIndex/writerIndex)

Sources

^[600-developer__big-data__netty__netty-ByteBuf.md]