Java:从流更新 CRC32
我是否误解了使用 CRC32
或 CheckedInputStream
类通过不断更新最新输入来计算校验和?当输入 <= 128KiB 时,会生成有效的 CRC32。任何大于 128KiB 的内容都会导致校验和失败。下面是我正在使用的一些代码(使用 CRC32
对象和 BufferedInputStream
但如果我使用 CheckedInputStream
来跟踪,也会发生同样的问题CRC32)。
我将不胜感激任何建议或意见,谢谢
private static long calcCRC32() throws IOException {
BufferedInputStream inStream = new BufferedInputStream(System.in);
int BLOCK_SIZE = 128*1024; //128KiB
int len;
byte[] buffer = new byte[BLOCK_SIZE];
CRC32 crc32 = new CRC32();
crc32.reset();
while((len = bufferedInputStream.read(buffer, 0, BLOCK_SIZE)) > 0){
crc32.update(buffer, 0, len);
buffer = new byte[BLOCK_SIZE];
}
return crc32.getValue();
}
Am I misunderstanding the use of either of the CRC32
or CheckedInputStream
classes to calculate a checksum by continuously updating with the latest input? When the input is <= 128KiB a valid CRC32 is generated. Anything larger than 128KiB and the checksum fails. Below is some code I am working with (using a CRC32
object and BufferedInputStream
but the same problem happens if I use a CheckedInputStream
to keep track of the CRC32).
I would appreciate any advice or comments, thank you
private static long calcCRC32() throws IOException {
BufferedInputStream inStream = new BufferedInputStream(System.in);
int BLOCK_SIZE = 128*1024; //128KiB
int len;
byte[] buffer = new byte[BLOCK_SIZE];
CRC32 crc32 = new CRC32();
crc32.reset();
while((len = bufferedInputStream.read(buffer, 0, BLOCK_SIZE)) > 0){
crc32.update(buffer, 0, len);
buffer = new byte[BLOCK_SIZE];
}
return crc32.getValue();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论