Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Java NIO Scatter/Gather

Introduction to Java NIO Scatter/Gather

The Java Nio which includes the built-in functionality for scatter and gather concepts that defined separately are employed while reading from and writing to data channels which is read into more than one buffer stream during a scattered read from a channel as an result which produces the channel scatters and data into many buffers which handles the multiple pieces of the transmitted data that maintain the header and body.

Key Takeaways

  • The message processor for routing Multiple targets simultaneously sent a request message using Scatter-Gather.
  • It gathers all of the responses and combines them into a single message.
  • All message processor, which was deprecated version in Mule and it was replaced by Scatter-Gather.
  • Each segment which helps to overall the read and write request by each set of vectors in scatter/gather list.
  • Scatter Gather employs two or more routes—parallel processes—in tandem.

What is Java NIO Scatter and Gather?

When reading from and writing to data channels, which are read into multiple buffer streams during scattered reads from a channel, the Java NIO is used, which includes built-in functionality for scatter and gather concepts that are defined separately. As a result, the channel scatters and data into many buffers, which manages the numerous pieces of the transmitted data that maintain the header and body. Data generally read more than a one buffer during a scattered set of data channels as a result from data into many buffers. A write operation is known as collecting write to a channel which involves writing data from multiple buffers into a single channel. Data from many buffers which is used and therefore gathered into one data channel through via the other channel.

Application Building Class

A server aggregates which helps data from the clients who have been generated with shareable results from their trainer like Federated scatter and gather workflow. It has been included through via reference implementation of the default workflow from earlier set of iterations of NVIDIA FLARE. The app control will follow the work flow of nvflare.app and common.workflows.scatter and gather its data foundation. Its for loop Scatter and Gather there are some default steps which involved in developing the application.

Like below:

1. Java Class Creation

It is mainly required for java files that comes under to generate and execute the application which is based upon the jars. Right-click on src/main/java folder for helps to deploy the jar files. If its maven or other web based tool projects then generate the web.xml and also configured the required dependencies through via xml. After project creation pom.xml helps developers to start and add the dependencies for Junit etc.

Here we download the required jars like nio.jar contains n number of class files which helps to identify the required source files.

The package name will be most entered in the new pop-up window that will appear for creating the implementation class for after the package which is generated in the application end.

The above screenshot I have created the package called TestNG.

Class name called GatheringAndScattering which we created under the TestNG Package. Additionally if we want to choose or select the methods like Inherited abstract methods, Constructors from superclass and public static void main(String[] args). These methods are helpful for passing the data channels like both read and write operations. It includes the multiple set of read and write operations from the same channel there is used on the ScatteringByteChannel and GatheringByteChannel API interface for to supporting the Java NIO.

2. ScatteringByteChannel

It helps to read from a variety of channels and this allows us to read the same data information from a single point of channel into a n number of data buffers. For this several set of buffers that are mainly allotted and added to an array of the buffer type. The read() method mainly helps to receive the same set of array as an input and uses it for to write the data from the channel in the order in which the array’s buffer appear. The data channel continues on for to full fill the next set of buffers while after the current data filed.

Example

Code:

package TestNG;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel;
import java.nio.charset.Charset;
public class GatheringAndScattering {
private static String FILENAME = "D://January18.txt";
private static Charset charset = Charset.forName("UTF-8");
public static void main(String[] args) {
String inps1 = "Welcome To My Domain";
String inps2 = "Have a Nice Day";
gathering(inps1, inps2);
scatmethod();
}
private static void scatmethod() {
ByteBuffer buff1 = ByteBuffer.allocate(1024);
ByteBuffer buff2 = ByteBuffer.allocate(1024);
ByteBuffer inps1 = null;
ByteBuffer inps2 = null;
FileInputStream in;
try {
in = new FileInputStream(FILENAME);
ScatteringByteChannel scatter = in.getChannel();
scatter.read(new ByteBuffer[] {buff1, buff2});
buff1.position(0);
buff2.position(0);
int ln1 = buff1.asIntBuffer().get();
int ln2 = buff2.asIntBuffer().get();
System.out.println("Getting the length of the variable 1" + ln1);
System.out.println("Getting the length of the variable 2 " + ln2);
inps1 = ByteBuffer.allocate(ln1);
inps2 = ByteBuffer.allocate(ln2);
scatter.read(new ByteBuffer[] {inps1, inps2});
} catch (FileNotFoundException exObj) {
exObj.printStackTrace();
} catch (IOException ioObj) {
ioObj.printStackTrace();
}
String iinps1 = new String(buff1.array(), charset);
String iinps2 = new String(buff2.array(), charset);
System.out.println(inps1);
System.out.println(inps2);
}
private static void gathering(String inps1, String inps2) {
ByteBuffer buf1 = ByteBuffer.allocate(1024);
ByteBuffer buf2 = ByteBuffer.allocate(1024);
ByteBuffer inpss1 = ByteBuffer.wrap(inps1.getBytes());
ByteBuffer inpss2 = ByteBuffer.wrap(inps2.getBytes());
int l1 = inps1.length();
int l2 = inps2.length();
buf1.asIntBuffer().put(l1);
buf2.asIntBuffer().put(l2);
System.out.println("Length of the Gathering1= " + l1);
System.out.println("Length of the Gathering2= " + l2);
try {
FileOutputStream out = new FileOutputStream(FILENAME);
GtheringByteChannel gather = out.getChannel();
gather.write(new ByteBuffer[] {buf1, buf2, inpss1, inpss2});
out.close();
gather.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}
}

Output:

  • In the above example we used required Gathering and Scattering jar files and its classes to implement the operations.
  • It declares the string format inputs as two separate variables and store it as the separate file format and path location.
  • Then using ByteBuffer java file classes to access the user input datas that is it will read and write the datas from the file.
  • Using different methods to perform the data operations from one method to another method.
  • Try-catch block to perform the basic file operations and handling the exceptions.

Java NIO Scatter/Gather Run

Generally Right-click on the GatheringAndScattering class which helps to run the option like Run As-> Java Application from the menu that appears. The sample example also to be debugged by the programmers to observe what happens at each stage of the application.

It have two buffers in place and both have the random string and a random number will be generated and stored in separate buffers. We will utilize the file datas on the channel to read and write the data kept in both buffers on while using the GatheringByteChannel. We need to confirm that the data is stored and retrieved the data match will read the data back from the file into two different buffers using ScatteringByteChannel and output the data content in console.

Methods

There are n number of methods that helps to perform the Java NIO channel which offers a crucial stage feature called gather and scatter or vectored Input and Outputs. It’s a powerful technique and feature for writing the data bytes from a set of buffers to a stream by using write() method.

1. Scattering Reads

Data from a single data channel which helps to read and write into several buffer streams by using the same technique called scattering read. Its mainly extended by using the class called ReadableByteChannel along with the methods like read(ByteBuffer[] variablename) throws some set of Exceptions.

2. Gathering Writes

When writing data from several buffers from the channel called into one channel and to collecting write is utilised. Its mainly called the same like Scattering Read interface but extend with the WritableByteChannel class. Method called write(ByteBuffer[] variablename) throws with some exceptions.

3. ScatteringByteChannel

Its read from a variety of data channels that helps to allow a data read information from a single point of channel into a n number of buffers. Its allotted and added to an array of the buffer type. Its using the read() method and then it will receive the data input arrays and write data from the channel in which array buffers appear. The channel continues on to fill the buffer once filled.

4. GatheringByteChannel

Write across with several set of data channels that allowed us for to write the information from several buffer ranges into a single channel. A several buffer type array is added to once more with data allocation and using write() method to receive the array argument and published data array. The buffer limit ranges are set for to write the datas.

Conclusion

When used appropriately for scatter and gather which effective set of technologies that let has been assigned to developers with grunt work on the Operating System. The datas are divided into various set of buckets that compiles the code as each fragments into a single set of units which eliminating the buffer copies and amount of code.

Recommended Articles

This is a guide to Java NIO Scatter/Gather. Here we discuss the introduction, application building class, example, java NIO Scatter/Gather run and methods. You can also look at the following articles to learn more –

  1. Java NIO File
  2. Java NIO Path
  3. Java nio ByteBuffer
  4. Java NIO Channel

The post Java NIO Scatter/Gather appeared first on EDUCBA.



This post first appeared on Free Online CFA Calculator Training Course | EduCB, please read the originial post: here

Share the post

Java NIO Scatter/Gather

×

Subscribe to Free Online Cfa Calculator Training Course | Educb

Get updates delivered right to your inbox!

Thank you for your subscription

×