gpt4 book ai didi

java - 如何将 File 对象转换为 Netty ByteBuffer

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:44:46 25 4
gpt4 key购买 nike

有没有办法从 java File 对象中获取 netty ByteBuf?我正在尝试使用自定义 Snappy验证校验和的类。下面是我的代码,我想在 bytebuf 变量中获取 File 对象。

import io.netty.buffer.ByteBuf;

File file = new File("boot.zip");
ByteBuf byteBuf = Unpooled.buffer(128);
byteBuf.writeBytes(new FileInputStream(file));
int expectedChecksum = Snappy.calculateChecksum(byteBuf);
logger.info("checksum = "+expectedChecksum);

boolean checksumValid = true;
try {
CustomSnappy.validateChecksum(expectedChecksum, byteBuf);
} catch(DecompressionException e) {
checksumValid = false;
e.printStackTrace();
logger.error("DecompressionException in checksum calculation " + e);
} catch(Exception e) {
checksumValid = false;
e.printStackTrace();
logger.error("Exception in checksum calculation " + e);
}

我使用了 Unpooled.buffer(128) 但它给了我 No signature of method :io.netty.buffer.UnpooledHeapByteBuf.writeBytes() is applicable for argument types: (java.io.FileInputStream) values: [ java.io.FileInputStream@28b6520b]

最佳答案

您可以从 FileChannel 创建一个 MappedByteBuf 并通过 Unpooled.wrappedBuffer(..) 将其包装在 ByteBuffer 中。

关于java - 如何将 File 对象转换为 Netty ByteBuffer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27582881/

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