简体   繁体   English

Java解压缩字节数组-错误的数据检查

[英]Java Decompressing byte array - incorrect data check

I have a little problem: I decompress byte array and everything is ok with following code but sometimes with some data it throws DataFormatException with incorrect data check. 我有一个小问题:我解压缩了字节数组,使用以下代码可以正常工作,但有时对于某些数据,它会抛出DataFormatException并带有不正确的数据检查。 Any ideas? 有任何想法吗?

 private byte[] decompress(byte[] compressed) throws DecoderException {
    Inflater decompressor = new Inflater();
    decompressor.setInput(compressed);
    ByteArrayOutputStream outPutStream = new ByteArrayOutputStream(compressed.length);
    byte temp [] = new byte[8196];
    while (!decompressor.finished()) {

        try {
            int count = decompressor.inflate(temp);
            logger.info("count = " + count);
            outPutStream.write(temp, 0, count);
        }
        catch (DataFormatException e) {
            logger.info(e.getMessage());
            throw new DecoderException("Wrong format", e);
        }
    }
    try {
        outPutStream.close();
    } catch (IOException e) {
        throw new DecoderException("Cant close outPutStream ", e);
    }
    return outPutStream.toByteArray();
}

1 Some warning: do you use the same algorithm in both sides ? 1警告:双方是否使用相同的算法?

do you use bytes ? 你用字节吗? (not String) (不是字符串)

your arrays have the good sizes ? 您的数组大小合适吗?

2 I suggest you check step by step, catching exceptions, checking sizes, null, and comparing bytes. 2我建议您逐步检查,捕获异常,检查大小,空值和比较字节。

like this: Using Java Deflater/Inflater with custom dictionary causes IllegalArgumentException 像这样: 将Java Deflater / Inflater与自定义词典一起使用会导致IllegalArgumentException

  • Take your input 接受您的输入

  • Compress it 压缩它

  • copy your bytes 复制您的字节

  • decompress them 解压它们

  • compare output with input 将输出与输入进行比较

3 if you cant find, take another example which works, and modify it step by step 3如果找不到,请举另一个可行的示例,然后逐步进行修改

hope it helps 希望能帮助到你

尝试使用其他压缩级别或使用nowrap选项

I found out why its happening byte temp [] = new byte[8196]; 我发现了为什么它发生的字节temp [] = new byte [8196]; its too big, it must be exactly size of decompressed array cause it was earlier Base64 encoded, how i can get this size before decompressing it? 它太大,它必须正好是解压缩数组的大小,因为它是较早的Base64编码的,我如何在解压缩之前得到这个大小?

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

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