"Java" "NiO" 5, Java NiO scatter/gather

Source: Internet
Author: User

Title means Java NiO the dispersion concentrates
Java NiO built-in support for dispersion and collection. The concept is mainly used to read and write channel scattered aggregation.


A channel of the readout dispersion is read in multiple data buffer. So. The data is scattered across multiple buffer.
A central write to a channel is to write data from multiple buffer to one channel. So. The channel collects data from multiple buffer into one channel.
Decentralized collection is often used in scenarios where data transfer is handled separately.

For example, a message consists of a header and body, and you need to keep the header and body in different buffer.

This makes it easier to handle the header and body separately.

Scatter read


Look at the example:

Notice how buffer is inserted into the array, and the array is passed to the Channel.read method as a parameter. The Read method extracts data from the channel and writes the data in the order of buffer in the buffer array, which is full. will be written to the next buffer in sequence.
In fact, scattered reads fill a buffer before moving to the next buffer, meaning that such a way is not suitable for dynamic changes in message size.

In other words, suppose you have a header and body, and then the header is fixed-size (for example: 128 bytes), so that the scattered reads can play very well.

Centralized writing


Note: The flip method Call of buffer

The buffer array is passed as a parameter to the Write method, and the contents of the buffer are written sequentially to the channel.

Only data that is between position and limit is written.
Therefore, suppose that a buffer has a capacity of 128 bytes. But with just 58 bytes of content, only 58 bytes are written to the channel. So. Compared to the scattered reads. Centralized write is better for processing with a message size that is not fixed.

Next section: "Java" "NiO" 6, Java NIO Channel to channel transfers

"Java" "NiO" 5, Java NiO scatter/gather

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.