Netty5 Source Analysis (v)--bytebuf buffer

Source: Internet
Author: User

The bytebuf buffer of Netty is more flexible and convenient than the bytebuffer of Java itself. Its class structure is also more complex, here only to say a few points of Bytebuf core.


1. The most important thing is to understand why you should bytebuf this component. Mainly because of the select/poll/epoll-based IO multiplexing technology of NIO non-blocking synchronous IO model, because it is synchronous IO, requires the user thread to handle IO read and write, because of the non-blocking, each call to read, write read and write bytes is indeterminate, Therefore, the non-blocking synchronous IO must have a buffer component to hold the intermediate state of each read and write, through the buffer to determine whether read and write completed. For more information, please refer to http://blog.csdn.net/iter_zc/article/details/39291647


2. Bytebuf is not a bytebuffer package, but a buffer is re-implemented. Bytebuffer only uses a position pointer to record the current reading and writing position, Bytebuf uses two pointers Readerindex, Writerindex to record the current reading and writing position, the use of more simple and convenient.


3. Bytebuffer is a fixed-length buffer that throws an exception when the put method is writing data that is larger than the writable capacity. BYTEBUF has improved this design to support automatic expansion. Checks whether a full write is possible before each put and, if not, automatically expands the capacity of the bytebuf to ensure that the put method does not throw an exception.

Public bytebuf writeint (int value) {ensurewritable (4);        _setint (writerindex, value);        Writerindex + = 4;    return this; }   public bytebuf ensurewritable (int minwritablebytes) {        if ( Minwritablebytes < 0) {            throw new IllegalArgumentException (String.Format (                     "Minwritablebytes:%d (expected: >= 0)", minwritablebytes));        }        if (minwritablebytes <= Writablebytes ()) {            return this;        }        if (minwritablebytes > Maxcapacity- Writerindex) {           throw new Indexoutofboundsexception (String.Format (                     "Writerindex (%d) + minwritablebytes (%d) exceeds maxcapacity ( %d):%s ",                     Writerindex, Minwritablebytes, maxcapacity, this));       }        //Normalize The capacity to the power of 2.     & nbsp;  int newcapacity = calculatenewcapacity (Writerindex + minwritablebytes);        //Adjust to the new capacity.        capacity (newcapacity);         return this;   }//unpooledheapbytebuf capacity method to automatically expand public BYTEBUF Capacity (int newcapacity) {        ensureaccessIble ();        if (newcapacity < 0 | | newcapacity > maxcapacity ()) {             throw new IllegalArgumentException ("Newcapacity:" + newcapacity);       }        int Oldcapacity = array.length;        if (newcapacity > OldCapacity) {             byte[] NewArray = new byte[newcapacity];             system.arraycopy (array, 0, NewArray, 0, Array.Length);             SetArray (NewArray);        } else if (Newcapacity < oldcapacity) {            byte[] NewArray = new BYTE[NEWCAPACITY];&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;  int readerindex = Readerindex ();            if ( Readerindex < newcapacity) {                 int writerindex = Writerindex ();                 if (Writerindex > Newcapacity) {                     Writerindex (writerindex = newcapacity);                }                 system.arraycopy (Array, Readerindex, NewArray, Readerindex, Writerindex-readerindex);           } else {                 Setindex (NewcapaCity, newcapacity);           }             SetArray (newarray);       }         return this;   }private void SetArray (byte[] initialarray) {         array = initialarray;        Tmpniobuf = null;   }

4. Like Bytebuffer, Bytebuf also supports in-heap buffers and out-of-heap direct buffers, which, by experience, use an out-of-heap direct buffer for the buffer of the underlying IO processing thread, reducing the IO replication one time. The codec of the business message uses the in-heap buffer, which is more efficient and does not involve the replication of kernel buffers.


5. The bytebuf buffer in the heap is divided into memory pool buffer pooledbytebuf and normal memory buffer unpooledheapbytebuf. Pooledbytebuf uses a binary tree to implement a memory pool that centrally manages the allocation and release of memory without creating a new buffer object each time it is used. UNPOOLEDHEAPBYTEBUF creates a new buffer object each time. It is recommended to use POOLEDBYTEBUF in high concurrency, which can save memory allocation. In the case of performance assurance, the UNPOOLEDHEAPBYTEBUF can be used, the implementation is relatively simple.


Please refer to the documentation for the specific BYTEBUF API.






Netty5 Source Analysis (v)--bytebuf buffer

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.