Class LZ4Compressor


  • public abstract class LZ4Compressor
    extends java.lang.Object
    LZ4 compressor.

    Instances of this class are thread-safe.

    • Constructor Summary

      Constructors 
      Constructor Description
      LZ4Compressor()  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      byte[] compress​(byte[] src)
      Convenience method, equivalent to calling compress(src, 0, src.length).
      int compress​(byte[] src, byte[] dest)
      Convenience method, equivalent to calling compress(src, 0, src.length, dest, 0).
      byte[] compress​(byte[] src, int srcOff, int srcLen)
      Convenience method which returns src[srcOff:srcOff+srcLen] compressed.
      int compress​(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff)
      abstract int compress​(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen)
      Compresses src[srcOff:srcOff+srcLen] into dest[destOff:destOff+maxDestLen] and returns the compressed length.
      abstract int compress​(java.nio.ByteBuffer src, int srcOff, int srcLen, java.nio.ByteBuffer dest, int destOff, int maxDestLen)
      Compresses src[srcOff:srcOff+srcLen] into dest[destOff:destOff+maxDestLen] and returns the compressed length.
      void compress​(java.nio.ByteBuffer src, java.nio.ByteBuffer dest)
      Compresses src into dest.
      int maxCompressedLength​(int length)
      Returns the maximum compressed length for an input of size length.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

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

      • LZ4Compressor

        public LZ4Compressor()
    • Method Detail

      • maxCompressedLength

        public final int maxCompressedLength​(int length)
        Returns the maximum compressed length for an input of size length.
        Parameters:
        length - the input size in bytes
        Returns:
        the maximum compressed length in bytes
      • compress

        public abstract int compress​(byte[] src,
                                     int srcOff,
                                     int srcLen,
                                     byte[] dest,
                                     int destOff,
                                     int maxDestLen)
        Compresses src[srcOff:srcOff+srcLen] into dest[destOff:destOff+maxDestLen] and returns the compressed length. This method will throw a LZ4Exception if this compressor is unable to compress the input into less than maxDestLen bytes. To prevent this exception to be thrown, you should make sure that maxDestLen >= maxCompressedLength(srcLen).
        Parameters:
        src - the source data
        srcOff - the start offset in src
        srcLen - the number of bytes to compress
        dest - the destination buffer
        destOff - the start offset in dest
        maxDestLen - the maximum number of bytes to write in dest
        Returns:
        the compressed size
        Throws:
        LZ4Exception - if maxDestLen is too small
      • compress

        public abstract int compress​(java.nio.ByteBuffer src,
                                     int srcOff,
                                     int srcLen,
                                     java.nio.ByteBuffer dest,
                                     int destOff,
                                     int maxDestLen)
        Compresses src[srcOff:srcOff+srcLen] into dest[destOff:destOff+maxDestLen] and returns the compressed length. This method will throw a LZ4Exception if this compressor is unable to compress the input into less than maxDestLen bytes. To prevent this exception to be thrown, you should make sure that maxDestLen >= maxCompressedLength(srcLen). ByteBuffer positions remain unchanged.
        Parameters:
        src - the source data
        srcOff - the start offset in src
        srcLen - the number of bytes to compress
        dest - the destination buffer
        destOff - the start offset in dest
        maxDestLen - the maximum number of bytes to write in dest
        Returns:
        the compressed size
        Throws:
        LZ4Exception - if maxDestLen is too small
      • compress

        public final int compress​(byte[] src,
                                  int srcOff,
                                  int srcLen,
                                  byte[] dest,
                                  int destOff)
        Parameters:
        src - the source data
        srcOff - the start offset in src
        srcLen - the number of bytes to compress
        dest - the destination buffer
        destOff - the start offset in dest
        Returns:
        the compressed size
        Throws:
        LZ4Exception - if dest is too small
      • compress

        public final int compress​(byte[] src,
                                  byte[] dest)
        Convenience method, equivalent to calling compress(src, 0, src.length, dest, 0).
        Parameters:
        src - the source data
        dest - the destination buffer
        Returns:
        the compressed size
        Throws:
        LZ4Exception - if dest is too small
      • compress

        public final byte[] compress​(byte[] src,
                                     int srcOff,
                                     int srcLen)
        Convenience method which returns src[srcOff:srcOff+srcLen] compressed.

        Warning: this method has an important overhead due to the fact that it needs to allocate a buffer to compress into, and then needs to resize this buffer to the actual compressed length.

        Here is how this method is implemented:

         final int maxCompressedLength = maxCompressedLength(srcLen);
         final byte[] compressed = new byte[maxCompressedLength];
         final int compressedLength = compress(src, srcOff, srcLen, compressed, 0);
         return Arrays.copyOf(compressed, compressedLength);
         
        Parameters:
        src - the source data
        srcOff - the start offset in src
        srcLen - the number of bytes to compress
        Returns:
        the compressed data
      • compress

        public final byte[] compress​(byte[] src)
        Convenience method, equivalent to calling compress(src, 0, src.length).
        Parameters:
        src - the source data
        Returns:
        the compressed data
      • compress

        public final void compress​(java.nio.ByteBuffer src,
                                   java.nio.ByteBuffer dest)
        Compresses src into dest. Calling this method will update the positions of both ByteBuffers.
        Parameters:
        src - the source data
        dest - the destination buffer
        Throws:
        LZ4Exception - if dest is too small
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object