gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:06:57 25 4
gpt4 key购买 nike

我试过使用这段代码:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com