NIOライブラリで遊んでいます。ポート8888で接続をリッスンしようとしています。接続が受け入れられたら、そのチャネルからにすべてをダンプしますsomefile
。
でそれを行う方法は知っていますがByteBuffers
、非常に効率的だと言われているで動作させたいと思いますFileChannel.transferFrom
。
これは私が得たものです:
ServerSocketChannel ssChannel = ServerSocketChannel.open();
ssChannel.socket().bind(new InetSocketAddress(8888));
SocketChannel sChannel = ssChannel.accept();
FileChannel out = new FileOutputStream("somefile").getChannel();
while (... sChannel has not reached the end of the stream ...) <-- what to put here?
out.transferFrom(sChannel, out.position(), BUF_SIZE);
out.close();
だから、私の質問は、「transferFrom
ストリームの終わりに達するまで、いくつかのチャネル」をどのように表現するのですか?
編集:使用されるバッファのサイズは質問とは無関係であるため、1024をBUF_SIZEに変更しました。