use of org.tukaani.xz.LZMAOutputStream in project hbase by apache.
the class LzmaCompressor method compress.
@Override
public int compress(byte[] b, int off, int len) throws IOException {
// remaining, provide them to the caller.
if (outBuf.hasRemaining()) {
int remaining = outBuf.remaining(), n = Math.min(remaining, len);
outBuf.get(b, off, n);
LOG.trace("compress: {} bytes from outBuf", n);
return n;
}
// We don't actually begin compression until our caller calls finish().
if (finish) {
if (inBuf.position() > 0) {
inBuf.flip();
int uncompressed = inBuf.remaining();
// If we don't have enough capacity in our currently allocated output buffer,
// allocate a new one which does.
int needed = maxCompressedLength(uncompressed);
// Can we decompress directly into the provided array?
ByteBuffer writeBuffer;
boolean direct = false;
if (len <= needed) {
writeBuffer = ByteBuffer.wrap(b, off, len);
direct = true;
} else {
if (outBuf.capacity() < needed) {
needed = CompressionUtil.roundInt2(needed);
LOG.trace("compress: resize outBuf {}", needed);
outBuf = ByteBuffer.allocate(needed);
} else {
outBuf.clear();
}
writeBuffer = outBuf;
}
int oldPos = writeBuffer.position();
// that inefficiencies here may not matter.
try (ByteBufferOutputStream lowerOut = new ByteBufferOutputStream(writeBuffer) {
@Override
protected // do not want that behavior here.
void checkSizeAndGrow(int extra) {
long capacityNeeded = curBuf.position() + (long) extra;
if (capacityNeeded > curBuf.limit()) {
throw new BufferOverflowException();
}
}
}) {
try (LZMAOutputStream out = new LZMAOutputStream(lowerOut, lzOptions, uncompressed, ARRAY_CACHE)) {
out.write(inBuf.array(), inBuf.arrayOffset(), uncompressed);
}
}
int written = writeBuffer.position() - oldPos;
bytesWritten += written;
inBuf.clear();
LOG.trace("compress: compressed {} -> {}", uncompressed, written);
finished = true;
outBuf.flip();
if (!direct) {
int n = Math.min(written, len);
outBuf.get(b, off, n);
LOG.trace("compress: {} bytes", n);
return n;
} else {
LOG.trace("compress: {} bytes direct", written);
return written;
}
} else {
finished = true;
}
}
LOG.trace("No output");
return 0;
}
Aggregations