0

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. 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
  • It seems that the compressed data can't pass the Adler32 check. The simplest solution is to use nowrap option when initializing Inflater and remove the first two bytes in compressed data. Commented Jun 14, 2021 at 9:17

3 Answers 3

1

Try with a different compression level or using the nowrap options

Sign up to request clarification or add additional context in comments.

Comments

1

1 Some warning: do you use the same algorithm in both sides ?

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.

like this: Using Java Deflater/Inflater with custom dictionary causes 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

hope it helps

1 Comment

problem is that i do not compress it, i have just compressed value via Go lang
0

I found out why its happening byte 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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.