rzeznik
rzeznik

Reputation: 91

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

Upvotes: 0

Views: 290

Answers (3)

rzeznik
rzeznik

Reputation: 91

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?

Upvotes: 0

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

Upvotes: 1

Filippo Fratoni
Filippo Fratoni

Reputation: 399

Try with a different compression level or using the nowrap options

Upvotes: 1

Related Questions