The Bytebuf of Netty

Source: Internet
Author: User
Tags readable
first, the function principle
Bytebuf is a byte-stored buffer.
Bytebuf through two pointers to assist the buffer read and write operation, read operation using Readindex, write operation using Writeindex.
+-------------------+------------------+------------------+
| discardable bytes |  Readable bytes  |  Writable bytes  |     | | (CONTENT)    |                  |
+-------------------+------------------+------------------+
|                   |                  |                  |
0      <=      readerindex   <=   writerindex    <=    capacity
discardable bytes Discarded read space readable bytes readable space writeable writable space
Like what:
Bytebuf Heapbuffer = Unpooled.buffer ();
System.out.println (Heapbuffer);
Results:
Unpooledheapbytebuf (ridx:0, widx:0, cap:256)
RIDX is readerindex read Data index, location starting from 0 Widx is writeindex write data index, position from 0 start cap is capacity buffer initialization capacity, default 256, can be set by Unpooled.buffer (8), The initialization buffer capacity is 8.
If the write content exceeds Cap,cap, the capacity is automatically increased, but the buffer maximum capacity maxcapacity is not exceeded.
Bytebuf Heapbuffer = Unpooled.buffer (8);
SYSTEM.OUT.PRINTLN ("Initialization:" +heapbuffer);
Heapbuffer.writebytes ("Test test test");
System.out.println ("Write test test:" +heapbuffer);
Results:
Initialization: Unpooledheapbytebuf (ridx:0, widx:0, cap:8) write test test test
: Unpooledheapbytebuf (ridx:0, widx:18, cap:64)
Cap initialization 8, up to 64
Buffer content copied to byte array
1, create buffer
bytebuf Heapbuffer = Unpooled.buffer (8);
		
2, write buffer content
heapbuffer.writebytes ("Test test test". GetBytes ());
		
3, create byte array
byte[] b = new byte[heapbuffer.readablebytes ()];
		
System.out.println (b[11]);
		
4, copy content to byte array b
heapbuffer.readbytes (b);
		
System.out.println (b[11]);
		
5, byte array spin string
str = new string (b);
		
System.out.println (str);
Results:
0 -107 test test test

Bytebuf Turn Bytebuffer
Bytebuffer BB = Heapbuffer.niobuffer ();

Main class inheritance diagram for Bytebuf

From the point of view of memory allocation, BYTEBUF can be divided into two categories:
1, heap memory (HEAPBYTEBUF) byte buffer: The feature is the memory allocation and recovery speed, can be automatically recycled by the JVM; The disadvantage is that if you do socket IO read and write, you need to do an additional memory replication, the heap memory corresponding buffer to copy into the kernel channel, The performance will fall to a certain extent
2, Direct Memory (DIRECTBYTEBUF) byte buffer: Not heap memory, it is allocated to the external memory, compared to heap memory, its allocation and recovery speed will be slower, but write it or read from the socket channel, due to less memory replication, faster than heap memory
The best practice of Netty is to use DIRECTBYTEBUF in the read-write buffer of the I/O communication thread, and the codec module of the back-end business message uses HEAPBYTEBUF so that the combination can achieve optimal performance.
Four ways of declaring bytebuf
Bytebuf Heapbuffer = Unpooled.buffer ();
System.out.println (heapbuffer);
		
Bytebuf Directbuffer = Unpooled.directbuffer ();
System.out.println (directbuffer);
		
Bytebuf Wrappedbuffer = unpooled.wrappedbuffer (new byte[128));
System.out.println (wrappedbuffer);
		
Bytebuf Copiedbuffer = unpooled.copiedbuffer (new byte[128));
System.out.println (Copiedbuffer);
Results:
Unpooledheapbytebuf (ridx:0, widx:0, cap:256)
simpleleakawarebytebuf (Unpooledunsafedirectbytebuf ridx:0, Widx: 0, cap:256))
unpooledheapbytebuf (ridx:0, widx:128, cap:128/128)
unpooledheapbytebuf (ridx:0, widx:128, cap:1 28/128)

Not finished, to be continued

Related Article

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.