Java Nio Scatter/Gather Example
In Java Nio, the channel provides an important capability known as scatter/gather or vectored I/O in some circles. Scatter/gather is a simple yet powerful concept and this tutorial explains how scatter/gather can be really useful in situations where developers need to separate work with the various parts of the transmitted data.
Table Of Contents
1. Introduction
Java Nio was developed to allow the Java programmers implement the high-speed input-output operations without using the custom native code. Nio moves the time-taking I/O activities like filling, namely and draining buffers etc back into the operating system, thus allows the great increase in the operational speed.
Java Nio consists of the following core components:
- Channel & Buffers: In standard I/O API the character streams and the byte streams are used but in NIO, developers work with the channels and buffers. In this case, the data is always written from a buffer to a channel and read from a channel to a buffer
- Selectors: It is an object that can be used for monitoring the multiple channels for events like data arrived, connections opened etc. Thus, a single thread can monitor the multiple channels for the data
- Non-blocking I/O: Here the application immediately returns the available data and application should have a pooling mechanism to find out when more data is available
Do note, Java NIO has more components and classes but the Channel, Buffer, and Selector are used as the core of the API.
1.1 Java Nio Scatter/Gather
Java Nio comes with built-in scatter/gather support. Scatter/gather are the concepts used for reading from, and writing to channels i.e. Scatter/Gather is a technique through which bytes can be read from a stream into a set of buffers with a single read() invocation and bytes can be written from a set of buffers to a stream with a single write() invocation.
Scatter/Gather can be really useful in situations where developers need to separate work with the various parts of the transmitted data. For e.g., if a message consists of a header and a body, developers can keep the header and body in separate buffers for easy processing.
1.1.1 Scattering Reads
The ‘scattering read‘ is used for reading the data from a single channel into the multiple buffers. Here is an illustration of the Scatter principle:

Let’s see a code snippet that performs a scattering read operation.
ByteBuffer bblen1 = ByteBuffer.allocate(1024);
ByteBuffer bblen2 = ByteBuffer.allocate(1024);
scatter.read(new ByteBuffer[] {bblen1, bblen2});
Notice how the buffers are first inserted into an array, then the array is passed as a parameter to the scatter.read() method. The read() method writes the data from the channel in a sequence that buffers occur in an array. Once a buffer is full, the channel moves on to fill the next buffer.
1.1.2 Gathering Writes
The ‘gathering write‘ is used for writing the data from the multiple buffers into a single channel. Here is an illustration of the Gather principle:

Let’s see a code snippet that performs a gathering write operation.
ByteBuffer bblen1 = ByteBuffer.allocate(1024);
ByteBuffer bblen2 = ByteBuffer.allocate(1024);
gather.write(new ByteBuffer[] {bblen1, bblen2});
The array of buffers are passed into the write() method, which writes the content of the buffers in a sequence they are encountered in an array. Do note, only the data between the position and the limit of the buffers are written.
Now, open up the Eclipse IDE and let’s write a quick example to understand how to use this feature!
2. Java Nio Scatter/Gather Example
2.1 Tools Used
We are using Eclipse Kepler SR2, JDK 8 and Maven. Having said that, we have tested the code against JDK 1.7 and it works well.
2.2 Project Structure
Firstly, let’s review the final project structure, in case you are confused about where you should create the corresponding files or folder later!

2.3 Project Creation
This section will demonstrate on how to create a Java-based Maven project with Eclipse. In Eclipse IDE, go to File -> New -> Maven Project.
In the New Maven Project window, it will ask you to select project location. By default, ‘Use default workspace location’ will be selected. Select the ‘Create a simple project (skip archetype selection)’ checkbox and just click on next button to proceed.
It will ask you to ‘Enter the group and the artifact id for the project’. We will input the details as shown in the below image. The version number will be by default: 0.0.1-SNAPSHOT.
Click on Finish and the creation of a maven project is completed. If you observe, it has downloaded the maven dependencies and a pom.xml file will be created. It will have the following code:
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>JavaNioScatterGather</groupId> <artifactId>JavaNioScatterGather</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> </project>
Developers can start adding the dependencies that they want like JUnit etc. Let’s start building the application!











This is great and all but you did not provide an actual practical example of the one use case you mentioned “header and body in separate buffers”. This tutorial is pretty useless without an actual practical example. It is just more noise on the internet to take up your time and not learn anything new.
Jarrod,
Hope you are doing well. Thanks for pointing out the use case but due to the unavailability of the FTP resource at that moment I wasn’t able to write down an actual practical code (i.e. clearing showing the header & body separation) and hence had to redirect the code approach from FTP to File. I will follow up with the support team and would request them for one more article on this topic!
Have a great day!