Class Lz4Compressor

  • All Implemented Interfaces:
    Compressor, AutoCloseable

    public final class Lz4Compressor
    extends Object
    implements Compressor
    Compresses a Buffer using the LZ4 format. See original LZ4 Github project and LZ4 block format for full description. Since the original LZ4 block format does not contains size of compressed block and size of original data this encoder uses format like LZ4 Java library written by Adrien Grand and approved by Yann Collet (author of original LZ4 library). * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Magic * Token * Compressed * Decompressed * Checksum * + * LZ4 compressed * * * * length * length * * * block * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    • Method Detail

      • newFactory

        public static Supplier<Lz4Compressor> newFactory()
        Creates the fastest LZ4 compressor factory with default block size (64 KB) and xxhash hashing for Java, based on Yann Collet's work available at Github.
      • newFactory

        public static Supplier<Lz4Compressor> newFactory​(boolean highCompressor)
        Creates a new LZ4 compressor factory with high or fast compression, default block size (64 KB) and xxhash hashing for Java, based on Yann Collet's work available at Github.
        Parameters:
        highCompressor - if true codec will use compressor which requires more memory and is slower but compresses more efficiently
        Returns:
        the factory.
      • newFactory

        public static Supplier<Lz4Compressor> newFactory​(net.jpountz.lz4.LZ4Factory factory,
                                                         boolean highCompressor,
                                                         int blockSize,
                                                         Checksum checksum)
        Creates a new customizable LZ4 compressor factory.
        Parameters:
        factory - user customizable LZ4Factory instance which may be JNI bindings to the original C implementation, a pure Java implementation or a Java implementation that uses the Unsafe
        highCompressor - if true codec will use compressor which requires more memory and is slower but compresses more efficiently
        blockSize - the maximum number of bytes to try to compress at once, must be >= 64 and <= 32 M
        checksum - the Checksum instance to use to check data for integrity
        Returns:
        the factory.
      • newFactory

        public static Supplier<Lz4Compressor> newFactory​(net.jpountz.lz4.LZ4Factory factory,
                                                         boolean highCompressor,
                                                         int blockSize,
                                                         Checksum checksum,
                                                         int maxEncodeSize)
        Creates a new customizable LZ4 compressor factory.
        Parameters:
        factory - user customizable LZ4Factory instance which may be JNI bindings to the original C implementation, a pure Java implementation or a Java implementation that uses the Unsafe
        highCompressor - if true codec will use compressor which requires more memory and is slower but compresses more efficiently
        blockSize - the maximum number of bytes to try to compress at once, must be >= 64 and <= 32 M
        checksum - the Checksum instance to use to check data for integrity
        maxEncodeSize - the maximum size for an encode (compressed) buffer
        Returns:
        the factory.
      • compress

        public Buffer compress​(Buffer input,
                               BufferAllocator allocator)
                        throws CompressionException
        Description copied from interface: Compressor
        This method will read from the input Buffer and compress into a new Buffer that will be allocated (if needed) from the BufferAllocator. This method is expected to consume all data from the input but not take ownership. The caller is responsible to release the input buffer after this method returns.
        Specified by:
        compress in interface Compressor
        Parameters:
        input - the Buffer that contains the data to be compressed.
        allocator - the BufferAllocator that is used to allocate a new buffer (if needed) to write the compressed bytes too.
        Returns:
        the Buffer that contains the compressed data. The caller of this method takes ownership of the buffer. The return value will never be null.
        Throws:
        CompressionException - thrown if an compression error was encountered or the compressor was closed already.
      • finish

        public Buffer finish​(BufferAllocator allocator)
        Description copied from interface: Compressor
        By calling this method we signal that the compression stream is marked as finish. The returned Buffer might contain a "trailer" which marks the end of the stream.
        Specified by:
        finish in interface Compressor
        Returns:
        the Buffer which represent the end of the compression stream, which might be empty if the compressor don't need a trailer to signal the end. The caller of this method takes ownership of the buffer. The return value will never be null.
      • isClosed

        public boolean isClosed()
        Description copied from interface: Compressor
        Return true if the decompressor was closed, false otherwise.
        Specified by:
        isClosed in interface Compressor
        Returns:
        true if the decompressor was closed, false otherwise.