first some code:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
CBZip2OutputStream zos = new CBZip2OutputStream(bos);
provider.sendXMLFilelist(zos);
zos.flush();
bos.flush();
length = bos.size();
"provider" send (lets say) 200 bytes to "zos". But length
is == 1. I know bzip is good, but 1 byte seems a litte less.
When I do: provider.sendXMLFilelist(bos);
than length is == 200.
Why doesn't CBZip2OutputStream output all of his compresse开发者_运维技巧d bytes?
I'm using this implementation: http://www.kohsuke.org/bzip2/
I think i now have the answer. you have to .close()
the bzip2 stream. Bzip2 ist a block codec and it doesn't know if it has to pad the data or if there is more.
So by telling him to close the stream brings him to output all of its compressed data.
I've had issues with this library implementation in the past, I'd recommend you to look at another bzip2 implementation from Apache commons. Migration shouldn't be complex (replace CBZip2OutputStream -> BZip2CompressorOutputStream and you should go).
精彩评论