@Override public int compress(byte[] src, int srcOff, int srcLen, byte[] dst, int dstOff) throws DataCorruptionException { try { if (dstStream == null) { dstStream = new NoCopyByteArrayOutputStream(dst, dstOff); } else { dstStream.reuse(dst, dstOff); } BZip2CompressorOutputStream compressStream = new BZip2CompressorOutputStream( dstStream, BZip2CompressorOutputStream.chooseBlockSize(srcLen)); compressStream.write(src, srcOff, srcLen); compressStream.close(); int compressedLen = dstStream.getNumBytesWritten(); dstStream.close(); if (compressedLen > (dst.length - dstOff)) { throw new InsufficientBufferException("destination buffer remains " + (dst.length - dstOff) + " bytes, requires " + compressedLen + " bytes."); } return compressedLen; } catch (IOException e) { throw new DataCorruptionException(e); } } }