The Netty uses bytebuf instead of the bytebuffer provided by Java NIO as a container for bytes.
First, the index
BYTEBUF provides two pointer variables to support read and write operations, read operations using Readerindex, and write operations using Writerindex. Such as:
- Bytes can be discarded because they have been read
- Readable bytes, written but not yet read
- Writable bytes
Second, index management
- Call Markreaderindex (), Markwriterindex (), Resetreaderindex (), and Resetwriterindex () to set and reposition Readerindex and WR Iterindex,
- Call Readerindex (int) or writerindex (int) to move the pointer to the specified position
- Call Clear () to set both Readerindex and Writerindex to 0
Third, the query operation
You can use the Bytebufprocessor parameter method, and the following example implements the search for a carriage return character (\ r):
1 bytebuf in = (bytebuf) msg; 2 int index = in.foreachbyte (BYTEPROCESSOR.FIND_CR);
Iv. derived buffers
The slice method and the Copy method all have the ability to copy, but they differ, and the following two examples illustrate their differences.
First look at the slice example:
1Charset UTF8 = charset.forname ("UTF-8");2Bytebuf buf = Unpooled.copiedbuffer ("Netty in Action rocks!", UTF8);3 4Bytebuf sliced = Buf.slice (0, 14);//Create a new slice starting from 0 to 145System.out.println (sliced.tostring (UTF8));//print Netty in Action6 7Buf.setbyte (0, (byte) ' J ');//Update bytes indexed to 08 //assertion succeeded, indicating two data shares after slice9 assertBuf.getbyte (0) = = sliced.getbyte (0);
This note slice returns a copy of the original buffer, sharing the same piece of data. So if you need to manipulate a piece of data, use the slice method.
Let's look at how the Copy method differs:
1Charset UTF8 = charset.forname ("UTF-8");2Bytebuf buf = Unpooled.copiedbuffer ("Netty in Action rocks!", UTF8); 3 4Bytebuf copy = buf.copy (0, 14);//Note that the copy is used here5 System.out.println (copy.tostring (UTF8));6 7Buf.setbyte (0, (byte) ' J '); 8 //assertion succeeds stating that the original data modification does not affect copy9 assertBuf.getbyte (0)! = Copy.getbyte (0);
As you can see, the code is almost the same, but the derived bytebuf effect is different.
V. Read/write operations
There are two main types of read/write operations:
- Get ()/Set () operation: The write index and the read index remain unchanged starting at the given index
- Read ()/write () operation: increments the current write index or read index, starting at the given index, based on the number of bytes accessed.
Particular attention needs to be paid to the effect of the above two types of operations on read and write indexes.
The common get () operation is as follows:
The common set () operation is as follows:
The common read () operation is as follows:
Each read () method corresponds to a write () method, as follows:
Six, more operations
Some of the more commonly used methods are as follows:
Netty Getting Started (iv) BYTEBUF byte-level operations