Java NIO series of tutorials (4) Scatter/Gather

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

Author : Jakob Jenkov    Translator : Guo Lei   

Java NIO began to support scatter/gather, which is used to describe the operation of reading from or writing to a Channel.
Scattering (scatter) reading from a Channel refers to writing the read data into multiple buffers during a read operation. Therefore, the Channel "scatters" the data read from the Channel into multiple Buffers.
Gathering into a Channel means writing data from multiple buffers to the same Channel during a write operation. Therefore, the Channel “gathers” the data in multiple Buffers and sends them to the Channel.

Scatter / gather is often used in situations where the transmitted 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 message header into different buffers, so that you can easily Handle message headers and message bodies.

Scattering Reads
Scattering Reads means that data is read from a channel to multiple buffers. Described as below:

Java NIO: Scattering Read

Java NIO: Scattering Read

The code example is as follows:

ByteBuffer header = ByteBuffer.allocate(128);
ByteBuffer body   = ByteBuffer.allocate(1024);

ByteBuffer[] bufferArray = { header, body };

channel.read(bufferArray);

Note that the buffer is first inserted into the array, and then the array is used as the input parameter to channel.read(). The read() method writes the data read from the channel to the buffer in the order of the buffer in the array. When a buffer is full, the channel writes to another buffer.

Scattering Reads must fill the current buffer before moving the next buffer, which also means that it is not suitable for dynamic messages (Translator's Note: The message size is not fixed). In other words, if there is a message header and message body, the message header must be filled (eg 128byte) for Scattering Reads to work properly.

Gathering Writes

Gathering Writes means that data is written from multiple buffers to the same channel. Described as below:

Java NIO: Gathering Write

Java NIO: Gathering Write

The code example is as follows:

ByteBuffer header = ByteBuffer.allocate(128);
ByteBuffer body   = ByteBuffer.allocate(1024);

//write data into buffers

ByteBuffer[] bufferArray = { header, body };

channel.write(bufferArray);

The buffers array is the input parameter of the write() method. The write() method will write data to the channel according to the order of the buffers in the array. Note that only the data between position and limit will be written. Therefore, if a buffer has a capacity of 128 bytes, but only contains 58 bytes of data, then the 58 bytes of data will be written to the channel. Therefore, in contrast to Scattering Reads, Gathering Writes can handle dynamic messages better.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326941140&siteId=291194637