Netty Getting Started (iv) BYTEBUF byte-level operations

Source: Internet
Author: User

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:

  

    1. Bytes can be discarded because they have been read
    2. Readable bytes, written but not yet read
    3. Writable bytes

Second, index management
    1. Call Markreaderindex (), Markwriterindex (), Resetreaderindex (), and Resetwriterindex () to set and reposition Readerindex and WR Iterindex,
    2. Call Readerindex (int) or writerindex (int) to move the pointer to the specified position
    3. 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

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.