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!
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.