1. Bytebuf of Netty Learning

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.