Two simple questions, which I am not able to solve by reading the documentation:
byte[]
ByteBuf
?ByteBuffer
ByteBuf
?Sequential Access Indexing ByteBuf provides two pointer variables to support sequential read and write operations - readerIndex for a read operation and writerIndex for a write operation respectively.
ByteBuffer is among several buffers provided by Java NIO. Its just a container or holding tank to read data from or write data to. Above behavior is achieved by allocating a direct buffer using allocateDirect() API on Buffer. Java Documentation of Byte Buffer has useful information.
buffer Description. Abstraction of a byte buffer - the fundamental data structure to represent a low-level binary and text message. Netty uses its own buffer API instead of NIO ByteBuffer to represent a sequence of bytes. This approach has significant advantage over using ByteBuffer .
The documentation seems pretty clear to me:
Creation of a buffer
It is recommended to create a new buffer using the helper methods in Unpooled rather than calling an individual implementation's constructor.
Then in Unpooled
, you've got options of wrapping or copying. For example:
Unpooled.copiedBuffer(ByteBuffer)
Unpooled.copiedBuffer(byte[])
Unpooled.wrappedBuffer(ByteBuffer)
Unpooled.wrappedBuffer(byte[])
Choose whichever method is appropriate based on whether you want changes made in the returned ByteBuf
to be passed through to the original byte array/buffer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With