简体   繁体   English

如何获取输入流的CRC并返回此副本

[英]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. 我在crcUtils中有方法可以从字节数组中计算出来。 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 ? 那么有没有选择如何获得crc和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: 读取流时,使用CheckedInputStream计算CRC:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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