Scatter/Gather or Vectored I/OIn Java NIO the channel provides an important capability known as scatter/gather or vectored I/O. It is a simple yet powerful technique through which the bytes can be written from a set of buffers to a stream using a single write() function and bytes can be read from a stream into a set of buffers using a single read() function. Java NIO has inbuilt scatter/gather support. It can be used in reading from channels and writing to channels. Scattering ReadsThe 'scattering read' is used for reading the data from a single channel into multiple buffers. Let's see the illustration of the Scatter principle: Let's see the code example that performs a scattering read operation: Gathering WritesThe 'gathering write' is used for writing the data from a multiple buffers into a single channel. Let's see the simple illustration of the Gather principle: Let's see the code example that performs a gathering write operation: Basic Scatter/Gather ExampleLet's see the simple example of two buffers. The first buffer holds a random number and second buffer holds the data we want to write using scatter/gather mechanism: Output: In above program, first buffer print the random output on a console and second buffer print the "Scattering and Gathering example shown in javatpoint.com" on a console. It also replaces the content of testout.txt file with "Scattering and Gathering example shown in javatpoint.com". Next TopicData Transfer between Channels |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India