Title means Java NiO the dispersion concentrates
Java NiO built-in support for dispersion and collection. The concept is mainly used to read and write channel scattered aggregation.
A channel of the readout dispersion is read in multiple data buffer. So. The data is scattered across multiple buffer.
A central write to a channel is to write data from multiple buffer to one channel. So. The channel collects data from multiple buffer into one channel.
Decentralized collection is often used in scenarios where data transfer is handled separately.
For example, a message consists of a header and body, and you need to keep the header and body in different buffer.
This makes it easier to handle the header and body separately.
Scatter read
Look at the example:
Notice how buffer is inserted into the array, and the array is passed to the Channel.read method as a parameter. The Read method extracts data from the channel and writes the data in the order of buffer in the buffer array, which is full. will be written to the next buffer in sequence.
In fact, scattered reads fill a buffer before moving to the next buffer, meaning that such a way is not suitable for dynamic changes in message size.
In other words, suppose you have a header and body, and then the header is fixed-size (for example: 128 bytes), so that the scattered reads can play very well.
Centralized writing
Note: The flip method Call of buffer
The buffer array is passed as a parameter to the Write method, and the contents of the buffer are written sequentially to the channel.
Only data that is between position and limit is written.
Therefore, suppose that a buffer has a capacity of 128 bytes. But with just 58 bytes of content, only 58 bytes are written to the channel. So. Compared to the scattered reads. Centralized write is better for processing with a message size that is not fixed.
Next section: "Java" "NiO" 6, Java NIO Channel to channel transfers
"Java" "NiO" 5, Java NiO scatter/gather