简体   繁体   中英

How to get crc of input stream and return clone of this is

I have one input stream. First I need to get crc of this is what I know how to do eg:

 CRCUtils.getCRC32(IOUtils.toByteArray(is))

I have method in crcUtils which can calculate it from byte array. But then I need this input stream again and I cant use previous one because it reach end. So is there option how to get crc and clone of is ?

You can do it as such:

byte[] byteArr = IOUtils.toByteArray(is);
CRCUtils.getCRC32(byteArr);
InputStream is1 = new ByteArrayInputStream(byteArr); 

您可以在输入流中使用标记 (如果支持)。

如果在处理文件之前不需要CRC值,则还可以创建FilteredInputStream的子类,该子类将读取的所有内容传递给CRC32,并提供一些额外的方法来获取CRC值。

Use CheckedInputStream to compute the CRC while reading a stream:

CRC32 crc = new CRC32();
try (InputStream in = new CheckedInputStream(new FileInputStream("myfile"), crc)) {
    // read the input ...
}
// now crc contains the checksum of the input
long crc32 = crc.getValue();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM