0

zeroCopy 操作で Channels.newChannel(OutputStream/InputStream) を使用することに疑問があります。zeroCopy として機能しますか。最初のヘッダー部分(ファイルとユーザー関連情報)を送信してからファイルコンテンツを送信する必要があるなど、いくつかの制限があります。テストのために、BufferedOutputStreamもオーバーライドしますが、fileChannel.transferTo呼び出しでオーバーライドメソッドを呼び出しています...そのような状況(ヘッダー+コンテンツ)でzeroCopyを達成する方法を教えてください。

テストコードの一部:

String host = "127.0.0.1";
SocketAddress sad = new InetSocketAddress(host, ZeroCopyServer.PORT);
Socket s=new Socket();
s.connect(sad);

OutputStream o=s.getOutputStream();
OutputStream out=new BufferedOutputStream(o);
WritableByteChannel ch=Channels.newChannel(o);
//can i use
//WritableByteChannel ch=Channels.newChannel(out);

String msg="Hi how are you and what are you doing...";
out.write(msg.getBytes());
out.flush();

String fname = "hello.txt";
String fname2 = "input";
long fileSize = new File(fname).length();

FileChannel fc = new FileInputStream(fname).getChannel();
FileChannel fc2 = new FileInputStream(fname2).getChannel();

fc.transferTo(0, fc.size(), ch);
fc2.transferTo(0, fc2.size(), ch);

fc.close();
fc2.close();
out.close();
ch.close();
4

1 に答える 1