【发布时间】:2018-10-23 00:50:53
【问题描述】:
目标:从一个来源解密数据并将解密后的数据写入文件。
try (FileInputStream fis = new FileInputStream(targetPath.toFile());
ReadableByteChannel channel = newDecryptedByteChannel(path, associatedData))
{
FileChannel fc = fis.getChannel();
long position = 0;
while (position < ???)
{
position += fc.transferFrom(channel, position, CHUNK_SIZE);
}
}
newDecryptedByteChannel(Path,byte[]) 的实现应该没什么意思,它只是返回一个 ReadableByteChannel。
问题:结束while循环的条件是什么?何时到达“字节通道结束”? transferFrom 是这里的正确选择吗?
This question 可能是相关的(答案是将计数设置为Long.MAX_VALUE)。不幸的是,这对我没有帮助,因为文档说最多可以传输 count 个字节,具体取决于通道的性质和状态。
另一个想法是只检查实际传输的字节数是否为 0(从transferFrom 返回),但如果源通道是非阻塞的并且在其中立即可用的字节数少于 count,则此条件可能为真输入缓冲区。
【问题讨论】: