Example usage for javax.imageio.stream MemoryCacheImageInputStream readBits

List of usage examples for javax.imageio.stream MemoryCacheImageInputStream readBits

Introduction

In this page you can find the example usage for javax.imageio.stream MemoryCacheImageInputStream readBits.

Prototype

public long readBits(int numBits) throws IOException 

Source Link

Usage

From source file:org.apache.pdfbox.filter.LZWFilter.java

private void doLZWDecode(InputStream encoded, OutputStream decoded, int earlyChange) throws IOException {
    List<byte[]> codeTable = new ArrayList<byte[]>();
    int chunk = 9;
    final MemoryCacheImageInputStream in = new MemoryCacheImageInputStream(encoded);
    long nextCommand;
    long prevCommand = -1;

    try {/* www.j av a 2s  .  co  m*/
        while ((nextCommand = in.readBits(chunk)) != EOD) {
            if (nextCommand == CLEAR_TABLE) {
                chunk = 9;
                codeTable = createCodeTable();
                prevCommand = -1;
            } else {
                if (nextCommand < codeTable.size()) {
                    byte[] data = codeTable.get((int) nextCommand);
                    byte firstByte = data[0];
                    decoded.write(data);
                    if (prevCommand != -1) {
                        checkIndexBounds(codeTable, prevCommand, in);
                        data = codeTable.get((int) prevCommand);
                        byte[] newData = Arrays.copyOf(data, data.length + 1);
                        newData[data.length] = firstByte;
                        codeTable.add(newData);
                    }
                } else {
                    checkIndexBounds(codeTable, prevCommand, in);
                    byte[] data = codeTable.get((int) prevCommand);
                    byte[] newData = Arrays.copyOf(data, data.length + 1);
                    newData[data.length] = data[0];
                    decoded.write(newData);
                    codeTable.add(newData);
                }

                chunk = calculateChunk(codeTable.size(), earlyChange);
                prevCommand = nextCommand;
            }
        }
    } catch (EOFException ex) {
        LOG.warn("Premature EOF in LZW stream, EOD code missing");
    }
    decoded.flush();
}