Class ZstdCompressCtx

java.lang.Object
com.github.luben.zstd.ZstdCompressCtx
All Implemented Interfaces:
Closeable, AutoCloseable

public class ZstdCompressCtx extends Object
  • Constructor Details

    • ZstdCompressCtx

      public ZstdCompressCtx()
      Create a context for faster compress operations One such context is required for each thread - put this in a ThreadLocal.
  • Method Details

    • setLevel

      @NotNull public @NotNull ZstdCompressCtx setLevel(int level)
      Set compression level
      Parameters:
      level - compression level, default: Zstd.defaultCompressionLevel()
    • setMagicless

      @NotNull public @NotNull ZstdCompressCtx setMagicless(boolean magiclessFlag)
      Enable or disable magicless frames
      Parameters:
      magiclessFlag - A 32-bits magic number is written at start of frame, default: false
    • setChecksum

      @NotNull public @NotNull ZstdCompressCtx setChecksum(boolean checksumFlag)
      Enable or disable compression checksums
      Parameters:
      checksumFlag - A 32-bits checksum of content is written at end of frame, default: false
    • setWorkers

      @NotNull public @NotNull ZstdCompressCtx setWorkers(int workers)
    • setOverlapLog

      @NotNull public @NotNull ZstdCompressCtx setOverlapLog(int overlapLog)
    • setJobSize

      @NotNull public @NotNull ZstdCompressCtx setJobSize(int jobSize)
    • setTargetLength

      @NotNull public @NotNull ZstdCompressCtx setTargetLength(int targetLength)
    • setMinMatch

      @NotNull public @NotNull ZstdCompressCtx setMinMatch(int minMatch)
    • setSearchLog

      @NotNull public @NotNull ZstdCompressCtx setSearchLog(int searchLog)
    • setChainLog

      @NotNull public @NotNull ZstdCompressCtx setChainLog(int chainLog)
    • setHashLog

      @NotNull public @NotNull ZstdCompressCtx setHashLog(int hashLog)
    • setWindowLog

      @NotNull public @NotNull ZstdCompressCtx setWindowLog(int windowLog)
    • setStrategy

      @NotNull public @NotNull ZstdCompressCtx setStrategy(int strategy)
    • setContentSize

      @NotNull public @NotNull ZstdCompressCtx setContentSize(boolean contentSizeFlag)
      Enable or disable content size
      Parameters:
      contentSizeFlag - Content size will be written into frame header _whenever known_, default: true
    • setDictID

      @NotNull public @NotNull ZstdCompressCtx setDictID(boolean dictIDFlag)
      Enable or disable dictID
      Parameters:
      dictIDFlag - When applicable, dictionary's ID is written into frame header, default: true
    • setLong

      @NotNull public @NotNull ZstdCompressCtx setLong(int windowLog)
      Enable or disable LongDistanceMatching and set the window size
      Parameters:
      windowLog - Maximum allowed back-reference distance, expressed as power of 2. This will set a memory budget for streaming decompression, with larger values requiring more memory and typically compressing more. Must be clamped between 10 and 32/64 but values greater than 27 may not be decompressable in all context as they require more memory. 0 disables LDM.
    • registerSequenceProducer

      @NotNull public @NotNull ZstdCompressCtx registerSequenceProducer(@Nullable @Nullable SequenceProducer producer)
      Register an external sequence producer
      Parameters:
      producer - the user-defined SequenceProducer to register.
    • setSequenceProducerFallback

      @NotNull public @NotNull ZstdCompressCtx setSequenceProducerFallback(boolean fallbackFlag)
      Enable or disable sequence producer fallback
      Parameters:
      fallbackFlag - fall back to the default internal sequence producer if an external sequence producer returns an error code, default: false
    • setSearchForExternalRepcodes

      @NotNull public @NotNull ZstdCompressCtx setSearchForExternalRepcodes(@NotNull Zstd.ParamSwitch searchRepcodes)
      Set whether to search external sequences for repeated offsets that can be encoded as repcodes.
      Parameters:
      searchRepcodes - whether to search for repcodes
    • setValidateSequences

      @NotNull public @NotNull ZstdCompressCtx setValidateSequences(@NotNull Zstd.ParamSwitch validateSequences)
      Enable or disable sequence validation. Useful for the sequence-level API and with external sequence producers.
      Parameters:
      validateSequences - whether to enable sequence validation
    • setEnableLongDistanceMatching

      @NotNull public @NotNull ZstdCompressCtx setEnableLongDistanceMatching(@NotNull Zstd.ParamSwitch enableLDM)
      Enable or disable long-distance matching.
      Parameters:
      enableLDM - whether to enable long-distance matching.
    • loadDict

      @NotNull public @NotNull ZstdCompressCtx loadDict(@Nullable @Nullable ZstdDictCompress dict)
      Load compression dictionary to be used for subsequently compressed frames.
      Parameters:
      dict - the dictionary
    • loadDict

      @NotNull public @NotNull ZstdCompressCtx loadDict(byte @Nullable [] dict)
      Load compression dictionary to be used for subsequently compressed frames.
      Parameters:
      dict - the dictionary or `null` to remove loaded dictionary
    • getFrameProgression

      @NotNull public @NotNull ZstdFrameProgression getFrameProgression()
      Tells how much data has been ingested (read from input), consumed (input actually compressed) and produced (output) for current frame.
    • reset

      public void reset()
      Clear all state and parameters from the compression context. This leaves the object in a state identical to a newly created compression context.
    • setPledgedSrcSize

      public void setPledgedSrcSize(long srcSize)
      Promise to compress a certain number of source bytes. Knowing the number of bytes to compress up front helps to choose proper compression settings and size internal buffers. Additionally, the pledged size is stored in the header of the output stream, allowing decompressors to know how much uncompressed data to expect. Attempting to compress more or less than the pledged size will result in an error.
    • compressByteBufferStream

      public boolean compressByteBufferStream(@NotNull @NotNull ByteBuffer dst, @NotNull @NotNull ByteBuffer src, @NotNull @NotNull EndDirective endOp)
      Compress as much of the src ByteBuffer into the dst ByteBuffer as possible. Both buffers may be direct or array-backed heap buffers. The destination buffer must be writable.
      Parameters:
      dst - destination of compressed data
      src - buffer to compress
      endOp - directive for handling the end of the stream
      Returns:
      true if all state has been flushed from internal buffers
      Throws:
      IllegalArgumentException - if either buffer is unsupported or the destination is read-only
    • compressDirectByteBufferStream

      public boolean compressDirectByteBufferStream(@NotNull @NotNull ByteBuffer dst, @NotNull @NotNull ByteBuffer src, @NotNull @NotNull EndDirective endOp)
      Compress as much of the src ByteBuffer into the dst ByteBuffer as possible.
      Parameters:
      dst - destination of compressed data
      src - buffer to compress
      endOp - directive for handling the end of the stream
      Returns:
      true if all state has been flushed from internal buffers
    • compressDirectByteBuffer

      public int compressDirectByteBuffer(@NotNull @NotNull ByteBuffer dstBuff, int dstOffset, int dstSize, @NotNull @NotNull ByteBuffer srcBuff, int srcOffset, int srcSize)
      Compresses buffer 'srcBuff' into buffer 'dstBuff' reusing this ZstdCompressCtx. Destination buffer should be sized to handle worst cases situations (input data not compressible). Worst case size evaluation is provided by function ZSTD_compressBound(). This is a low-level function that does not take into account or affect the `limit` or `position` of source or destination buffers.
      Parameters:
      dstBuff - the destination buffer - must be direct
      dstOffset - the start offset of 'dstBuff'
      dstSize - the size of 'dstBuff' (after 'dstOffset')
      srcBuff - the source buffer - must be direct
      srcOffset - the start offset of 'srcBuff'
      srcSize - the length of 'srcBuff' (after 'srcOffset')
      Returns:
      the number of bytes written into buffer 'dstBuff'.
    • compressByteArray

      public int compressByteArray(byte @NotNull [] dstBuff, int dstOffset, int dstSize, byte @NotNull [] srcBuff, int srcOffset, int srcSize)
      Compresses byte array 'srcBuff' into byte array 'dstBuff' reusing this ZstdCompressCtx. Destination buffer should be sized to handle worst cases situations (input data not compressible). Worst case size evaluation is provided by function ZSTD_compressBound().
      Parameters:
      dstBuff - the destination buffer (byte array)
      dstOffset - the start offset of 'dstBuff'
      dstSize - the size of 'dstBuff' (after 'dstOffset')
      srcBuff - the source buffer (byte array)
      srcOffset - the start offset of 'srcBuff'
      srcSize - the length of 'srcBuff' (after 'srcOffset')
      Returns:
      the number of bytes written into buffer 'dstBuff'.
    • compress

      public int compress(@NotNull @NotNull ByteBuffer dstBuf, @NotNull @NotNull ByteBuffer srcBuf)
      Compresses the data in buffer 'srcBuf'
      Parameters:
      dstBuf - the destination buffer - must be direct. It is assumed that the `position()` of this buffer marks the offset at which the compressed data are to be written, and that the `limit()` of this buffer is the maximum compressed data size to allow.

      When this method returns successfully, its `position()` will be set to its current `position()` plus the compressed size of the data.

      srcBuf - the source buffer - must be direct. It is assumed that the `position()` of this buffer marks the beginning of the uncompressed data to be compressed, and that the `limit()` of this buffer marks its end.

      When this method returns successfully, its `position()` will be set to the initial `limit()`.

      Returns:
      the size of the compressed data
    • compress

      @NotNull public @NotNull ByteBuffer compress(@NotNull @NotNull ByteBuffer srcBuf) throws ZstdException
      Compresses the data in buffer 'srcBuf'
      Parameters:
      srcBuf - the source buffer - must be direct. It is assumed that the `position()` of the buffer marks the beginning of the uncompressed data to be compressed, and that the `limit()` of this buffer marks its end.

      When this method returns successfully, its `position()` will be set to its initial `limit()`.

      Returns:
      A newly allocated direct ByteBuffer containing the compressed data.
      Throws:
      ZstdException
    • compress

      public int compress(byte @NotNull [] dst, byte @NotNull [] src)
    • compress

      public byte @NotNull [] compress(byte @NotNull [] src)
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable