Java Nio 简明教程

Java NIO - Scatter

众所周知,与 Java 的传统 IO API 相比,Java NIO 是一个针对数据 IO 操作进行了更优化的 API。Java NIO 提供的另外一项支持是将数据从多个缓冲区读/写到通道。这种多读多写支持称为分散和聚集,其中多个缓冲区从读取数据时单个通道中分散,而多个缓冲区在写入数据时从单个通道中聚集。

As we know that Java NIO is a more optimized API for data IO operations as compared to the conventional IO API of Java.One more additional support which Java NIO provides is to read/write data from/to multiple buffers to channel.This multiple read and write support is termed as Scatter and Gather in which data is scattered to multiple buffers from single channel in case of read data while data is gathered from multiple buffers to single channel in case of write data.

为了实现从通道的多读多写,Java NIO 为读取和写入数据提供了 ScatteringByteChannel 和 GatheringByteChannel API,如以下示例所示。

In order to achieve this multiple read and write from channel there is ScatteringByteChannel and GatheringByteChannel API which Java NIO provides for read and write the data as illustrate in below example.

ScatteringByteChannel

Read from multiple channels - 在此处,我们从单个通道中读取数据到多个缓冲区。为此,分配多个缓冲区并将其添加到缓冲区类型数组中。然后,将此数组作为参数传递给 ScatteringByteChannel read() 方法,该方法随后以数组中缓冲区出现的顺序从通道写入数据。一个缓冲区填满后,通道将填充下一个缓冲区。

Read from multiple channels − In this we made to reads data from a single channel into multiple buffers.For this multiple buffers are allocated and are added to a buffer type array.Then this array is passed as parameter to the ScatteringByteChannel read() method which then writes data from the channel in the sequence the buffers occur in the array.Once a buffer is full, the channel moves on to fill the next buffer.

以下示例说明如何在 Java NIO 中执行数据分散。

The following example shows how scattering of data is performed in Java NIO

C:/Test/temp.txt

Hello World!
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ScatteringByteChannel;

public class ScatterExample {
   private static String FILENAME = "C:/Test/temp.txt";
   public static void main(String[] args) {
      ByteBuffer bLen1 = ByteBuffer.allocate(1024);
      ByteBuffer bLen2 = ByteBuffer.allocate(1024);
      FileInputStream in;
      try {
         in = new FileInputStream(FILENAME);
         ScatteringByteChannel scatter = in.getChannel();
         scatter.read(new ByteBuffer[] {bLen1, bLen2});
         bLen1.position(0);
         bLen2.position(0);
         int len1 = bLen1.asIntBuffer().get();
         int len2 = bLen2.asIntBuffer().get();
         System.out.println("Scattering : Len1 = " + len1);
         System.out.println("Scattering : Len2 = " + len2);
      }
      catch (FileNotFoundException exObj) {
         exObj.printStackTrace();
      }
      catch (IOException ioObj) {
         ioObj.printStackTrace();
      }
   }
}

Output

Scattering : Len1 = 1214606444
Scattering : Len2 = 0

最后可以得出结论,如果正确使用,Java NIO 中的分散/聚集方法是一种经过优化且可以多任务处理。它允许你将分离出读入多个存储桶中的数据或将不同数据块组装成一个整体的繁琐工作委托给操作系统。毫无疑问,这节省了时间,并且通过避免缓冲区副本更有效地使用了操作系统,并减少了需要编写和调试的代码量。

In last it can be concluded that scatter/gather approach in Java NIO is introduced as an optimized and multitasked when used properly.It allows you to delegate to the operating system the grunt work of separating out the data you read into multiple buckets, or assembling disparate chunks of data into a whole.No doubt this saves time and uses operating system more efficiently by avoiding buffer copies, and reduces the amount of code need to write and debug.