Question

Can anybody tell me how to decompress a byte array?

Here is my code. I have been trying it for ages and it is giving me a DataFormatException.

byte bArray[] = new byte[tSizeOfTile];
input.read(bArray, offset, limit);

byte[] unComp = new byte[bArray.length + 100];

Inflater inflate = new Inflater();
inflate.setInput(bArray);
inflate.inflate(unComp, offset, bArray.length + 100);
inflate.end();
Was it helpful?

Solution

Well, one definite issue is that you're apparently using InputStream.read without checking the return value. That means you may have read less data than you expected to.

Also, you're trying to inflate into unComp from the offset, but with a maximum length being the same as unComp.length. That means if offset is anything other than 0, you could be trying to write past the end of the array.

Did you mean the offset to refer to the input array? I don't believe that's what it means. You should use setInput to only provide input data.

However, you could make all of this a lot easier for yourself by using InflaterInputStream instead of handling Inflate yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top