2

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.

4

1 回答 1

3
  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
于 2012-12-15T16:55:53.533 回答