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 in which multiple buffer data is written to the same channel during a write operation, so the channel
Sends data from multiple buffer "aggregates (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:
The code examples are as follows:
Bytebuffer Header = bytebuffer.allocate (+); Bytebuffer body = bytebuffer.allocate (1024x768= {header, Body};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:
The code examples are as follows:
Bytebuffer Header = bytebuffer.allocate (+); Bytebuffer body = bytebuffer.allocate (1024x768); // write data into buffers bytebuffer[] Bufferarray = {header, Body};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.
"In-depth study of Java NIO 6" Java NiO scatter/gather