protected LZWInputStream(final InputStream inputStream, final ByteOrder byteOrder) { this.in = new BitInputStream(inputStream, byteOrder); }
@Override int available() throws IOException { return (int) Math.min(blockLength - read, reader.bitsAvailable() / Byte.SIZE); } }
@Override int read(byte[] b, int off, int len) throws IOException { // as len is an int and (blockLength - read) is >= 0 the min must fit into an int as well int max = (int) Math.min(blockLength - read, len); int readSoFar = 0; while (readSoFar < max) { int readNow; if (reader.bitsCached() > 0) { byte next = (byte) readBits(Byte.SIZE); b[off + readSoFar] = memory.add(next); readNow = 1; } else { readNow = in.read(b, off + readSoFar, max - readSoFar); if (readNow == -1) { throw new EOFException("Truncated Deflate64 Stream"); } memory.add(b, off + readSoFar, readNow); } read += readNow; readSoFar += readNow; } return max; }
/** * Returns at most 63 bits read from the underlying stream. * * @param count the number of bits to read, must be a positive * number not bigger than 63. * @return the bits concatenated as a long using the stream's byte order. * -1 if the end of the underlying stream has been reached before reading * the requested number of bits * @throws IOException on error */ public long readBits(final int count) throws IOException { if (count < 0 || count > MAXIMUM_CACHE_SIZE) { throw new IllegalArgumentException("count must not be negative or greater than " + MAXIMUM_CACHE_SIZE); } if (ensureCache(count)) { return -1; } if (bitsCachedSize < count) { return processBitsGreater57(count); } return readCachedBits(count); }
private int readNextByte(BitInputStream in) throws IOException { long b = in.readBits(8); return (int) b; }
@Override public void close() throws IOException { final BitInputStream inShadow = this.bin; if (inShadow != null) { try { inShadow.close(); } finally { this.data = null; this.bin = null; } } }
private void reAlignReading() throws IOException { // "compress" works in multiples of 8 symbols, each codeBits bits long. // When codeBits changes, the remaining unused symbols in the current // group of 8 are still written out, in the old codeSize, // as garbage values (usually zeroes) that need to be skipped. long codeReadsToThrowAway = 8 - (totalCodesRead % 8); if (codeReadsToThrowAway == 8) { codeReadsToThrowAway = 0; } for (long i = 0; i < codeReadsToThrowAway; i++) { readNextCode(); } in.clearBitCache(); }
private void switchToUncompressedState() throws IOException { reader.alignWithByteBoundary(); long bLen = readBits(16); long bNLen = readBits(16); if (((bLen ^ 0xFFFF) & 0xFFFF) != bNLen) { //noinspection DuplicateStringLiteralInspection throw new IllegalStateException("Illegal LEN / NLEN values"); } state = new UncompressedState(bLen); }
/** * Drops bits until the next bits will be read from a byte boundary. * @since 1.16 */ public void alignWithByteBoundary() { int toSkip = bitsCachedSize % Byte.SIZE; if (toSkip > 0) { readCachedBits(toSkip); } }
/** * Returns at most 63 bits read from the underlying stream. * * @param count the number of bits to read, must be a positive * number not bigger than 63. * @return the bits concatenated as a long using the stream's byte order. * -1 if the end of the underlying stream has been reached before reading * the requested number of bits * @throws IOException on error */ public long readBits(final int count) throws IOException { if (count < 0 || count > MAXIMUM_CACHE_SIZE) { throw new IllegalArgumentException("count must not be negative or greater than " + MAXIMUM_CACHE_SIZE); } if (ensureCache(count)) { return -1; } if (bitsCachedSize < count) { return processBitsGreater57(count); } return readCachedBits(count); }
private static long readBits(BitInputStream reader, int numBits) throws IOException { long r = reader.readBits(numBits); if (r == -1) { throw new EOFException("Truncated Deflate64 Stream"); } return r; } }
@Override public void close() throws IOException { in.close(); }
private boolean init(final boolean isFirstStream) throws IOException { if (null == bin) { throw new IOException("No InputStream"); } if (!isFirstStream) { bin.clearBitCache(); } final int magic0 = readNextByte(this.bin); if (magic0 == -1 && !isFirstStream) { return false; } final int magic1 = readNextByte(this.bin); final int magic2 = readNextByte(this.bin); if (magic0 != 'B' || magic1 != 'Z' || magic2 != 'h') { throw new IOException(isFirstStream ? "Stream is not in the BZip2 format" : "Garbage after a valid BZip2 stream"); } final int blockSize = readNextByte(this.bin); if ((blockSize < '1') || (blockSize > '9')) { throw new IOException("BZip2 block size is invalid"); } this.blockSize100k = blockSize - '0'; this.computedCombinedCRC = 0; return true; }
private void switchToUncompressedState() throws IOException { reader.alignWithByteBoundary(); long bLen = readBits(16); long bNLen = readBits(16); if (((bLen ^ 0xFFFF) & 0xFFFF) != bNLen) { //noinspection DuplicateStringLiteralInspection throw new IllegalStateException("Illegal LEN / NLEN values"); } state = new UncompressedState(bLen); }
/** * Drops bits until the next bits will be read from a byte boundary. * @since 1.16 */ public void alignWithByteBoundary() { int toSkip = bitsCachedSize % Byte.SIZE; if (toSkip > 0) { readCachedBits(toSkip); } }
/** * Reads the next code from the stream. * @return the next code * @throws IOException on error */ protected int readNextCode() throws IOException { if (codeSize > 31) { throw new IllegalArgumentException("code size must not be bigger than 31"); } return (int) in.readBits(codeSize); }
HuffmanDecoder(InputStream in) { this.reader = new BitInputStream(in, ByteOrder.LITTLE_ENDIAN); this.in = in; state = new InitialState(); }
@Override public void close() throws IOException { final BitInputStream inShadow = this.bin; if (inShadow != null) { try { inShadow.close(); } finally { this.data = null; this.bin = null; } } }
private void reAlignReading() throws IOException { // "compress" works in multiples of 8 symbols, each codeBits bits long. // When codeBits changes, the remaining unused symbols in the current // group of 8 are still written out, in the old codeSize, // as garbage values (usually zeroes) that need to be skipped. long codeReadsToThrowAway = 8 - (totalCodesRead % 8); if (codeReadsToThrowAway == 8) { codeReadsToThrowAway = 0; } for (long i = 0; i < codeReadsToThrowAway; i++) { readNextCode(); } in.clearBitCache(); }
@Override int available() throws IOException { return (int) Math.min(blockLength - read, reader.bitsAvailable() / Byte.SIZE); } }