0

With the help of this thread I was able to write a decompress() and a compress() function. My programme receives data in gzipped form, inflates it, sometimes modifies it, then re-compresses it again and sends it along. After hours of headache and bug tracing, I was able to find out that sometimes(!), the GZIPOutputStream that I use just won't close properly. I am able to flush it but after that, nothing happens. However, at other times, it simply works >.>

This is the code of my compress() method.

public static byte[] compress(String data) throws IOException {
    try (PipedInputStream pipedIn = new PipedInputStream();
            GZIPOutputStream compressor= new GZIPOutputStream(new PipedOutputStream(pipedIn))) {
        compressor.write(data.getBytes("UTF-8"));
        compressor.flush();
        compressor.close();

        // Sometimes does not reach this point.

        return IOUtils.toByteArray(pipedIn);
    }
}

For debugging purposes, I added a few System.Outs. The console output was as follows:

------------------------------------
Decompressing ...
compressed byte count:   628
uncompressed byte count: 1072
------------------------------------
Compressing ...
uncompressed byte count: 1072
compressed byte count:   628
------------------------------------
>>> That worked!
------------------------------------
Decompressing ...
compressed byte count:   526
uncompressed byte count: 2629
------------------------------------
uncompressed byte count: 2629
compressed byte count:   526
------------------------------------
>>> That worked!
------------------------------------
Decompressing ...
compressed byte count:   1888
uncompressed byte count: 6254
------------------------------------

After that, nothing happens. Any help with this problem would be greatly appreciated!

MightyMalcolm
  • 191
  • 1
  • 16

1 Answers1

1

There is probably nothing wrong with your usage of GZIP streams; rather it is the way you use the Piped streams. These are meant for interthread communication and will block when you use them your way.

Instead use a ByteArrayOutptuStream to capture the output.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • Could you give me a short example of how to do that? I wanted to do it in the first place, but I don't know how to get the data from the `OutputStream` into the `InputStream`. *EDIT: if all fails, I will just open another thread to pipe the info. – MightyMalcolm Oct 27 '15 at 13:46
  • 1
    [`ByteArrayOutputStream#toByteArray()`](http://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayOutputStream.html#toByteArray()) is all you need. – Marko Topolnik Oct 27 '15 at 13:48