The problem is that you're only flushing the stream - which doesn't necessarily mean there's no more data to come, which can affect the decompression.
If you change both of your flush()
calls to close()
, you'll see you get the appropriate data back... or as you're using a try-with-resources statement, just let that close the inner stream, and wait until after that to call toByteArray
:
try (ByteArrayOutputStream buffer = new ByteArrayOutputStream())
{
try (DeflaterOutputStream stream = new DeflaterOutputStream(buffer))
{
stream.write(testdata);
}
compressed = buffer.toByteArray();
System.out.println("Compressed data: " + Arrays.toString(compressed));
}
catch (IOException e)
{
System.out.println("IOException during compression.");
}
(Ditto when decompressing.)