Running out of memory while decompressing

Hi,
I am using the code from these examples to compress/decompress a byte array:
http://www.java2s.com/Tutorial/Java/0180__File/CompressaByteArray.htm
http://www.java2s.com/Tutorial/Java/0180__File/DecompressaByteArray.htm

Compressing works fine, but I am getting a java.lang.OutOfMemoryError exception when trying to decompress the data.
The compressed data is 7kb, uncompressed is around 35kb. Surely even with the default amount of memory this should be fine? if I can compress the data shouldn’t I be able to decompress it again?

Here’s the quick class I wrote (pretty much same as the examples)
http://www.java-gaming.org/?action=pastebin&id=173

Thanks,
roland


public static byte[] compress(byte[] data) {
   ByteArrayInputStream bais = new ByteArrayInputStream(data);
   ByteArrayOutputStream baos = new ByteArrayOutputStream();

   GZipOutputStream gz = new GZipOutputStream(baos);
   copy(bais, gz);
   return baos.toByteArray();
}

public static byte[] decompress(byte[] data) {
   ByteArrayInputStream bais = new ByteArrayInputStream(data);
   ByteArrayOutputStream baos = new ByteArrayOutputStream();

   GZipInputStream gz = new GZipInputStream(baos);
   copy(gz, baos);
   return baos.toByteArray();
}

Thanks for the reply, riven.

Is my copy method wrong? it always compresses to just 10 bytes :frowning:

http://pastebin.java-gaming.org/59f8d6b7416

On GZip streams, closing the streams is mandatory. (not even flushing will do the trick)

Thanks Riven! it’s all working now :slight_smile: