Java Nio
| 1 |
Java NIO Tutorial |
| 2 |
Java NIO Overview |
| 3 |
Java NIO Channel |
| 4 |
Java NIO Buffer |
| 5 |
Java NIO scatter/gather |
| 6 |
Java NIO Channel to channel transfers |
| 7 |
Java NIO Selector |
| 8 |
Java NIO FileChannel |
| 9 |
Java NIO Socketchannel |
| 10 |
Java NIO Serversocketchannel |
| 11 |
Java NIO Datagramchannel |
| 12 |
Java NIO Pipe |
| 13 |
Java NIO vs. IO |
Java NIO Scatter/gather
by Jakob JenkovConnect with me:
Rate article: Share Article:tweet
Table of Contents
- Scattering Reads
- Gathering writes
Java NIO comes with built-in scatter/gather support. Scatter/gather is concepts used in reading from, and writing to channels.
A scattering read from a channel is a read operation this reads data into more than one buffer. Thus, the channel "scatters" The data from the channel into the multiple buffers.
A gathering write to a channel is a write operation this writes data from more than one buffer into a single channel. Thus, the channel "gathers" the data from multiple buffers to one channel.
Scatter/gather can really useful in situations where do I need to work with various parts of the transmitted data SEPA Rately. For instance, if a message consists of a headers and a body, you might keep the header and body in separate buffers. Doing so could make it easier for your to work with header and body separately.
Scattering Reads
A "scattering read" reads data from a single channel into multiple buffers. Here is a illustration of that principle:
Here's an illustration of the Scatter principle:
Here's a code example that shows what to perform a scattering read:
Bytebuffer Header = bytebuffer.allocate (128); Bytebuffer Body = bytebuffer.allocate (1024); Bytebuffer[] Bufferarray = {header, body};channel.read (buffers);
Notice How the buffers is first inserted into a array, then the array passed as parameter to the channel.read() method. The read() method then writes data from the channel in the sequence the buffers occur in the array. Once a buffer is full, the channel moves in to fill the next buffer.
The fact that scattering reads fill up one buffer before moving on to the next and means that it's not suited for Dynamicall Y sized message parts. In other words, if you had a header and a body, and the header is a fixed size (e.g. bytes), then a scattering read wor KS fine.
Gathering writes
A "gathering write" writes data from multiple buffers to a single channel. Here is a illustration of that principle:
Here's a code example that shows how to perform a gathering write:
Bytebuffer Header = bytebuffer.allocate (128); Bytebuffer Body = bytebuffer.allocate (1024x768);//write data into buffersbytebuffer[] Bufferarray = {header, body};chan Nel.write (buffers);
The array of buffers is passed into write() the method, which writes the content of the buffers in the sequence they is en countered in the array. Only the data between position and limit of the buffers are written. Thus, if a buffer has a capacity of the bytes, but only contains-bytes, only the bytes is written from that buffer to T He channel. Thus, a gathering write works fine with dynamically sized message parts, and contrast to scattering reads.
Next: Java NIO Channel to Channel transfers
Java NIO Scatter/gather