Class LZW


  • public class LZW
    extends Object
    The LZW class provides static methods for compressing and expanding a binary input using LZW compression over the 8-bit extended ASCII alphabet with 12-bit codewords.

    WARNING: Starting with Oracle Java 7u6, the substring method takes time and space linear in the length of the extracted substring (instead of constant time an space as in earlier versions). As a result, compression takes quadratic time. TODO: fix. See this article for more details.

    For additional documentation, see Section 5.5 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.

    Author:
    Robert Sedgewick, Kevin Wayne
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void compress()
      Reads a sequence of 8-bit bytes from standard input; compresses them using LZW compression with 12-bit codewords; and writes the results to standard output.
      static void expand()
      Reads a sequence of bit encoded using LZW compression with 12-bit codewords from standard input; expands them; and writes the results to standard output.
      static void main​(String[] args)
      Sample client that calls compress() if the command-line argument is "-" an expand() if it is "+".
    • Method Detail

      • compress

        public static void compress()
        Reads a sequence of 8-bit bytes from standard input; compresses them using LZW compression with 12-bit codewords; and writes the results to standard output.
      • expand

        public static void expand()
        Reads a sequence of bit encoded using LZW compression with 12-bit codewords from standard input; expands them; and writes the results to standard output.
      • main

        public static void main​(String[] args)
        Sample client that calls compress() if the command-line argument is "-" an expand() if it is "+".
        Parameters:
        args - the command-line arguments