Channel channels in Java NiO (ii) scatter/aggregate Scatter/gather

Source: Internet
Author: User

What is Scatter/gather

Scatter/gather refers to implementing a simple I/O operation on multiple buffers, such as reading data from a channel to multiple buffers, or writing data from multiple buffers to a channel;

Scatter (scatter): Refers to the process of reading data from a channel into multiple buffer buffers, which fills each buffer until there is no data or buffer space in the channel;

Gather (aggregation): refers to the process of aggregating multiple buffer buffers into a channel, which is similar to connecting the contents of multiple buffers to a channel;

Scatter/gather interface

The following is the definition of the Scatteringbytechannel interface and the Gatheringbytechannel interface, and we can see that the methods defined in the interface are passed into a buffer array;

The so-called Scatter/gather operation is to aggregate (gather) the buffer array and write to a channel, or read the channel data and scatter (scatter) into the buffer array;

 Public InterfaceScatteringbytechannelextendsreadablebytechannel{ Public LongRead (bytebuffer[] dsts)throwsIOException;  Public LongRead (bytebuffer[] DSTs,intOffsetintLengththrowsIOException;} Public InterfaceGatheringbytechannelextendswritablebytechannel{ Public LongWrite (bytebuffer[] srcs)throwsIOException;  Public LongWrite (bytebuffer[] SRCs,intOffsetintLengththrowsIOException;}

As a reminder, the read () and write () methods with the offset and length parameters allow us to use only a subset of the buffer array, note that offset here refers to the buffer array index, not the index of the buffers data, and length refers to the number of buffers to use;

The following code will write the second, third, fourth buffer contents toward the channel;

int bytesread = Channel.write (fivebuffers, 1, 3);

Note that both scatter and gather operations are read or written sequentially in the order of buffer in the array;

Gather write

Scatter/gather is often used for situations where the transmitted data needs to be handled separately, let's look at an example of a clustered write:

Bytebuffer Header = bytebuffer.allocate (128); Bytebuffer Body   = bytebuffer.allocate (1024x768);//write data into buffersbytebuffer[] Bufferarray = {header, body};chan Nel.write (Bufferarray);

The code above willheader和body这两个缓冲区的数据写入到通道;

It is important to note that not all data is written to the channel, the data written is judged by the value of position and limit, and only the data between position and limit is written;

As an example, if the aboveheader缓冲区中有128个字节数据,但此时position=0,limit=58;那么只有下标索引为0-57的数据才会被写入到通道中;

Scatter read

Here is an example of a decentralized read:

Bytebuffer Header = bytebuffer.allocate (+); Bytebuffer body   = bytebuffer.allocate (1024x768= {header, Body};channel.read (Bufferarray);

The above code writes the data in the channel sequentially to buffer, and when one buffer is full, the channel is then written to the other buffer;

For example, if the channel has 200 bytes of data, then the header will be written to 128 bytes of data, thebody will be written to 72 bytes of data;

Benefits

More efficient (excerpt from "JAVA NIO");

Most modern operating systems support local vector I/O (native vectored I/O) operations.

When you request a scatter/gather operation on a channel, the request is translated into the appropriate local call to populate or extract the buffer directly, reducing or avoiding buffer copies and system calls;

Scatter/gather should use direct bytebuffers to get the maximum performance advantage from local I/O;

Resources

"Java NIO"

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

Channel channels in Java NiO (ii) scatter/aggregate 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.