Java-nio-gather
Java NIO-収集
Java NIOは、従来のJavaのIO APIと比較して、データIO操作用に最適化されたAPIであることがわかっているため、Java NIOが提供するもう1つの追加サポートは、複数のバッファーからチャネルへのデータの読み取り/書き込みです。書き込みサポートはScatter and Gatherと呼ばれ、データは読み取りデータの場合は単一チャネルから複数のバッファーに分散され、データは書き込みデータの場合は複数のバッファーから単一チャネルに収集されます。
チャネルからのこの複数の読み取りおよび書き込みを実現するために、ScatteringByteChannelおよびGatheringByteChannel APIがあります。JavaNIOは、以下の例に示すように、データの読み取りおよび書き込みを提供します。
GatheringByteChannel
複数のチャネルへの書き込み-これでは、複数のバッファから単一のチャネルにデータを書き込むようにしました。この場合も、複数のバッファが割り当てられ、バッファタイプの配列に追加されます。この配列は、パラメータとしてGatheringByteChannel write( )配列内のバッファが発生する順序で複数のバッファからデータを書き込むメソッド。ここで覚えておくべき1つのポイントは、バッファの位置と制限の間のデータのみが書き込まれることです。
次の例は、Java NIOでのデータ収集の実行方法を示しています
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.GatheringByteChannel;
public class GatherExample {
private static String FILENAME = "C:/Test/temp.txt";
public static void main(String[] args) {
String stream1 = "Gather data stream first";
String stream2 = "Gather data stream second";
ByteBuffer bLen1 = ByteBuffer.allocate(1024);
ByteBuffer bLen2 = ByteBuffer.allocate(1024);
//Next two buffer hold the data we want to write
ByteBuffer bstream1 = ByteBuffer.wrap(stream1.getBytes());
ByteBuffer bstream2 = ByteBuffer.wrap(stream2.getBytes());
int len1 = stream1.length();
int len2 = stream2.length();
//Writing length(data) to the Buffer
bLen1.asIntBuffer().put(len1);
bLen2.asIntBuffer().put(len2);
System.out.println("Gathering : Len1 = " + len1);
System.out.println("Gathering : Len2 = " + len2);
//Write data to the file
try {
FileOutputStream out = new FileOutputStream(FILENAME);
GatheringByteChannel gather = out.getChannel();
gather.write(new ByteBuffer[] {bLen1, bLen2, bstream1, bstream2});
out.close();
gather.close();
}
catch (FileNotFoundException exObj) {
exObj.printStackTrace();
}
catch(IOException ioObj) {
ioObj.printStackTrace();
}
}
}
出力
Gathering : Len1 = 24
Gathering : Len2 = 25
最後に、Java NIOのスキャッター/ギャザーアプローチは、適切に使用すると最適化されたマルチタスクとして導入されたと結論付けることができます。データの塊を全体に分散させることは間違いありません。これにより、バッファコピーを回避することで時間を節約し、オペレーティングシステムをより効率的に使用し、書き込みとデバッグに必要なコードの量を削減できます。