Search code examples
javanettyapache-commonsjava-ioapache-commons-io

How do I use a ChannelBufferOutputStream to check compression size


In a java program I am compressing an InputStream like this:

ChannelBufferOutputStream outputStream = new ChannelBufferOutputStream(ChannelBuffers.dynamicBuffer(BUFFER_SIZE));
GZIPOutputStream compressedOutputStream = new GZIPOutputStream(outputStream);
try {
   IOUtils.copy(inputStream, compressedOutputStream);
} finally {
   // this should print the byte size after compression
   System.out.println(outputStream.writtenBytes());         
}

I am testing this code with a json file that is ~31.000 byte uncompressed and ~7.000 byte compressed on disk. Sending a InputStream that is wrapping the uncompressed json file to the code above, outputStream.writtenBytes() returns 10 which would indicate that it compressed down to only 10 byte. That seems wrong, so I wonder where the problem is. ChannelBufferOutputStream javadoc says: Returns the number of written bytes by this stream so far. So it should be working.


Solution

    1. Try calling GZIPOutputStream.finish() or flush() methods before counting bytes
    2. If that does not work, you can create a proxy stream, whose mission - to count the number of bytes that have passed through it