Distribution of Bytebuf
I. Bytebufallocator Pool class
Can allocate heap-based or direct memory bytebuf
There are two ways to get Bytebufallocator references:
Channel.alloc ();
Ctx.alloc ();
Two implementations of Bytebufallocator:
Pooledbytebufallocator
Unpooledbytebufallocator
Two. unpooled Non-pooled bytebuf
Three. Bytebufutil
Bytebuf and Bytebufholder
Bytebuf's main have read and write two pointers, which he is more convenient and easy to understand than the native Bytebuffer place
read*, write* the beginning of the method will change the index, but set* will not change the index position
|**** version number 4 bytes length specific content ****|
Bytebuf Mode of Use
1. Heap buffers (array-based implementations)
So you can determine whether an array is supported by HasArray
Bytebuf heapbuf = ...; if // 1 byte // 2 int // 3 int length = heapbuf.readablebytes (); // 4 // 5}
2. Direct buffer
Bytebuf directbuf = ... if // 1 int length = directbuf.readablebytes (); // 2 byte New byte // 3 // //5}
3. Composite buffers (COMPOSITEBYTEBUF)
HasArray always returns False
Compositebytebuf messagebuf =// can support or direct // can support or direct Messagebuf.addcomponents (Headerbuf, bodybuf); // .... // //2 for (int//3 System.out.println (Messagebuf.component (i). toString ());}
accessing data
Compositebytebuf compbuf = ...; int // 1 byte New byte // 2 // 3 // 4
Random Access Index:
such as GetByte, will not change the index
Bytes that can be discarded are re-exploited:
Iscardreadbytes ();
Index operations:
Markwriterindex ()
Markreaderindex ()
Resetreaderindex ()
Resetwriterindex ()
Clear () lower cost than discardreadbytes ()
Query operation:
Foreachbyte
int index = Buffer.foreachbyte (BYTEBUFPROCESSOR.FIND_CR);
Foreachbyte (Bytebufprocessor.find_nul)
Derived buffers:
Represents a dedicated "view" of displaying bytebuf content
This view is generated by several methods
Duplicate (), slice (), slice (int, int), readOnly (), Order (Byteorder)
Is this view and source shared by data? Is the read-write index the same? is the tag index the same?
A: They're all shared.
Copy:
Copy () and copy (int, int)
This copy is independent, and the source data is not shared
Need a piece of data:
Slice (int, int)
And the source data is shared
Read and write operations:
Set write/get read does not alter index
Write/read
1. Bytebuf of Netty Learning