Class LZ4Frame


  • public class LZ4Frame
    extends java.lang.Object
    Native bindings to the frame API of LZ4.
    • Method Detail

      • nLZ4F_isError

        public static int nLZ4F_isError​(long code)
        Unsafe version of: isError
      • LZ4F_isError

        public static boolean LZ4F_isError​(long code)
        Tells when a function result is an error code.
      • nLZ4F_getErrorName

        public static long nLZ4F_getErrorName​(long code)
        Unsafe version of: getErrorName
      • LZ4F_getErrorName

        @Nullable
        public static java.lang.String LZ4F_getErrorName​(long code)
        Return error code string; for debugging.
      • LZ4F_compressionLevel_max

        public static int LZ4F_compressionLevel_max()
      • nLZ4F_compressFrameBound

        public static long nLZ4F_compressFrameBound​(long srcSize,
                                                    long preferencesPtr)
        Unsafe version of: compressFrameBound
      • LZ4F_compressFrameBound

        public static long LZ4F_compressFrameBound​(long srcSize,
                                                   @Nullable
                                                   LZ4FPreferences preferencesPtr)
        Returns the maximum possible size of a frame compressed with compressFrame given srcSize content and preferences.

        Returns the maximum possible compressed size with compressFrame given srcSize and preferences. preferencesPtr is optional. It can be replaced by NULL, in which case, the function will assume default preferences.

        Note

        this result is only usable with compressFrame. It may also be used with compressUpdate if no {@link #LZ4F_flush flush} operation is performed.

      • nLZ4F_compressFrame

        public static long nLZ4F_compressFrame​(long dstBuffer,
                                               long dstCapacity,
                                               long srcBuffer,
                                               long srcSize,
                                               long preferencesPtr)
        Unsafe version of: compressFrame
        Parameters:
        dstCapacity - MUST be ≥ compressFrameBound(srcSize, preferencesPtr)
      • LZ4F_compressFrame

        public static long LZ4F_compressFrame​(java.nio.ByteBuffer dstBuffer,
                                              java.nio.ByteBuffer srcBuffer,
                                              @Nullable
                                              LZ4FPreferences preferencesPtr)
        Compress an entire srcBuffer into a valid LZ4 frame.

        The LZ4F_preferences_t structure is optional: you can provide NULL as argument. All preferences will be set to default.

        Returns:
        number of bytes written into dstBuffer or an error code if it fails (can be tested using isError)
      • LZ4F_getVersion

        public static int LZ4F_getVersion()
      • nLZ4F_createCompressionContext

        public static long nLZ4F_createCompressionContext​(long cctxPtr,
                                                          int version)
        Unsafe version of: createCompressionContext
      • LZ4F_createCompressionContext

        public static long LZ4F_createCompressionContext​(PointerBuffer cctxPtr,
                                                         int version)
        The first thing to do is to create a compressionContext object, which will be used in all compression operations. This is achieved using LZ4F_createCompressionContext(), which takes as argument a version.

        The function will provide a pointer to a fully allocated LZ4F_cctx object. Object can release its memory using freeCompressionContext;

        Parameters:
        version - MUST be VERSION. It is intended to track potential version mismatch, notably when using DLL. Must be:
        VERSION
        Returns:
        if != zero, there was an error during context creation.
      • nLZ4F_freeCompressionContext

        public static long nLZ4F_freeCompressionContext​(long cctx)
      • LZ4F_freeCompressionContext

        public static long LZ4F_freeCompressionContext​(long cctx)
      • nLZ4F_compressBegin

        public static long nLZ4F_compressBegin​(long cctx,
                                               long dstBuffer,
                                               long dstCapacity,
                                               long prefsPtr)
        Unsafe version of: compressBegin
        Parameters:
        dstCapacity - must be ≥ HEADER_SIZE_MAX bytes
      • LZ4F_compressBegin

        public static long LZ4F_compressBegin​(long cctx,
                                              java.nio.ByteBuffer dstBuffer,
                                              @Nullable
                                              LZ4FPreferences prefsPtr)
        Will write the frame header into dstBuffer.
        Parameters:
        prefsPtr - optional: you can provide NULL as argument, all preferences will then be set to default
        Returns:
        number of bytes written into dstBuffer for the header or an error code (which can be tested using isError)
      • nLZ4F_compressBound

        public static long nLZ4F_compressBound​(long srcSize,
                                               long prefsPtr)
        Unsafe version of: compressBound
      • LZ4F_compressBound

        public static long LZ4F_compressBound​(long srcSize,
                                              @Nullable
                                              LZ4FPreferences prefsPtr)
        Provides minimum dstCapacity required to guarantee compression success given a srcSize and preferences, covering worst case scenario.

        Estimation is valid for either compressUpdate, flush or compressEnd. Estimation includes the possibility that internal buffer might already be filled by up to (blockSize-1) bytes. It also includes frame footer (ending + checksum), which would have to be generated by compressEnd. Estimation doesn't include frame header, as it was already generated by compressBegin.

        Result is always the same for a srcSize and prefsPtr, so it can be trusted to size reusable buffers.

        When srcSize==0, LZ4F_compressBound() provides an upper bound for flush and compressEnd operations.

        Parameters:
        prefsPtr - optional: when NULL is provided, preferences will be set to cover worst case scenario
      • nLZ4F_compressUpdate

        public static long nLZ4F_compressUpdate​(long cctx,
                                                long dstBuffer,
                                                long dstCapacity,
                                                long srcBuffer,
                                                long srcSize,
                                                long cOptPtr)
        Unsafe version of: compressUpdate
      • LZ4F_compressUpdate

        public static long LZ4F_compressUpdate​(long cctx,
                                               java.nio.ByteBuffer dstBuffer,
                                               java.nio.ByteBuffer srcBuffer,
                                               @Nullable
                                               LZ4FCompressOptions cOptPtr)
        LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary.

        Important rule: dstCapacity MUST be large enough to ensure operation success even in worst case situations. This value is provided by compressBound. If this condition is not respected, LZ4F_compress() will fail (result is an errorCode).

        LZ4F_compressUpdate() doesn't guarantee error recovery. When an error occurs, compression context must be freed or resized.

        Parameters:
        cOptPtr - optional: NULL can be provided, in which case all options are set to default
        Returns:
        number of bytes written into dstBuffer (it can be zero, meaning input data was just buffered) or an error code if it fails (which can be tested using isError)
      • nLZ4F_flush

        public static long nLZ4F_flush​(long cctx,
                                       long dstBuffer,
                                       long dstCapacity,
                                       long cOptPtr)
        Unsafe version of: flush
        Parameters:
        dstCapacity - must be large enough to ensure the operation will be successful
      • LZ4F_flush

        public static long LZ4F_flush​(long cctx,
                                      java.nio.ByteBuffer dstBuffer,
                                      @Nullable
                                      LZ4FCompressOptions cOptPtr)
        When data must be generated and sent immediately, without waiting for a block to be completely filled, it's possible to call flush. It will immediately compress any data buffered within cctx.
        Parameters:
        cOptPtr - optional: it's possible to provide NULL, all options will be set to default
        Returns:
        nb of bytes written into dstBuffer (can be zero, when there is no data stored within cctx) or an error code if it fails (which can be tested using isError)
      • nLZ4F_compressEnd

        public static long nLZ4F_compressEnd​(long cctx,
                                             long dstBuffer,
                                             long dstCapacity,
                                             long cOptPtr)
        Unsafe version of: compressEnd
      • LZ4F_compressEnd

        public static long LZ4F_compressEnd​(long cctx,
                                            java.nio.ByteBuffer dstBuffer,
                                            @Nullable
                                            LZ4FCompressOptions cOptPtr)
        To properly finish an LZ4 frame, invoke compressEnd. It will flush whatever data remained within cctx (like flush) and properly finalize the frame, with an endMark and a checksum.
        Parameters:
        cOptPtr - optional: NULL can be provided, in which case all options will be set to default
        Returns:
        nb of bytes written into dstBuffer, necessarily ≥ 4 (endMark), or an error code if it fails (which can be tested using isError).

        A successful call to compressEnd makes cctx available again for another compression task.

      • nLZ4F_createDecompressionContext

        public static long nLZ4F_createDecompressionContext​(long dctxPtr,
                                                            int version)
        Unsafe version of: createDecompressionContext
      • LZ4F_createDecompressionContext

        public static long LZ4F_createDecompressionContext​(PointerBuffer dctxPtr,
                                                           int version)
        Create an LZ4F_dctx object, to track all decompression operations.

        The function provides a pointer to an allocated and initialized LZ4F_dctx object. dctx memory can be released using freeDecompressionContext.

        Parameters:
        version - must be:
        VERSION
        Returns:
        an errorCode, which can be tested using isError
      • nLZ4F_freeDecompressionContext

        public static long nLZ4F_freeDecompressionContext​(long dctx)
        Unsafe version of: freeDecompressionContext
      • LZ4F_freeDecompressionContext

        public static long LZ4F_freeDecompressionContext​(long dctx)
        Frees an LZ4F_dctx object.
        Returns:
        an errorCode, which can be tested using isError.

        Result of LZ4F_freeDecompressionContext() indicates current state of decompressionContext when being released. That is, it should be == 0 if decompression has been completed fully and correctly.

      • nLZ4F_getFrameInfo

        public static long nLZ4F_getFrameInfo​(long dctx,
                                              long frameInfoPtr,
                                              long srcBuffer,
                                              long srcSizePtr)
        Unsafe version of: getFrameInfo
      • LZ4F_getFrameInfo

        public static long LZ4F_getFrameInfo​(long dctx,
                                             LZ4FFrameInfo frameInfoPtr,
                                             java.nio.ByteBuffer srcBuffer,
                                             PointerBuffer srcSizePtr)
        This function extracts frame parameters (max blockSize, dictID, etc.). Its usage is optional.

        Extracted information is typically useful for allocation and dictionary. This function works in 2 situations:

        • At the beginning of a new frame, in which case it will decode information from srcBuffer, starting the decoding process.

          Input size must be large enough to successfully decode the entire frame header. Frame header size is variable, but is guaranteed to be ≤ HEADER_SIZE_MAX bytes. It's allowed to provide more input data than this minimum.

        • After decoding has been started. In which case, no input is read, frame parameters are extracted from dctx.
        • If decoding has barely started, but not yet extracted information from header, LZ4F_getFrameInfo() will fail.

        The number of bytes consumed from srcBuffer will be updated within *srcSizePtr (necessarily ≤ original value). Decompression must resume from (srcBuffer + *srcSizePtr).

        Notes:

        1. in case of error, dctx is not modified. Decoding operation can resume from beginning safely
        2. frame parameters are copied into an already allocated LZ4F_frameInfo_t structure
        Returns:
        an hint about how many srcSize bytes LZ4F_decompress() expects for next call, or an error code which can be tested using LZ4F_isError()
      • nLZ4F_decompress

        public static long nLZ4F_decompress​(long dctx,
                                            long dstBuffer,
                                            long dstSizePtr,
                                            long srcBuffer,
                                            long srcSizePtr,
                                            long dOptPtr)
        Unsafe version of: decompress
      • LZ4F_decompress

        public static long LZ4F_decompress​(long dctx,
                                           java.nio.ByteBuffer dstBuffer,
                                           PointerBuffer dstSizePtr,
                                           java.nio.ByteBuffer srcBuffer,
                                           PointerBuffer srcSizePtr,
                                           LZ4FDecompressOptions dOptPtr)
        Call this function repetitively to regenerate compressed data from srcBuffer. The function will read up to *srcSizePtr bytes from srcBuffer, and decompress data into dstBuffer, of capacity *dstSizePtr.

        The nb of bytes consumed from srcBuffer will be written into *srcSizePtr (necessarily ≤ original value). The number of bytes decompressed into dstBuffer will be written into *dstSizePtr (necessarily ≤ original value).

        The nb of bytes regenerated into dstBuffer is provided within *dstSizePtr (necessarily ≤ original value).

        The function does not necessarily read all input bytes, so always check value in *srcSizePtr. Unconsumed source data must be presented again in subsequent invocations.

        dstBuffer can freely change between each consecutive function invocation. dstBuffer content will be overwritten.

        After a frame is fully decoded, dctx can be used again to decompress another frame.

        After a decompression error, use resetDecompressionContext before re-using dctx, to return to clean state.

        Returns:
        a hint of how many srcSize bytes LZ4F_decompress() expects for next call.

        Schematically, it's the size of the current (or remaining) compressed block + header of next block. Respecting the hint provides some small speed benefit, because it skips intermediate buffers. This is just a hint though, it's always possible to provide any srcSize.

        When a frame is fully decoded, return will be 0 (no more data expected). When provided with more bytes than necessary to decode a frame, LZ4F_decompress() will stop reading exactly at end of current frame, and return 0.

        If decompression failed, return is an error code, which can be tested using isError. After a decompression error, the dctx context is not resumable. Use resetDecompressionContext to return to clean state.

      • nLZ4F_resetDecompressionContext

        public static void nLZ4F_resetDecompressionContext​(long dctx)
        Unsafe version of: resetDecompressionContext
      • LZ4F_resetDecompressionContext

        public static void LZ4F_resetDecompressionContext​(long dctx)
        In case of an error, the context is left in "undefined" state. In which case, it's necessary to reset it, before re-using it.

        This method can also be used to abruptly stop an unfinished decompression, and start a new one using the same context resources.

        Since:
        1.8.0
      • LZ4F_getErrorCode

        public static int LZ4F_getErrorCode​(long functionResult)
      • nLZ4F_createCDict

        public static long nLZ4F_createCDict​(long dictBuffer,
                                             long dictSize)
        Unsafe version of: createCDict
      • LZ4F_createCDict

        public static long LZ4F_createCDict​(java.nio.ByteBuffer dictBuffer)
        When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once. LZ4_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay.

        LZ4_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only.

        dictBuffer can be released after LZ4_CDict creation, since its content is copied within CDict.

      • nLZ4F_freeCDict

        public static void nLZ4F_freeCDict​(long CDict)
      • LZ4F_freeCDict

        public static void LZ4F_freeCDict​(long CDict)
      • nLZ4F_compressFrame_usingCDict

        public static long nLZ4F_compressFrame_usingCDict​(long cctx,
                                                          long dst,
                                                          long dstCapacity,
                                                          long src,
                                                          long srcSize,
                                                          long cdict,
                                                          long preferencesPtr)
        Unsafe version of: compressFrame_usingCDict
      • LZ4F_compressFrame_usingCDict

        public static long LZ4F_compressFrame_usingCDict​(long cctx,
                                                         java.nio.ByteBuffer dst,
                                                         java.nio.ByteBuffer src,
                                                         long cdict,
                                                         LZ4FPreferences preferencesPtr)
        Compress an entire srcBuffer into a valid LZ4 frame using a digested Dictionary.

        dst MUST be ≥ compressFrameBound(srcSize, preferencesPtr). If this condition is not respected, function will fail (return an errorCode).

        Parameters:
        cctx - must point to a context created by createCompressionContext.
        cdict - if NULL, compress without a dictionary
        preferencesPtr - optional: you may provide NULL as argument, but it's not recommended, as it's the only way to provide dictID in the frame header
        Returns:
        number of bytes written into dstBuffer or an error code if it fails (can be tested using isError)
      • nLZ4F_compressBegin_usingCDict

        public static long nLZ4F_compressBegin_usingCDict​(long cctx,
                                                          long dstBuffer,
                                                          long dstCapacity,
                                                          long cdict,
                                                          long prefsPtr)
        Unsafe version of: compressBegin_usingCDict
      • LZ4F_compressBegin_usingCDict

        public static long LZ4F_compressBegin_usingCDict​(long cctx,
                                                         java.nio.ByteBuffer dstBuffer,
                                                         long cdict,
                                                         LZ4FPreferences prefsPtr)
        Inits streaming dictionary compression, and writes the frame header into dstBuffer.

        dstCapacity must be ≥ HEADER_SIZE_MAX bytes.

        Parameters:
        prefsPtr - optional: you may provide NULL as argument, however, it's the only way to provide dictID in the frame header
        Returns:
        number of bytes written into dstBuffer for the header, or an error code (which can be tested using isError)
      • nLZ4F_decompress_usingDict

        public static long nLZ4F_decompress_usingDict​(long dctxPtr,
                                                      long dstBuffer,
                                                      long dstSizePtr,
                                                      long srcBuffer,
                                                      long srcSizePtr,
                                                      long dict,
                                                      long dictSize,
                                                      long decompressOptionsPtr)
        Unsafe version of: decompress_usingDict
      • LZ4F_decompress_usingDict

        public static long LZ4F_decompress_usingDict​(long dctxPtr,
                                                     java.nio.ByteBuffer dstBuffer,
                                                     PointerBuffer dstSizePtr,
                                                     java.nio.ByteBuffer srcBuffer,
                                                     PointerBuffer srcSizePtr,
                                                     java.nio.ByteBuffer dict,
                                                     LZ4FDecompressOptions decompressOptionsPtr)
        Same as decompress, using a predefined dictionary.

        Dictionary is used "in place", without any preprocessing. It must remain accessible throughout the entire frame decoding.