Class CompressLZF

  • All Implemented Interfaces:
    Compressor

    public final class CompressLZF
    extends java.lang.Object
    implements Compressor

    This class implements the LZF lossless data compression algorithm. LZF is a Lempel-Ziv variant with byte-aligned output, and optimized for speed.

    Safety/Use Notes:

    • Each instance should be used by a single thread only.
    • The data buffers should be smaller than 1 GB.
    • For performance reasons, safety checks on expansion are omitted.
    • Invalid compressed data can cause an ArrayIndexOutOfBoundsException.

    The LZF compressed format knows literal runs and back-references:

    • Literal run: directly copy bytes from input to output.
    • Back-reference: copy previous data to output stream, with specified offset from location and length. The length is at least 3 bytes.

    The first byte of the compressed stream is the control byte. For literal runs, the highest three bits of the control byte are not set, the lower bits are the literal run length, and the next bytes are data to copy directly into the output. For back-references, the highest three bits of the control byte are the back-reference length. If all three bits are set, then the back-reference length is stored in the next byte. The lower bits of the control byte combined with the next byte form the offset for the back-reference.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int[] cachedHashTable
      Hash table for matching byte sequences (reused for performance).
      private static int HASH_SIZE
      The number of entries in the hash table.
      private static int MAX_LITERAL
      The maximum number of literals in a chunk (32).
      private static int MAX_OFF
      The maximum offset allowed for a back-reference (8192).
      private static int MAX_REF
      The maximum back-reference length (264).
    • Constructor Summary

      Constructors 
      Constructor Description
      CompressLZF()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compress​(byte[] in, int inPos, int inLen, byte[] out, int outPos)
      Compress a number of bytes.
      int compress​(java.nio.ByteBuffer in, int inPos, byte[] out, int outPos)
      Compress a number of bytes.
      void expand​(byte[] in, int inPos, int inLen, byte[] out, int outPos, int outLen)
      Expand a number of compressed bytes.
      static void expand​(java.nio.ByteBuffer in, java.nio.ByteBuffer out)
      Expand a number of compressed bytes.
      private static int first​(byte[] in, int inPos)
      Return the integer with the first two bytes 0, then the bytes at the index, then at index+1.
      private static int first​(java.nio.ByteBuffer in, int inPos)
      Return the integer with the first two bytes 0, then the bytes at the index, then at index+1.
      int getAlgorithm()
      Get the compression algorithm type.
      private static int hash​(int h)
      Compute the address in the hash table.
      private static int next​(int v, byte[] in, int inPos)
      Shift the value 1 byte left, and add the byte at index inPos+2.
      private static int next​(int v, java.nio.ByteBuffer in, int inPos)
      Shift the value 1 byte left, and add the byte at index inPos+2.
      void setOptions​(java.lang.String options)
      Set the compression options.
      • Methods inherited from class java.lang.Object

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

      • HASH_SIZE

        private static final int HASH_SIZE
        The number of entries in the hash table. The size is a trade-off between hash collisions (reduced compression) and speed (amount that fits in CPU cache).
        See Also:
        Constant Field Values
      • MAX_LITERAL

        private static final int MAX_LITERAL
        The maximum number of literals in a chunk (32).
        See Also:
        Constant Field Values
      • MAX_OFF

        private static final int MAX_OFF
        The maximum offset allowed for a back-reference (8192).
        See Also:
        Constant Field Values
      • MAX_REF

        private static final int MAX_REF
        The maximum back-reference length (264).
        See Also:
        Constant Field Values
      • cachedHashTable

        private int[] cachedHashTable
        Hash table for matching byte sequences (reused for performance).
    • Constructor Detail

      • CompressLZF

        public CompressLZF()
    • Method Detail

      • setOptions

        public void setOptions​(java.lang.String options)
        Description copied from interface: Compressor
        Set the compression options. This may include settings for higher performance but less compression.
        Specified by:
        setOptions in interface Compressor
        Parameters:
        options - the options
      • first

        private static int first​(byte[] in,
                                 int inPos)
        Return the integer with the first two bytes 0, then the bytes at the index, then at index+1.
      • first

        private static int first​(java.nio.ByteBuffer in,
                                 int inPos)
        Return the integer with the first two bytes 0, then the bytes at the index, then at index+1.
      • next

        private static int next​(int v,
                                byte[] in,
                                int inPos)
        Shift the value 1 byte left, and add the byte at index inPos+2.
      • next

        private static int next​(int v,
                                java.nio.ByteBuffer in,
                                int inPos)
        Shift the value 1 byte left, and add the byte at index inPos+2.
      • hash

        private static int hash​(int h)
        Compute the address in the hash table.
      • compress

        public int compress​(byte[] in,
                            int inPos,
                            int inLen,
                            byte[] out,
                            int outPos)
        Description copied from interface: Compressor
        Compress a number of bytes.
        Specified by:
        compress in interface Compressor
        Parameters:
        in - the input data
        inPos - the offset at the input array
        inLen - the number of bytes to compress
        out - the output area
        outPos - the offset at the output array
        Returns:
        the end position
      • compress

        public int compress​(java.nio.ByteBuffer in,
                            int inPos,
                            byte[] out,
                            int outPos)
        Compress a number of bytes.
        Parameters:
        in - the input data
        inPos - the offset at the input buffer
        out - the output area
        outPos - the offset at the output array
        Returns:
        the end position
      • expand

        public void expand​(byte[] in,
                           int inPos,
                           int inLen,
                           byte[] out,
                           int outPos,
                           int outLen)
        Description copied from interface: Compressor
        Expand a number of compressed bytes.
        Specified by:
        expand in interface Compressor
        Parameters:
        in - the compressed data
        inPos - the offset at the input array
        inLen - the number of bytes to read
        out - the output area
        outPos - the offset at the output array
        outLen - the size of the uncompressed data
      • expand

        public static void expand​(java.nio.ByteBuffer in,
                                  java.nio.ByteBuffer out)
        Expand a number of compressed bytes.
        Parameters:
        in - the compressed data
        out - the output area
      • getAlgorithm

        public int getAlgorithm()
        Description copied from interface: Compressor
        Get the compression algorithm type.
        Specified by:
        getAlgorithm in interface Compressor
        Returns:
        the type