Java NiO Series Tutorial (iv) Scatter/gather

Source: Internet
Author: User

Original address: http://ifeve.com/java-nio-scattergather/

Java NiO began to support Scatter/gather,scatter/gather to describe the operation of reading or writing to the channel from the channel (the translator notes: Channels are often translated in Chinese). Dispersion (scatter) reads from the channel means that the data read is written to multiple buffer during a read operation. As a result, the channel will "scatter" (scatter) the data read from the channel into multiple buffer. Aggregation (gather) writes a channel that writes multiple buffer data to the same channel during a write operation, so that the channel sends the data in multiple buffer "clustered (gather)" to the channel.

Scatter/gather is often used in situations where the transferred data needs to be processed separately, such as transmitting a message consisting of a message header and a message body, you may scatter the message body and the message header into different buffer, so that you can easily handle the message header and the message body.

scattering Reads Scattering reads refers to the data being read from a channel into multiple buffer. As described:

Java nio:scattering Read

The code examples are as follows:

1 Bytebuffer Header = bytebuffer.allocate (+); 2 bytebuffer Body   = bytebuffer.allocate (1024x768); 3 4 bytebuffer[] Bufferarray = {header, body}; 5 6 Channel.read (Bufferarray);

Note that buffer is first inserted into the array, and then the array is used as the input parameter of the Channel.read (). The read () method writes the data read from the channel into buffer in the order of buffer in the array, and when one buffer is full, the channel is written to the other buffer.

Scattering reads before moving the next buffer, it must fill the current buffer, which also means that it does not apply to dynamic messages (Translator Note: Message size is not fixed). In other words, if there is a message header and a message body, the message header must be populated (for example, 128byte) for scattering reads to work correctly.

Gathering writes

Gathering writes refers to data written from multiple buffer to the same channel. As described:

Java nio:gathering Write

The code examples are as follows:

1 Bytebuffer Header = bytebuffer.allocate (+); 2 bytebuffer Body   = bytebuffer.allocate (1024x768); 3 4 // write data into buffers 5 6 bytebuffer[] Bufferarray = {header, body}; 7 8 channel.write (Bufferarray);

The buffers array is the entry for the Write () method, and the write () method writes data to the channel in the order in which buffer is in the array, noting that only data between position and limit is written. Therefore, if a buffer has a capacity of 128byte, but contains only 58byte of data, then this 58byte of data will be written to the channel. Therefore, contrary to scattering reads, gathering writes can handle dynamic messages better.

Java NiO Series Tutorial (iv) Scatter/gather

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.