}
}
@Override
public long decompress(InputStream is, OutputStream os, long maxLength, long maxCheckSizeBytes) throws IOException, CompressionOutputSizeException {
BZip2CompressorInputStream bz2is = new BZip2CompressorInputStream(HeaderStreams.augInput(BZ_HEADER, is));
long written = 0;
int bufSize = 32768;
if(maxLength > 0 && maxLength < bufSize)
bufSize = (int)maxLength;
byte[] buffer = new byte[bufSize];
while(true) {
int expectedBytesRead = (int) Math.min(buffer.length, maxLength - written);
// We can over-read to determine whether we have over-read.
// We enforce maximum size this way.
// FIXME there is probably a better way to do this!
int bytesRead = bz2is.read(buffer, 0, buffer.length);
if(expectedBytesRead < bytesRead) {
Logger.normal(this, "expectedBytesRead="+expectedBytesRead+", bytesRead="+bytesRead+", written="+written+", maxLength="+maxLength+" throwing a CompressionOutputSizeException");
if(maxCheckSizeBytes > 0) {
written += bytesRead;
while(true) {
expectedBytesRead = (int) Math.min(buffer.length, maxLength + maxCheckSizeBytes - written);
bytesRead = bz2is.read(buffer, 0, expectedBytesRead);
if(bytesRead <= -1) throw new CompressionOutputSizeException(written);
if(bytesRead == 0) throw new IOException("Returned zero from read()");
written += bytesRead;
}
}