Class Zstd
- java.lang.Object
-
- org.lwjgl.util.zstd.Zstd
-
public class Zstd extends java.lang.ObjectNative bindings to Zstandard (zstd), a fast lossless compression algorithm, targeting real-time compression scenarios at zlib-level and better compression ratios.Introduction
zstd, short for Zstandard, is a fast lossless compression algorithm, targeting real-time compression scenarios at zlib-level and better compression ratios. The zstd compression library provides in-memory compression and decompression functions. The library supports compression levels from 1 up to
maxCLevelwhich is currently 22. Levels ≥ 20, labeled--ultra, should be used with caution, as they require more memory.Compression can be done in:
- a single step (described as Simple API)
- a single step, reusing a context (described as Explicit context)
- unbounded multiple steps (described as Streaming compression)
The compression ratio achievable on small data can be highly improved using a dictionary in:
- a single step (described as Simple dictionary API)
- a single step, reusing a dictionary (described as Bulk-processing dictionary API)
Advanced experimental functions can be accessed using
#define ZSTD_STATIC_LINKING_ONLYbefore includingzstd.h. Advanced experimental APIs shall never be used with a dynamic library. They are not "stable", their definition may change in the future. Only static linking is allowed.Streaming compression - HowTo
A
ZSTD_CStreamobject is required to track streaming operation.Use
createCStreamandfreeCStreamto create/release resources.ZSTD_CStreamobjects can be reused multiple times on consecutive compression operations. It is recommended to re-useZSTD_CStreamin situations where many streaming operations will be achieved consecutively, since it will play nicer with system's memory, by re-using already allocated memory. Use one separateZSTD_CStreamper thread for parallel execution.Start a new compression by initializing
ZSTD_CStreamcontext. UseinitCStreamto start a new compression operation.Use
compressStreamas many times as necessary to consume input stream. The function will automatically update bothposfields withininputandoutput. Note that the function may not consume the entire input, for example, because the output buffer is already full, in which caseinput.pos < input.size. The caller must check if input has been entirely consumed. If not, the caller must make some room to receive more compressed data, typically by emptying output buffer, or allocating a new output buffer, and then present again remaining input data.At any moment, it's possible to flush whatever data might remain stuck within internal buffer, using
flushStream.output->poswill be updated. Note that, ifoutput->sizeis too small, a single invocation ofZSTD_flushStream()might not be enough (return code > 0). In which case, make some room to receive more compressed data, and call againZSTD_flushStream().endStreaminstructs to finish a frame. It will perform a flush and write frame epilogue. The epilogue is required for decoders to consider a frame completed.flush()operation is the same, and follows same rules asZSTD_flushStream().Streaming decompression - HowTo
A
ZSTD_DStreamobject is required to track streaming operations.Use
createDStreamandfreeDStreamto create/release resources.ZSTD_DStreamobjects can be re-used multiple times.Use
initDStreamto start a new decompression operation, orZSTD_initDStream_usingDict()if decompression requires a dictionary.Use
decompressStreamrepetitively to consume your input. The function will update bothposfields. Ifinput.pos < input.size, some input has not been consumed. It's up to the caller to present again remaining data. Ifoutput.pos < output.size, decoder has flushed everything it could.
-
-
Field Summary
Fields Modifier and Type Field Description static intZSTD_CLEVEL_DEFAULTDefault compression level.static longZSTD_CONTENTSIZE_ERROR
ZSTD_CONTENTSIZE_UNKNOWNContent size.static intZSTD_VERSION_MAJOR
ZSTD_VERSION_MINORVersion number part.static intZSTD_VERSION_NUMBERVersion number.static intZSTD_VERSION_RELEASEVersion number part.static java.lang.StringZSTD_VERSION_STRINGVersion string.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static longnZSTD_compress(long dst, long dstCapacity, long src, long srcSize, int compressionLevel)Unsafe version of:compressstatic longnZSTD_compress_usingCDict(long cctx, long dst, long dstCapacity, long src, long srcSize, long cdict)Unsafe version of:compress_usingCDictstatic longnZSTD_compress_usingDict(long ctx, long dst, long dstCapacity, long src, long srcSize, long dict, long dictSize, int compressionLevel)Unsafe version of:compress_usingDictstatic longnZSTD_compressCCtx(long ctx, long dst, long dstCapacity, long src, long srcSize, int compressionLevel)Unsafe version of:compressCCtxstatic longnZSTD_compressStream(long zcs, long output, long input)Unsafe version of:compressStreamstatic longnZSTD_createCDict(long dictBuffer, long dictSize, int compressionLevel)Unsafe version of:createCDictstatic longnZSTD_createDDict(long dictBuffer, long dictSize)Unsafe version of:createDDictstatic longnZSTD_decompress(long dst, long dstCapacity, long src, long compressedSize)Unsafe version of:decompressstatic longnZSTD_decompress_usingDDict(long dctx, long dst, long dstCapacity, long src, long srcSize, long ddict)Unsafe version of:decompress_usingDDictstatic longnZSTD_decompress_usingDict(long dctx, long dst, long dstCapacity, long src, long srcSize, long dict, long dictSize)Unsafe version of:decompress_usingDictstatic longnZSTD_decompressDCtx(long ctx, long dst, long dstCapacity, long src, long srcSize)Unsafe version of:decompressDCtxstatic longnZSTD_decompressStream(long zds, long output, long input)Unsafe version of:decompressStreamstatic longnZSTD_endStream(long zcs, long output)Unsafe version of:endStreamstatic longnZSTD_flushStream(long zcs, long output)Unsafe version of:flushStreamstatic longnZSTD_freeCCtx(long cctx)Unsafe version of:freeCCtxstatic longnZSTD_freeCDict(long CDict)Unsafe version of:freeCDictstatic longnZSTD_freeCStream(long zcs)Unsafe version of:freeCStreamstatic longnZSTD_freeDCtx(long dctx)Unsafe version of:freeDCtxstatic longnZSTD_freeDDict(long ddict)Unsafe version of:freeDDictstatic longnZSTD_freeDStream(long zds)Unsafe version of:freeDStreamstatic longnZSTD_getErrorName(long code)Unsafe version of:getErrorNamestatic longnZSTD_getFrameContentSize(long src, long srcSize)Unsafe version of:getFrameContentSizestatic longnZSTD_initCStream(long zcs, int compressionLevel)static longnZSTD_initDStream(long zds)Unsafe version of:initDStreamstatic intnZSTD_isError(long code)Unsafe version of:isErrorstatic longnZSTD_versionString()Unsafe version of:versionStringstatic longZSTD_compress(java.nio.ByteBuffer dst, java.nio.ByteBuffer src, int compressionLevel)Compressessrccontent as a single zstd compressed frame into already allocateddst.static longZSTD_compress_usingCDict(long cctx, java.nio.ByteBuffer dst, java.nio.ByteBuffer src, long cdict)Compression using a digested Dictionary.static longZSTD_compress_usingDict(long ctx, java.nio.ByteBuffer dst, java.nio.ByteBuffer src, java.nio.ByteBuffer dict, int compressionLevel)Compression using a predefined Dictionary (seedictBuilder/zdict.h).static longZSTD_compressBound(long srcSize)Returns the maximum compressed size in worst case single-pass scenario.static longZSTD_COMPRESSBOUND(long srcSize)Pure Java version ofZSTD_compressBound(long).static longZSTD_compressCCtx(long ctx, java.nio.ByteBuffer dst, java.nio.ByteBuffer src, int compressionLevel)static longZSTD_compressStream(long zcs, ZSTDOutBuffer output, ZSTDInBuffer input)static longZSTD_createCCtx()Creates a compression context.static longZSTD_createCDict(java.nio.ByteBuffer dictBuffer, int compressionLevel)When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once.static longZSTD_createCStream()static longZSTD_createDCtx()Creates a decompression context.static longZSTD_createDDict(java.nio.ByteBuffer dictBuffer)Creates a digested dictionary, ready to start decompression operation without startup delay.static longZSTD_createDStream()static longZSTD_CStreamInSize()Returns the recommended size for input buffer.static longZSTD_CStreamOutSize()Returns the recommended size for output buffer.static longZSTD_decompress(java.nio.ByteBuffer dst, java.nio.ByteBuffer src)static longZSTD_decompress_usingDDict(long dctx, java.nio.ByteBuffer dst, java.nio.ByteBuffer src, long ddict)Decompression using a digested Dictionary.static longZSTD_decompress_usingDict(long dctx, java.nio.ByteBuffer dst, java.nio.ByteBuffer src, java.nio.ByteBuffer dict)Decompression using a predefined Dictionary (seedictBuilder/zdict.h).static longZSTD_decompressDCtx(long ctx, java.nio.ByteBuffer dst, java.nio.ByteBuffer src)static longZSTD_decompressStream(long zds, ZSTDOutBuffer output, ZSTDInBuffer input)static longZSTD_DStreamInSize()Returns the recommended size for input buffer.static longZSTD_DStreamOutSize()Returns the recommended size for output buffer.static longZSTD_endStream(long zcs, ZSTDOutBuffer output)static longZSTD_flushStream(long zcs, ZSTDOutBuffer output)static longZSTD_freeCCtx(long cctx)Frees memory allocated bycreateCCtx.static longZSTD_freeCDict(long CDict)Frees memory allocated bycreateCDict.static longZSTD_freeCStream(long zcs)Frees memory allocated bycreateCStream.static longZSTD_freeDCtx(long dctx)Frees memory allocated bycreateDCtx.static longZSTD_freeDDict(long ddict)Frees memory allocated withcreateDDict.static longZSTD_freeDStream(long zds)Frees memory allocated bycreateDStream.static java.lang.StringZSTD_getErrorName(long code)Provides readable string from an error code.static longZSTD_getFrameContentSize(java.nio.ByteBuffer src)Notes: a 0 return value means the frame is valid but "empty" decompressed size is an optional field, it may not be present, typically in streaming mode.static longZSTD_initCStream(long zcs, int compressionLevel)static longZSTD_initDStream(long zds)static booleanZSTD_isError(long code)Tells if asize_tfunction result is an error code.static intZSTD_maxCLevel()Returns the maximum compression level available.static intZSTD_versionNumber()Returns the version number.static java.lang.StringZSTD_versionString()Returns the version string.
-
-
-
Field Detail
-
ZSTD_VERSION_MAJOR, ZSTD_VERSION_MINOR, ZSTD_VERSION_RELEASE
Version number part.
-
ZSTD_VERSION_NUMBER
Version number.
-
ZSTD_VERSION_STRING
Version string.
-
ZSTD_CONTENTSIZE_UNKNOWN, ZSTD_CONTENTSIZE_ERROR
Content size.
-
ZSTD_CLEVEL_DEFAULT
Default compression level.
-
-
Method Detail
-
ZSTD_versionNumber
public static int ZSTD_versionNumber()
Returns the version number.
-
nZSTD_versionString
public static long nZSTD_versionString()
Unsafe version of:versionString
-
ZSTD_versionString
public static java.lang.String ZSTD_versionString()
Returns the version string.
-
nZSTD_compress
public static long nZSTD_compress(long dst, long dstCapacity, long src, long srcSize, int compressionLevel)Unsafe version of:compress
-
ZSTD_compress
public static long ZSTD_compress(java.nio.ByteBuffer dst, java.nio.ByteBuffer src, int compressionLevel)Compressessrccontent as a single zstd compressed frame into already allocateddst.Hint: compression runs faster if
dstCapacity≥compressBound(srcSize)- Returns:
- compressed size written into
dst(≤dstCapacity), or an error code if it fails (which can be tested usingisError).
-
nZSTD_decompress
public static long nZSTD_decompress(long dst, long dstCapacity, long src, long compressedSize)Unsafe version of:decompress- Parameters:
dstCapacity- is an upper bound oforiginalSizeto regenerate. If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data.compressedSize- must be the exact size of some number of compressed and/or skippable frames
-
ZSTD_decompress
public static long ZSTD_decompress(java.nio.ByteBuffer dst, java.nio.ByteBuffer src)- Returns:
- the number of bytes decompressed into
dst(≤dstCapacity), or anerrorCodeif it fails (which can be tested usingisError).
-
nZSTD_getFrameContentSize
public static long nZSTD_getFrameContentSize(long src, long srcSize)Unsafe version of:getFrameContentSize- Parameters:
srcSize- must be at least as large as the frame header. Hint: any size ≥FRAMEHEADERSIZE_MAXis large enough.
-
ZSTD_getFrameContentSize
public static long ZSTD_getFrameContentSize(java.nio.ByteBuffer src)
Notes:- a 0 return value means the frame is valid but "empty"
- decompressed size is an optional field, it may not be present, typically in streaming mode. When
return==ZSTD_CONTENTSIZE_UNKNOWN, data to decompress could be any size. In which case, it's necessary to use streaming mode to decompress data. Optionally, application can rely on some implicit limit, asdecompressonly needs an upper bound of decompressed size. (For example, data could be necessarily cut into blocks ≤ 16 KB). - decompressed size is always present when compression is completed using single-pass functions, such as
compress,compressCCtx,compress_usingDictorcompress_usingCDict. - decompressed size can be very large (64-bits value), potentially larger than what local system can handle as a single memory segment. In which case, it's necessary to use streaming mode to decompress data.
- If source is untrusted, decompressed size could be wrong or intentionally modified. Always ensure return value fits within application's authorized limits. Each application can set its own limits.
- Parameters:
src- should point to the start of a ZSTD encoded frame- Returns:
- decompressed size of
srcframe content, if knownCONTENTSIZE_UNKNOWNif the size cannot be determinedCONTENTSIZE_ERRORif an error occurred (e.g. invalid magic number,srcSizetoo small)
-
ZSTD_compressBound
public static long ZSTD_compressBound(long srcSize)
Returns the maximum compressed size in worst case single-pass scenario.
-
nZSTD_isError
public static int nZSTD_isError(long code)
Unsafe version of:isError
-
ZSTD_isError
public static boolean ZSTD_isError(long code)
Tells if asize_tfunction result is an error code.
-
nZSTD_getErrorName
public static long nZSTD_getErrorName(long code)
Unsafe version of:getErrorName
-
ZSTD_getErrorName
public static java.lang.String ZSTD_getErrorName(long code)
Provides readable string from an error code.
-
ZSTD_maxCLevel
public static int ZSTD_maxCLevel()
Returns the maximum compression level available.
-
ZSTD_createCCtx
public static long ZSTD_createCCtx()
Creates a compression context.When compressing many times, it is recommended to allocate a context just once, and re-use it for each successive compression operation. This will make workload friendlier for system's memory. Use one context per thread for parallel execution in multi-threaded environments.
-
nZSTD_freeCCtx
public static long nZSTD_freeCCtx(long cctx)
Unsafe version of:freeCCtx
-
ZSTD_freeCCtx
public static long ZSTD_freeCCtx(long cctx)
Frees memory allocated bycreateCCtx.
-
nZSTD_compressCCtx
public static long nZSTD_compressCCtx(long ctx, long dst, long dstCapacity, long src, long srcSize, int compressionLevel)Unsafe version of:compressCCtx
-
ZSTD_compressCCtx
public static long ZSTD_compressCCtx(long ctx, java.nio.ByteBuffer dst, java.nio.ByteBuffer src, int compressionLevel)
-
ZSTD_createDCtx
public static long ZSTD_createDCtx()
Creates a decompression context.When decompressing many times, it is recommended to allocate a context only once, and re-use it for each successive compression operation. This will make workload friendlier for system's memory. Use one context per thread for parallel execution.
-
nZSTD_freeDCtx
public static long nZSTD_freeDCtx(long dctx)
Unsafe version of:freeDCtx
-
ZSTD_freeDCtx
public static long ZSTD_freeDCtx(long dctx)
Frees memory allocated bycreateDCtx.
-
nZSTD_decompressDCtx
public static long nZSTD_decompressDCtx(long ctx, long dst, long dstCapacity, long src, long srcSize)Unsafe version of:decompressDCtx
-
ZSTD_decompressDCtx
public static long ZSTD_decompressDCtx(long ctx, java.nio.ByteBuffer dst, java.nio.ByteBuffer src)
-
nZSTD_compress_usingDict
public static long nZSTD_compress_usingDict(long ctx, long dst, long dstCapacity, long src, long srcSize, long dict, long dictSize, int compressionLevel)Unsafe version of:compress_usingDict
-
ZSTD_compress_usingDict
public static long ZSTD_compress_usingDict(long ctx, java.nio.ByteBuffer dst, java.nio.ByteBuffer src, @Nullable java.nio.ByteBuffer dict, int compressionLevel)Compression using a predefined Dictionary (seedictBuilder/zdict.h).This function loads the dictionary, resulting in significant startup delay.
When
dict == NULL || dictSize < 8no dictionary is used.
-
nZSTD_decompress_usingDict
public static long nZSTD_decompress_usingDict(long dctx, long dst, long dstCapacity, long src, long srcSize, long dict, long dictSize)Unsafe version of:decompress_usingDict
-
ZSTD_decompress_usingDict
public static long ZSTD_decompress_usingDict(long dctx, java.nio.ByteBuffer dst, java.nio.ByteBuffer src, @Nullable java.nio.ByteBuffer dict)Decompression using a predefined Dictionary (seedictBuilder/zdict.h). Dictionary must be identical to the one used during compression.This function loads the dictionary, resulting in significant startup delay.
When
dict == NULL || dictSize < 8no dictionary is used.
-
nZSTD_createCDict
public static long nZSTD_createCDict(long dictBuffer, long dictSize, int compressionLevel)Unsafe version of:createCDict
-
ZSTD_createCDict
public static long ZSTD_createCDict(java.nio.ByteBuffer dictBuffer, int compressionLevel)When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once.ZSTD_createCDict()will create a digested dictionary, ready to start future compression operations without startup delay.ZSTD_CDictcan be created once and shared by multiple threads concurrently, since its usage is read-only.dictBuffercan be released afterZSTD_CDictcreation, since its content is copied within CDict.
-
nZSTD_freeCDict
public static long nZSTD_freeCDict(long CDict)
Unsafe version of:freeCDict
-
ZSTD_freeCDict
public static long ZSTD_freeCDict(long CDict)
Frees memory allocated bycreateCDict.
-
nZSTD_compress_usingCDict
public static long nZSTD_compress_usingCDict(long cctx, long dst, long dstCapacity, long src, long srcSize, long cdict)Unsafe version of:compress_usingCDict
-
ZSTD_compress_usingCDict
public static long ZSTD_compress_usingCDict(long cctx, java.nio.ByteBuffer dst, java.nio.ByteBuffer src, long cdict)Compression using a digested Dictionary.Faster startup than
compress_usingDict, recommended when same dictionary is used multiple times. Note that compression level is decided during dictionary creation. Frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no)
-
nZSTD_createDDict
public static long nZSTD_createDDict(long dictBuffer, long dictSize)Unsafe version of:createDDict
-
ZSTD_createDDict
public static long ZSTD_createDDict(java.nio.ByteBuffer dictBuffer)
Creates a digested dictionary, ready to start decompression operation without startup delay.dictBuffercan be released afterDDictcreation, as its content is copied insideDDict.
-
nZSTD_freeDDict
public static long nZSTD_freeDDict(long ddict)
Unsafe version of:freeDDict
-
ZSTD_freeDDict
public static long ZSTD_freeDDict(long ddict)
Frees memory allocated withcreateDDict.
-
nZSTD_decompress_usingDDict
public static long nZSTD_decompress_usingDDict(long dctx, long dst, long dstCapacity, long src, long srcSize, long ddict)Unsafe version of:decompress_usingDDict
-
ZSTD_decompress_usingDDict
public static long ZSTD_decompress_usingDDict(long dctx, java.nio.ByteBuffer dst, java.nio.ByteBuffer src, long ddict)Decompression using a digested Dictionary.Faster startup than
decompress_usingDict, recommended when same dictionary is used multiple times.
-
ZSTD_createCStream
public static long ZSTD_createCStream()
-
nZSTD_freeCStream
public static long nZSTD_freeCStream(long zcs)
Unsafe version of:freeCStream
-
ZSTD_freeCStream
public static long ZSTD_freeCStream(long zcs)
Frees memory allocated bycreateCStream.
-
nZSTD_initCStream
public static long nZSTD_initCStream(long zcs, int compressionLevel)
-
ZSTD_initCStream
public static long ZSTD_initCStream(long zcs, int compressionLevel)
-
nZSTD_compressStream
public static long nZSTD_compressStream(long zcs, long output, long input)Unsafe version of:compressStream
-
ZSTD_compressStream
public static long ZSTD_compressStream(long zcs, ZSTDOutBuffer output, ZSTDInBuffer input)- Returns:
- a size hint, preferred
nbof bytes to use as input for next function call or an error code, which can be tested usingisError.Notes:
- it's just a hint, to help latency a little, any other value will work fine
- size hint is guaranteed to be ≤
CStreamInSize
-
nZSTD_flushStream
public static long nZSTD_flushStream(long zcs, long output)Unsafe version of:flushStream
-
ZSTD_flushStream
public static long ZSTD_flushStream(long zcs, ZSTDOutBuffer output)- Returns:
nbof bytes still present within internal buffer (0 if it's empty) or an error code, which can be tested usingisError
-
nZSTD_endStream
public static long nZSTD_endStream(long zcs, long output)Unsafe version of:endStream
-
ZSTD_endStream
public static long ZSTD_endStream(long zcs, ZSTDOutBuffer output)- Returns:
- 0 if frame fully completed and fully flushed, or > 0 if some data is still present within internal buffer (value is minimum size estimation for
remaining data to flush, but it could be more) or an error code, which can be tested using
isError
-
ZSTD_CStreamInSize
public static long ZSTD_CStreamInSize()
Returns the recommended size for input buffer.
-
ZSTD_CStreamOutSize
public static long ZSTD_CStreamOutSize()
Returns the recommended size for output buffer. Guarantee to successfully flush at least one complete compressed block in all circumstances.
-
ZSTD_createDStream
public static long ZSTD_createDStream()
-
nZSTD_freeDStream
public static long nZSTD_freeDStream(long zds)
Unsafe version of:freeDStream
-
ZSTD_freeDStream
public static long ZSTD_freeDStream(long zds)
Frees memory allocated bycreateDStream.
-
nZSTD_initDStream
public static long nZSTD_initDStream(long zds)
Unsafe version of:initDStream
-
ZSTD_initDStream
public static long ZSTD_initDStream(long zds)
- Returns:
- recommended first input size
-
nZSTD_decompressStream
public static long nZSTD_decompressStream(long zds, long output, long input)Unsafe version of:decompressStream
-
ZSTD_decompressStream
public static long ZSTD_decompressStream(long zds, ZSTDOutBuffer output, ZSTDInBuffer input)- Returns:
- 0 when a frame is completely decoded and fully flushed, an error code, which can be tested using
isError, any other value > 0, which means there is still some decoding to do to complete current frame. The return value is a suggested next input size (a hint to improve latency) that will never load more than the current frame.
-
ZSTD_DStreamInSize
public static long ZSTD_DStreamInSize()
Returns the recommended size for input buffer.
-
ZSTD_DStreamOutSize
public static long ZSTD_DStreamOutSize()
Returns the recommended size for output buffer. Guarantee to successfully flush at least one complete compressed block in all circumstances.
-
ZSTD_COMPRESSBOUND
public static long ZSTD_COMPRESSBOUND(long srcSize)
Pure Java version ofZSTD_compressBound(long).
-
-