Class LZWInputStream

    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected LZWInputStream​(java.io.InputStream inputStream, java.nio.ByteOrder byteOrder)
      Constructs a new instance.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected abstract int addEntry​(int previousCode, byte character)
      Add a new entry to the dictionary.
      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.
      protected int addRepeatOfPreviousCode()
      Add entry for repeat of previousCode we haven't added, yet.
      void close()  
      protected abstract int decompressNextSymbol()
      Reads the next code and expand it.
      protected int expandCodeToOutputStack​(int code, boolean addedUnfinishedEntry)
      Expands the entry with index code to the output stack and may create a new entry.
      protected int getClearCode()
      Gets the clear code.
      protected int getCodeSize()
      Gets the code size in bits.
      long getCompressedCount()
      Gets the amount of raw or compressed bytes read by the stream.
      protected int getPrefix​(int offset)
      Gets the prefix at the given offset.
      protected int getPrefixesLength()
      Gets the prefixes length.
      protected int getTableSize()
      Gets the table size.
      protected void incrementCodeSize()
      Increments the code size by one.
      protected void initializeTables​(int maxCodeSize)
      Initializes the arrays based on the maximum code size.
      protected void initializeTables​(int maxCodeSize, int memoryLimiKiB)
      Initializes the arrays based on the maximum code size.
      int read()  
      int read​(byte[] b, int off, int len)  
      protected int readNextCode()
      Reads the next code from the stream.
      protected void resetCodeSize()
      Resets the code size to its default value.
      protected void resetPreviousCode()
      Resets the previous code to its default value.
      protected void setClearCode​(int codeSize)
      Sets the clear code based on the code size.
      protected void setCodeSize​(int codeSize)
      Sets the code size in bits.
      protected void setPrefix​(int offset, int value)
      Sets the prefix at the given offset.
      protected void setTableSize​(int tableSize)
      Sets the table size.
      • Methods inherited from class java.io.InputStream

        available, mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skip, transferTo
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • LZWInputStream

        protected LZWInputStream​(java.io.InputStream inputStream,
                                 java.nio.ByteOrder byteOrder)
        Constructs a new instance.
        Parameters:
        inputStream - The underlying input stream.
        byteOrder - the input byte order.
    • Method Detail

      • addEntry

        protected abstract int addEntry​(int previousCode,
                                        byte character)
                                 throws java.io.IOException
        Add a new entry to the dictionary.
        Parameters:
        previousCode - the previous code
        character - the next character to append
        Returns:
        the new code
        Throws:
        java.io.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 java.io.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:
        java.io.IOException - on error
      • close

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

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

        protected int expandCodeToOutputStack​(int code,
                                              boolean addedUnfinishedEntry)
                                       throws java.io.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:
        java.io.IOException - if an I/O error occurs.
      • getClearCode

        protected int getClearCode()
        Gets the clear code.
        Returns:
        the clear code.
      • getCodeSize

        protected int getCodeSize()
        Gets the code size in bits.
        Returns:
        the code size in bits.
      • getPrefix

        protected int getPrefix​(int offset)
        Gets the prefix at the given offset.
        Parameters:
        offset - offset to query.
        Returns:
        the prefix at the given offset.
      • getPrefixesLength

        protected int getPrefixesLength()
        Gets the prefixes length.
        Returns:
        the prefixes length.
      • getTableSize

        protected int getTableSize()
        Gets the table size.
        Returns:
        the table size.
      • incrementCodeSize

        protected void incrementCodeSize()
        Increments the code size by one.
      • initializeTables

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

        protected void initializeTables​(int maxCodeSize,
                                        int memoryLimiKiB)
                                 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
        memoryLimiKiB - maximum allowed estimated memory usage in kibibytes (KiB).
        Throws:
        MemoryLimitException - if estimated memory usage is greater than memoryLimitKiB
        java.lang.IllegalArgumentException - if maxCodeSize is not bigger than 0
      • read

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

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

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

        protected void resetCodeSize()
        Resets the code size to its default value.
      • resetPreviousCode

        protected void resetPreviousCode()
        Resets the previous code to its default value.
      • setClearCode

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

        protected void setCodeSize​(int codeSize)
        Sets the code size in bits.
        Parameters:
        codeSize - the code size in bits.
      • setPrefix

        protected void setPrefix​(int offset,
                                 int value)
        Sets the prefix at the given offset.
        Parameters:
        offset - the target offset.
        value - the new value.
      • setTableSize

        protected void setTableSize​(int tableSize)
        Sets the table size.
        Parameters:
        tableSize - the new table size.