提问人:davolfman 提问时间:2/3/2022 最后编辑:davolfman 更新时间:2/3/2022 访问量:63
从输入流中提取最后 n 个字节的良好通用技术有哪些
What are good general techniques to extract the last n bytes from a stream of input
问:
假设我有一个长度未知的输入字节的二进制流,以可识别的类似 EOF 的条件结尾,那么有哪些算法可以提取最后一个字节,比如 8 个字节,并以与其余输入不同的方式处理它们?假设没有任何很好的实现特定功能,例如寻找到最后,或倒带流或调用tac。假设输入可能大于 RAM,因此完全缓冲将失败。
对于文件或字段的第一个字节,这非常简单:消耗那么多字节并根据需要处理它们。但对于最后一个字节,算法似乎更容易出错。
我过去通过构建一个 2 段循环缓冲区并在确认未达到 EOF 后才处理最旧的段来做到这一点。
while not end of STREAM
read n bytes from STREAM and place in open segment
set m to number of bytes actually read
if end of STREAM
o = n - m
process first m bytes normally from older segment
process last o bytes from older segment specially
process first m bytes from newer segment specially
else
process n bytes from older segment normally
declare older segment open
这不仅仅是由极端情况组成,它是由极端情况组成的。 有没有其他算法可以更安全地避免实现错误?或者有没有更好的方法来做这个算法?
答: 暂无答案
评论