@Signature public void __construct(Environment env, InputStream inputStream, boolean framed) throws IOException { this.inputStream = framed ? new FramedLZ4CompressorInputStream(inputStream) : new BlockLZ4CompressorInputStream(inputStream); } }
private int readOnce(byte[] b, int off, int len) throws IOException { if (inUncompressed) { int cnt = currentBlock.read(b, off, len); count(cnt); return cnt; } BlockLZ4CompressorInputStream l = (BlockLZ4CompressorInputStream) currentBlock; long before = l.getBytesRead(); int cnt = currentBlock.read(b, off, len); count(l.getBytesRead() - before); return cnt; }
return -1; case NO_BLOCK: // NOSONAR - fallthrough intended readSizes(); int litLen = readLiteral(b, off, len); if (!hasMoreDataInBlock()) { state = State.LOOKING_FOR_BACK_REFERENCE; return litLen > 0 ? litLen : read(b, off, len); case LOOKING_FOR_BACK_REFERENCE: // NOSONAR - fallthrough intended if (!initializeBackReference()) { state = State.EOF; return -1; int backReferenceLen = readBackReference(b, off, len); if (!hasMoreDataInBlock()) { state = State.NO_BLOCK; return backReferenceLen > 0 ? backReferenceLen : read(b, off, len); default: throw new IOException("Unknown stream state " + state);
private void readSizes() throws IOException { int nextBlock = readOneByte(); if (nextBlock == -1) { throw new IOException("Premature end of stream while looking for next block"); } nextBackReferenceSize = nextBlock & BACK_REFERENCE_SIZE_MASK; long literalSizePart = (nextBlock & LITERAL_SIZE_MASK) >> SIZE_BITS; if (literalSizePart == BACK_REFERENCE_SIZE_MASK) { literalSizePart += readSizeBytes(); } startLiteral(literalSizePart); state = State.IN_LITERAL; }
/** * @return false if there is no more back-reference - this means this is the * last block of the stream. */ private boolean initializeBackReference() throws IOException { int backReferenceOffset = 0; try { backReferenceOffset = (int) ByteUtils.fromLittleEndian(supplier, 2); } catch (IOException ex) { if (nextBackReferenceSize == 0) { // the last block has no back-reference return false; } throw ex; } long backReferenceSize = nextBackReferenceSize; if (nextBackReferenceSize == BACK_REFERENCE_SIZE_MASK) { backReferenceSize += readSizeBytes(); } // minimal match length 4 is encoded as 0 startBackReference(backReferenceOffset, backReferenceSize + 4); state = State.IN_BACK_REFERENCE; return true; }
private void nextBlock() throws IOException { maybeFinishCurrentBlock(); long len = ByteUtils.fromLittleEndian(supplier, 4); boolean uncompressed = (len & UNCOMPRESSED_FLAG_MASK) != 0; int realLen = (int) (len & (~UNCOMPRESSED_FLAG_MASK)); if (realLen == 0) { verifyContentChecksum(); if (!decompressConcatenated) { endReached = true; } else { init(false); } return; } InputStream capped = new BoundedInputStream(in, realLen); if (expectBlockChecksum) { capped = new ChecksumCalculatingInputStream(blockHash, capped); } if (uncompressed) { inUncompressed = true; currentBlock = capped; } else { inUncompressed = false; BlockLZ4CompressorInputStream s = new BlockLZ4CompressorInputStream(capped); if (expectBlockDependency) { s.prefill(blockDependencyBuffer); } currentBlock = s; } }
private long readSizeBytes() throws IOException { long accum = 0; int nextByte; do { nextByte = readOneByte(); if (nextByte == -1) { throw new IOException("Premature end of stream while parsing length"); } accum += nextByte; } while (nextByte == 255); return accum; }
private void readSizes() throws IOException { int nextBlock = readOneByte(); if (nextBlock == -1) { throw new IOException("Premature end of stream while looking for next block"); } nextBackReferenceSize = nextBlock & BACK_REFERENCE_SIZE_MASK; long literalSizePart = (nextBlock & LITERAL_SIZE_MASK) >> SIZE_BITS; if (literalSizePart == BACK_REFERENCE_SIZE_MASK) { literalSizePart += readSizeBytes(); } startLiteral(literalSizePart); state = State.IN_LITERAL; }
private void nextBlock() throws IOException { maybeFinishCurrentBlock(); long len = ByteUtils.fromLittleEndian(supplier, 4); boolean uncompressed = (len & UNCOMPRESSED_FLAG_MASK) != 0; int realLen = (int) (len & (~UNCOMPRESSED_FLAG_MASK)); if (realLen == 0) { verifyContentChecksum(); if (!decompressConcatenated) { endReached = true; } else { init(false); } return; } InputStream capped = new BoundedInputStream(in, realLen); if (expectBlockChecksum) { capped = new ChecksumCalculatingInputStream(blockHash, capped); } if (uncompressed) { inUncompressed = true; currentBlock = capped; } else { inUncompressed = false; BlockLZ4CompressorInputStream s = new BlockLZ4CompressorInputStream(capped); if (expectBlockDependency) { s.prefill(blockDependencyBuffer); } currentBlock = s; } }
/** * @return false if there is no more back-reference - this means this is the * last block of the stream. */ private boolean initializeBackReference() throws IOException { int backReferenceOffset = 0; try { backReferenceOffset = (int) ByteUtils.fromLittleEndian(supplier, 2); } catch (IOException ex) { if (nextBackReferenceSize == 0) { // the last block has no back-reference return false; } throw ex; } long backReferenceSize = nextBackReferenceSize; if (nextBackReferenceSize == BACK_REFERENCE_SIZE_MASK) { backReferenceSize += readSizeBytes(); } // minimal match length 4 is encoded as 0 startBackReference(backReferenceOffset, backReferenceSize + 4); state = State.IN_BACK_REFERENCE; return true; }
private long readSizeBytes() throws IOException { long accum = 0; int nextByte; do { nextByte = readOneByte(); if (nextByte == -1) { throw new IOException("Premature end of stream while parsing length"); } accum += nextByte; } while (nextByte == 255); return accum; }
return -1; case NO_BLOCK: // NOSONAR - fallthrough intended readSizes(); int litLen = readLiteral(b, off, len); if (!hasMoreDataInBlock()) { state = State.LOOKING_FOR_BACK_REFERENCE; return litLen > 0 ? litLen : read(b, off, len); case LOOKING_FOR_BACK_REFERENCE: // NOSONAR - fallthrough intended if (!initializeBackReference()) { state = State.EOF; return -1; int backReferenceLen = readBackReference(b, off, len); if (!hasMoreDataInBlock()) { state = State.NO_BLOCK; return backReferenceLen > 0 ? backReferenceLen : read(b, off, len); default: throw new IOException("Unknown stream state " + state);
private int readOnce(byte[] b, int off, int len) throws IOException { if (inUncompressed) { int cnt = currentBlock.read(b, off, len); count(cnt); return cnt; } BlockLZ4CompressorInputStream l = (BlockLZ4CompressorInputStream) currentBlock; long before = l.getBytesRead(); int cnt = currentBlock.read(b, off, len); count(l.getBytesRead() - before); return cnt; }
/** * @param stream the stream to read from, should be buffered */ @Override public CompressorInputStream getCompressorStream(InputStream stream) throws IOException { return framed ? new FramedLZ4CompressorInputStream(stream) : (CompressorInputStream) new BlockLZ4CompressorInputStream(stream); }
/** * @param stream the stream to read from, should be buffered * @param decompressConcatenated * if true, decompress until the end of the * input; if false, stop after the first * stream */ @Override public CompressorInputStream getCompressorStream(InputStream stream, boolean decompressConcatenated) throws IOException { if (!framed && decompressConcatenated) { throw new UnsupportedOperationException("only the frame format supports" + " concatenated streams"); } return framed ? new FramedLZ4CompressorInputStream(stream, decompressConcatenated) : (CompressorInputStream) new BlockLZ4CompressorInputStream(stream); }