Class LZWEncoder

  • All Implemented Interfaces:
    Encoder

    final class LZWEncoder
    extends java.lang.Object
    implements Encoder
    LZWEncoder

    Inspired by LZWTreeEncoder by Wen Yu and the algorithm described by Bob Montgomery which "[...] uses a tree method to search if a new string is already in the table, which is much simpler, faster, and easier to understand than hashing."

    Version:
    $Id: LZWEncoder.java,v 1.0 02.12.13 14:13 haraldk Exp$
    • Constructor Summary

      Constructors 
      Constructor Description
      LZWEncoder​(long length)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private static int bitmaskFor​(int bits)  
      void encode​(java.io.OutputStream stream, java.nio.ByteBuffer buffer)
      Encodes up to buffer.remaining() bytes into the given input stream, from the given buffer.
      (package private) void encodeBytes​(java.io.OutputStream stream, java.nio.ByteBuffer buffer)  
      private void increaseCodeSizeOrResetIfNeeded​(java.io.OutputStream stream)  
      private static int maxValue​(int codeLen)  
      private void resetTables()  
      private void writeCode​(java.io.OutputStream stream, int code)  
      • Methods inherited from class java.lang.Object

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

      • CHILDREN

        private final short[] CHILDREN
      • SIBLINGS

        private final short[] SIBLINGS
      • SUFFIXES

        private final short[] SUFFIXES
      • parent

        private int parent
      • bitsPerCode

        private int bitsPerCode
      • nextValidCode

        private int nextValidCode
      • maxCode

        private int maxCode
      • bits

        private int bits
      • bitPos

        private int bitPos
      • remaining

        private long remaining
    • Constructor Detail

      • LZWEncoder

        LZWEncoder​(long length)
    • Method Detail

      • encode

        public void encode​(java.io.OutputStream stream,
                           java.nio.ByteBuffer buffer)
                    throws java.io.IOException
        Description copied from interface: Encoder
        Encodes up to buffer.remaining() bytes into the given input stream, from the given buffer.
        Specified by:
        encode in interface Encoder
        Parameters:
        stream - the output stream to encode data to
        buffer - buffer to read data from
        Throws:
        java.io.IOException - if an I/O error occurs
      • encodeBytes

        void encodeBytes​(java.io.OutputStream stream,
                         java.nio.ByteBuffer buffer)
                  throws java.io.IOException
        Throws:
        java.io.IOException
      • increaseCodeSizeOrResetIfNeeded

        private void increaseCodeSizeOrResetIfNeeded​(java.io.OutputStream stream)
                                              throws java.io.IOException
        Throws:
        java.io.IOException
      • resetTables

        private void resetTables()
      • writeCode

        private void writeCode​(java.io.OutputStream stream,
                               int code)
                        throws java.io.IOException
        Throws:
        java.io.IOException
      • maxValue

        private static int maxValue​(int codeLen)
      • bitmaskFor

        private static int bitmaskFor​(int bits)