0
我想在使用commons compress的內存中使用bzip2數據。我想這一點:如何在內存中壓縮commons compress?
private static final int bufferSize = 8192;
public void compress(
ByteArrayInputStream byteArrayInputStream,
CompressorOutputStream compressorOutputStream) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final byte[] buffer = new byte[bufferSize];
int n = 0;
while (-1 != (n = byteArrayInputStream.read(buffer)))
compressorOutputStream.write(buffer, 0, n);
}
public byte[] compressBZIP2(byte[] inputBytes) throws Exception {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(inputBytes);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
BZip2CompressorOutputStream bZip2CompressorOutputStream = new BZip2CompressorOutputStream(byteArrayOutputStream);
compress(byteArrayInputStream, bZip2CompressorOutputStream);
return byteArrayOutputStream.toByteArray();
}
但這並不工作,
byte[] bzipCompressed = resultCompressor.compressBZIP2(contentBytes);
一切的結果總是有3個字節,而多數民衆贊成。我究竟做錯了什麼?