java - netty - 如何将 ChannelBuffer 类型保存到文件中?

标签 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); 
   }   

但是由于文件流是二进制的,写入速度好像真的很慢!有什么办法可以真正快速地将 ChannelBuffer 保存到文件中吗?

最佳答案

尝试将整个缓冲区写入文件。本示例代码来自netty文件上传app .

    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();

关于java - netty - 如何将 ChannelBuffer 类型保存到文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10467408/

相关文章:

sockets - 将 SO_LINGER 与 udp 套接字一起使用是否有效?

java - mac地址问题

C# 网络 API

python - 如何更改 TLS 上下文选项

c++ - 内存池和缓冲区 C++

java - 安卓 Firebase Firestore : Get data from deep in Firestore document

java - 如果文件具有 unix 或 dos 样式的行结尾,eclipse 如何计算?

java - Singleton作为RecyclerView.Adapter的数据源

java - 和引擎警报对话框错误

ios - 修复我的网络事件指示器