Class LZWInputStream

All Implemented Interfaces:
Closeable, AutoCloseable, InputStreamStatistics
Direct Known Subclasses:
ZCompressorInputStream

public abstract class LZWInputStream extends CompressorInputStream implements InputStreamStatistics

Generic LZW implementation. It is used internally for the Z decompressor and the Unshrinking Zip file compression method, but may be useful for third-party projects in implementing their own LZW variations.

Since:
1.10
This class is not thread-safe
  • Field Details

  • Constructor Details

  • Method Details

    • addEntry

      protected abstract int addEntry(int previousCode, byte character) throws IOException
      Add a new entry to the dictionary.
      Parameters:
      previousCode - the previous code
      character - the next character to append
      Returns:
      the new code
      Throws:
      IOException - on error
    • addEntry

      protected int addEntry(int previousCode, byte character, int maxTableSize)
      Adds a new entry if the maximum table size hasn't been exceeded and returns the new index.
      Parameters:
      previousCode - the previous code
      character - the character to append
      maxTableSize - the maximum table size
      Returns:
      the new code or -1 if maxTableSize has been reached already
    • addRepeatOfPreviousCode

      protected int addRepeatOfPreviousCode() throws IOException
      Add entry for repeat of previousCode we haven't added, yet.
      Returns:
      new code for a repeat of the previous code or -1 if maxTableSize has been reached already
      Throws:
      IOException - on error
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InputStream
      Throws:
      IOException
    • decompressNextSymbol

      protected abstract int decompressNextSymbol() throws IOException
      Read the next code and expand it.
      Returns:
      the expanded next code, negative on EOF
      Throws:
      IOException - on error
    • expandCodeToOutputStack

      protected int expandCodeToOutputStack(int code, boolean addedUnfinishedEntry) throws IOException
      Expands the entry with index code to the output stack and may create a new entry
      Parameters:
      code - the code
      addedUnfinishedEntry - whether unfinished entries have been added
      Returns:
      the new location of the output stack
      Throws:
      IOException - on error
    • getClearCode

      protected int getClearCode()
    • getCodeSize

      protected int getCodeSize()
    • getCompressedCount

      public long getCompressedCount()
      Description copied from interface: InputStreamStatistics
      Gets the amount of raw or compressed bytes read by the stream.
      Specified by:
      getCompressedCount in interface InputStreamStatistics
      Returns:
      the amount of raw or compressed bytes read by the stream.
      Since:
      1.17
    • getPrefix

      protected int getPrefix(int offset)
    • getPrefixesLength

      protected int getPrefixesLength()
    • getTableSize

      protected int getTableSize()
    • incrementCodeSize

      protected void incrementCodeSize()
    • initializeTables

      protected void initializeTables(int maxCodeSize)
      Initializes the arrays based on the maximum code size.
      Parameters:
      maxCodeSize - maximum code size
      Throws:
      IllegalArgumentException - if maxCodeSize is out of bounds for prefixes and characters.
    • initializeTables

      protected void initializeTables(int maxCodeSize, int memoryLimitInKb) throws MemoryLimitException
      Initializes the arrays based on the maximum code size. First checks that the estimated memory usage is below memoryLimitInKb
      Parameters:
      maxCodeSize - maximum code size
      memoryLimitInKb - maximum allowed estimated memory usage in Kb
      Throws:
      MemoryLimitException - if estimated memory usage is greater than memoryLimitInKb
      IllegalArgumentException - if maxCodeSize is not bigger than 0
    • read

      public int read() throws IOException
      Specified by:
      read in class InputStream
      Throws:
      IOException
    • read

      public int read(byte[] b, int off, int len) throws IOException
      Overrides:
      read in class InputStream
      Throws:
      IOException
    • readNextCode

      protected int readNextCode() throws IOException
      Reads the next code from the stream.
      Returns:
      the next code
      Throws:
      IOException - on error
    • resetCodeSize

      protected void resetCodeSize()
    • resetPreviousCode

      protected void resetPreviousCode()
    • setClearCode

      protected void setClearCode(int codeSize)
      Sets the clear code based on the code size.
      Parameters:
      codeSize - code size
    • setCodeSize

      protected void setCodeSize(int cs)
    • setPrefix

      protected void setPrefix(int offset, int value)
    • setTableSize

      protected void setTableSize(int newSize)