Channel (2) disperses/aggregates Scatter/Gather and nioscatter in Java NIO
What is Scatter/Gather?
Scatter/gather refers to implementing a simple I/O operation on multiple buffers, such as reading data from channels to multiple buffers, or write data from multiple buffers to the channel;
Scatter (scattered): refers to the process of reading data from the channel and distributing data to multiple Buffer zones. This process fills up each Buffer zone, until there is no data in the channel or there is no space in the buffer;
Gather (aggregation): refers to the process of gathering multiple Buffer buffers and writing them to the channel. This process is similar to connecting the content of multiple buffers to write the channel;
Scatter/gather Interface
The following are definitions of the ScatteringByteChannel interface and GatheringByteChannel interface. We can find that all the methods defined in the interface are passed in a Buffer array;
The so-called scatter/gather operation is to aggregate the Buffer array and write it to a channel, or read the channel data and distribute it to the Buffer array;
public interface ScatteringByteChannel extends ReadableByteChannel{ public long read(ByteBuffer[] dsts) throws IOException; public long read(ByteBuffer[] dsts, int offset, int length) throws IOException;}public interface GatheringByteChannel extends WritableByteChannel{ public long write(ByteBuffer[] srcs) throws IOException; public long write(ByteBuffer[] srcs, int offset, int length) throws IOException;}
Note: The read () and write () methods with the offset and length parameters allow us to use only the subset of the buffer array. Note that the offset here refers to the index of the buffer array, instead of the index of the Buffer data, length refers to the number of buffers to be used;
The following code writes the second, third, and fourth buffer content to the channel;
int bytesRead = channel.write (fiveBuffers, 1, 3);
Note that both scatter and gather operations read or write data in sequence in the buffer array;
Gather write
Scatter/gather is often used for separate processing of transmitted data. The following is an example of clustered data writing:
ByteBuffer header = ByteBuffer.allocate(128);ByteBuffer body = ByteBuffer.allocate(1024);//write data into buffersByteBuffer[] bufferArray = { header, body };channel.write(bufferArray);
The above code willThe data in the buffer zones header and body are written to the channel;
Note that not all data is written to the channel. The written data should be determined based on the value of position and limit. Only data between position and limit will be written;
For exampleThe header Buffer contains 128 bytes of data, but position = 0 and limit = 58; therefore, only data with a subscript index of 0-57 will be written to the channel;
Scatter reading
The following is an example of distributed reading:
ByteBuffer header = ByteBuffer.allocate(128);ByteBuffer body = ByteBuffer.allocate(1024);ByteBuffer[] bufferArray = { header, body };channel.read(bufferArray);
The above code writes data in the channel to the Buffer in sequence. When a buffer is full, the channel then writes data to another buffer;
For example, if the channel contains 200 bytes of data, the header will be written into 128 bytes of data, and the body will be written into 72 bytes of data;
Benefits
More efficient (the following content is taken 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 as an appropriate local call to directly fill or extract the buffer zone, reduces or avoids buffer copy and system calls;
Scatter/Gather should use direct ByteBuffers to obtain the maximum performance advantage from local I/O;
References
Java NIO
Http://ifeve.com/java-nio-scattergather/