che*_*gpc 2 java networking network-programming netty
我试过使用这段代码:
saver=new FileOutputStream(file);
byte c;
while ( content.readable() ){ //content is a ChannelBuffer type
c = content.readByte();
saver.write(c);
}
Run Code Online (Sandbox Code Playgroud)
但由于文件流是二进制的,写入速度似乎很慢!有没有办法将ChannelBuffer快速保存到文件中?
尝试将整个缓冲区写入文件.此示例代码来自netty文件上传应用程序.
FileOutputStream outputStream = new FileOutputStream(file);
FileChannel localfileChannel = outputStream.getChannel();
ByteBuffer byteBuffer = buffer.toByteBuffer();
int written = 0;
while (written < size) {
written += localfileChannel.write(byteBuffer);
}
buffer.readerIndex(buffer.readerIndex() + written);
localfileChannel.force(false);
localfileChannel.close();
Run Code Online (Sandbox Code Playgroud)