Speed can be tuned dynamically, selecting an "acceleration" factor which trades compression ratio for faster speed. On the other end, a high
compression derivative, LZ4_HC, is also provided, trading CPU time for improved compression ratio. All versions feature the same decompression
speed.
LZ4 is also compatible with dictionary compression, and can ingest any input file as dictionary, including those created by Zstandard Dictionary Builder. (note: only the final 64KB are used).
The raw LZ4 block compression format is detailed within lz4_Block_format.
Arbitrarily long files or data streams are compressed using multiple blocks, for streaming requirements. These blocks are organized into a frame, defined into lz4_Frame_format. Interoperable versions of LZ4 must also respect the frame format.
It's possible to have input and output sharing the same buffer, for highly constrained memory environments. In both cases, it requires input to lay at the end of the buffer, and decompression to start at beginning of the buffer. Buffer size must feature some margin, hence be larger than final size.
|<------------------------buffer--------------------------------->|
|<-----------compressed data--------->|
|<-----------decompressed size------------------>|
|<----margin---->|
This technique is more useful for decompression, since decompressed size is typically larger, and margin is short.
In-place decompression will work inside any buffer which size is ≥ LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize). This presumes that
decompressedSize > compressedSize. Otherwise, it means compression actually expanded data, and it would be more efficient to store
such data with a flag indicating it's not compressed. This can happen when data is not compressible (already compressed, or encrypted).
For in-place compression, margin is larger, as it must be able to cope with both history preservation, requiring input data to remain unmodified up to
DISTANCE_MAX, and data expansion, which can happen when input is not compressible. As a consequence, buffer size requirements are much higher, and
memory savings offered by in-place compression are more limited.
There are ways to limit this cost for compression:
LZ4_DISTANCE_MAX. Note that it is a compile-time constant, so all compressions will apply this limit.
Lower values will reduce compression ratio, except when input_size < LZ4_DISTANCE_MAX, so it's a reasonable trick when inputs are known
to be small.dstCapacity parameter in LZ4_compress*(). When this size
is < LZ4_COMPRESSBOUND(inputSize), then compression can fail, in which case, the return code will be 0 (zero). The caller must be ready
for these cases to happen, and typically design a backup scheme to send data uncompressed.The combination of both techniques can significantly reduce the amount of margin required for in-place compression.
In-place compression can work in any buffer which size is ≥ (maxCompressedSize) with maxCompressedSize == LZ4_COMPRESSBOUND(srcSize)
for guaranteed compression success. COMPRESS_INPLACE_BUFFER_SIZE depends on both maxCompressedSize and LZ4_DISTANCE_MAX, so it's
possible to reduce memory requirements by playing with them.
| Modifier and Type | Field and Description |
|---|---|
| public static final int | LZ4_DISTANCE_MAX
History window size; can be user-defined at compile time. |
| public static final int | |
| public static final int | |
| public static final int | |
| public static final int | LZ4_MAX_INPUT_SIZE
Maximum input size. |
| public static final int | LZ4_MEMORY_USAGE
Memory usage formula : Increasing memory usage improves compression ratio, at the cost of speed. |
| public static final int | LZ4_MEMORY_USAGE_DEFAULT
Tuning parameters. |
| public static final int | LZ4_MEMORY_USAGE_MAX
Tuning parameters. |
| public static final int | LZ4_MEMORY_USAGE_MIN
Tuning parameters. |
| public static final int | |
| public static final int | |
| public static final int | LZ4_VERSION_MAJOR
Version number part. |
| public static final int | LZ4_VERSION_MINOR
Version number part. |
| public static final int | LZ4_VERSION_NUMBER
Version number. |
| public static final int | LZ4_VERSION_RELEASE
Version number part. |
| public static final String | LZ4_VERSION_STRING
Version string. |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public static void | LZ4_attach_dictionary(long workingStream, long dictionaryStream)
This is an experimental API that allows efficient use of a static dictionary many times. |
| public static int | Returns: the number of bytes written into bufferdest (necessarily ≤ maxOutputSize) or 0 if compression failsCompresses |
| public static int | Returns: nb bytes written intodest (necessarily ≤ targetDestSize) or 0 if compression failswill be modified to indicate how many bytes where read from srcSizePtr)source to fill dest. New value is necessarily ≤ input value.Reverse the logic: compresses as much data as possible from |
| public static int | LZ4_compress_fast(ByteBuffer src, ByteBuffer dst, int acceleration)
Same as |
| public static int | Returns: size of compressed block or 0 if there is an error (typically, cannot fit intodst). After an error, the stream status is undefined (invalid),
it can only be reset or freed.Compress |
| public static int | LZ4_compress_fast_extState(ByteBuffer state, ByteBuffer src, ByteBuffer dst, int acceleration)
Same as |
| public static int | LZ4_compress_fast_extState_fastReset(ByteBuffer state, ByteBuffer src, ByteBuffer dst, int acceleration)
A variant of |
| public static int | LZ4_COMPRESS_INPLACE_BUFFER_SIZE(int
is generally maxCompressedSize)COMPRESSBOUND(inputSize), but can be set to any lower value, with the risk that compression can fail (return code 0)
|
| public static int | |
| public static int | |
| public static native int | Returns: maximum output size in a "worst case" scenario or 0, if input size is incorrect (too large or negative)max supported value is inputSize)MAX_INPUT_SIZEProvides the maximum size that LZ4 compression may output in a "worst case" scenario (input data not compressible). |
| public static native long | |
| public static native long | |
| public static int | |
| public static native int | Returns: minimum ring buffer size, or 0 if there is an error (invalidmaxBlockSize)In a ring buffer scenario (optional), blocks are presumed decompressed next to each other up to the moment there is not enough remaining space for next
block ( |
| public static int | LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(int decompressedSize)
Note 2: margin is overestimated a bit, since it could use |
| public static int | |
| public static int | Returns: the number of bytes decompressed into destination buffer (necessarily ≤dstCapacity)If destination buffer is not large enough, decoding will stop and output an error code (negative value). |
| public static int | LZ4_decompress_safe_continue(long LZ4_streamDecode, ByteBuffer src, ByteBuffer dst)
These decoding functions allow decompression of consecutive blocks in "streaming" mode. |
| public static int | Returns: the number of bytes decoded indst (necessarily ≤ targetOutputSize). If source stream is detected malformed, function returns a
negative result.Decompresses an LZ4 compressed block, of size |
| public static int | LZ4_decompress_safe_partial_usingDict(ByteBuffer src, ByteBuffer dst, int targetOutputSize, ByteBuffer dictStart)
|
| public static int | LZ4_decompress_safe_usingDict(ByteBuffer src, ByteBuffer dst, ByteBuffer dictStart)
These decoding functions work the same as a combination of |
| public static int | |
| public static int | |
| public static long | |
| public static int | Returns: loaded dictionary size, in bytes (necessarily ≤ 64 KB)Use this function to reference a static dictionary into |
| public static void | LZ4_resetStream_fast(long streamPtr)
Use this to prepare an |
| public static int | Returns: saved dictionary size in bytes (necessarily ≤maxDictSize), or 0 if errorIf last 64KB data cannot be guaranteed to remain available at its current memory location, save it into a safer place ( |
| public static boolean | Returns: 1 if OK, 0 if errorAn |
| public static native int | |
| public static native int | |
| public static String | |
| public static native void | nLZ4_attach_dictionary(long workingStream, long dictionaryStream)
Unsafe version of: |
| public static native int | nLZ4_compress_default(long src, long dst, int
max supported value is srcSize, int MAX_INPUT_SIZEsize of buffer dstCapacity)dst (which must be already allocated)Unsafe version of: |
| public static native int | nLZ4_compress_destSize(long src, long dst, long
will be modified to indicate how many bytes where read from srcSizePtr, int targetDstSize)source to fill dest. New value is necessarily ≤ input value.Unsafe version of: |
| public static native int | nLZ4_compress_fast(long src, long dst, int srcSize, int dstCapacity, int acceleration)
Unsafe version of: |
| public static native int | nLZ4_compress_fast_continue(long streamPtr, long src, long dst, int srcSize, int dstCapacity, int acceleration)
Unsafe version of: |
| public static native int | nLZ4_compress_fast_extState(long state, long src, long dst, int srcSize, int dstCapacity, int acceleration)
Unsafe version of: |
| public static native int | nLZ4_compress_fast_extState_fastReset(long state, long src, long dst, int srcSize, int dstCapacity, int acceleration)
Unsafe version of: |
| public static native int | nLZ4_decompress_safe(long src, long dst, int
is the exact complete size of the compressed block compressedSize, int is the size of destination buffer (which must be already allocated), presumed an upper bound of decompressed size dstCapacity)Unsafe version of: |
| public static native int | nLZ4_decompress_safe_continue(long LZ4_streamDecode, long src, long dst, int srcSize, int dstCapacity)
Unsafe version of: |
| public static native int | nLZ4_decompress_safe_partial(long src, long dst, int compressedSize, int targetOutputSize, int dstCapacity)
Unsafe version of: |
| public static native int | nLZ4_decompress_safe_partial_usingDict(long src, long dst, int compressedSize, int targetOutputSize, int maxOutputSize, long dictStart, int dictSize)
Unsafe version of: |
| public static native int | nLZ4_decompress_safe_usingDict(long src, long dst, int srcSize, int dstCapacity, long dictStart, int dictSize)
Unsafe version of: |
| public static native int | |
| public static native int | |
| public static native long | |
| public static native int | |
| public static native void | |
| public static native int | |
| public static native int | nLZ4_setStreamDecode(long LZ4_streamDecode, long dictionary, int dictSize)
Unsafe version of: |
| public static native long |
| LZ4_DISTANCE_MAX | back to summary |
|---|---|
| public static final int LZ4_DISTANCE_MAX History window size; can be user-defined at compile time. | |
| LZ4_HASH_SIZE_U32 | back to summary |
|---|---|
| public static final int LZ4_HASH_SIZE_U32 | |
| LZ4_HASHLOG | back to summary |
|---|---|
| public static final int LZ4_HASHLOG | |
| LZ4_HASHTABLESIZE | back to summary |
|---|---|
| public static final int LZ4_HASHTABLESIZE | |
| LZ4_MAX_INPUT_SIZE | back to summary |
|---|---|
| public static final int LZ4_MAX_INPUT_SIZE Maximum input size. | |
| LZ4_MEMORY_USAGE | back to summary |
|---|---|
| public static final int LZ4_MEMORY_USAGE Memory usage formula : Increasing memory usage improves compression ratio, at the cost of speed. Reduced memory usage may improve speed at the cost of ratio, thanks to better cache locality. Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache. | |
| LZ4_MEMORY_USAGE_DEFAULT | back to summary |
|---|---|
| public static final int LZ4_MEMORY_USAGE_DEFAULT Tuning parameters. | |
| LZ4_MEMORY_USAGE_MAX | back to summary |
|---|---|
| public static final int LZ4_MEMORY_USAGE_MAX Tuning parameters. | |
| LZ4_MEMORY_USAGE_MIN | back to summary |
|---|---|
| public static final int LZ4_MEMORY_USAGE_MIN Tuning parameters. | |
| LZ4_STREAM_MINSIZE | back to summary |
|---|---|
| public static final int LZ4_STREAM_MINSIZE | |
| LZ4_STREAMDECODE_MINSIZE | back to summary |
|---|---|
| public static final int LZ4_STREAMDECODE_MINSIZE | |
| LZ4_VERSION_MAJOR | back to summary |
|---|---|
| public static final int LZ4_VERSION_MAJOR Version number part. | |
| LZ4_VERSION_MINOR | back to summary |
|---|---|
| public static final int LZ4_VERSION_MINOR Version number part. | |
| LZ4_VERSION_NUMBER | back to summary |
|---|---|
| public static final int LZ4_VERSION_NUMBER Version number. | |
| LZ4_VERSION_RELEASE | back to summary |
|---|---|
| public static final int LZ4_VERSION_RELEASE Version number part. | |
| LZ4_VERSION_STRING | back to summary |
|---|---|
| public static final String LZ4_VERSION_STRING Version string. | |
| LZ4 | back to summary |
|---|---|
| protected LZ4() | |
| LZ4_attach_dictionary | back to summary |
|---|---|
| public static void LZ4_attach_dictionary(long workingStream, long dictionaryStream) This is an experimental API that allows efficient use of a static dictionary many times. Rather than re-loading the dictionary buffer into a working context before each compression, or copying a pre-loaded dictionary's Several assumptions are made about the state of the dictionary stream. Currently, only streams which have been prepared by Alternatively, the provided If a dictionary is provided, it replaces any pre-existing stream history. The dictionary contents are the only history that can be referenced and logically immediately precede the data compressed in the first subsequent compression call. The dictionary will only remain attached to the working stream through the first compression call, at the end of which it is cleared. The dictionary stream (and source buffer) must remain in-place / accessible / unchanged through the completion of the first compression call on the stream. | |
| LZ4_compress_default | back to summary |
|---|---|
| public static int LZ4_compress_default(ByteBuffer src, ByteBuffer dst) Compresses Compression is guaranteed to succeed if If the function cannot compress This function is protected against buffer overflow scenarios (never writes outside
| |
| LZ4_compress_destSize | back to summary |
|---|---|
| public static int LZ4_compress_destSize(ByteBuffer src, ByteBuffer dst, IntBuffer srcSizePtr) Reverse the logic: compresses as much data as possible from This function either compresses the entire Note acceleration parameter is fixed to
| |
| LZ4_compress_fast | back to summary |
|---|---|
| public static int LZ4_compress_fast(ByteBuffer src, ByteBuffer dst, int acceleration) Same as The larger the acceleration value, the faster the algorithm, but also the lesser the compression. It's a trade-off. It can be fine tuned, with each
successive value providing roughly +~3% to speed. An acceleration value of "1" is the same as regular | |
| LZ4_compress_fast_continue | back to summary |
|---|---|
| public static int LZ4_compress_fast_continue(long streamPtr, ByteBuffer src, ByteBuffer dst, int acceleration) Compress
Note 1: Each invocation to Note 2: The previous 64KB of source data is assumed to remain present, unmodified, at same address in memory! Note 3: When input is structured as a double-buffer, each buffer can have any size, including < 64 KB. Make sure that buffers are separated, by at least one byte. This construction ensures that each block only depends on previous block. Note 4: If input buffer is a ring-buffer, it can have any size, including < 64 KB.
| |
| LZ4_compress_fast_extState | back to summary |
|---|---|
| public static int LZ4_compress_fast_extState(ByteBuffer state, ByteBuffer src, ByteBuffer dst, int acceleration) Same as Use | |
| LZ4_compress_fast_extState_fastReset | back to summary |
|---|---|
| public static int LZ4_compress_fast_extState_fastReset(ByteBuffer state, ByteBuffer src, ByteBuffer dst, int acceleration) A variant of Using this variant avoids an expensive initialization step. It is only safe to call if the state buffer is known to be correctly initialized already
(see above comment on | |
| LZ4_COMPRESS_INPLACE_BUFFER_SIZE | back to summary |
|---|---|
public static int LZ4_COMPRESS_INPLACE_BUFFER_SIZE(int maxCompressedSize)
| |
| LZ4_COMPRESS_INPLACE_MARGIN | back to summary |
|---|---|
| public static int LZ4_COMPRESS_INPLACE_MARGIN() | |
| LZ4_COMPRESSBOUND | back to summary |
|---|---|
| public static int LZ4_COMPRESSBOUND(int isize) See | |
| LZ4_compressBound | back to summary |
|---|---|
| public static native int LZ4_compressBound(int inputSize) Provides the maximum size that LZ4 compression may output in a "worst case" scenario (input data not compressible). This function is primarily useful for memory allocation purposes (destination buffer size). Macro Note that
| |
| LZ4_createStream | back to summary |
|---|---|
| public static native long LZ4_createStream() Allocates and initializes an
| |
| LZ4_createStreamDecode | back to summary |
|---|---|
| public static native long LZ4_createStreamDecode() Creates a streaming decompression tracking context. A tracking context can be re-used multiple times.
| |
| LZ4_DECODER_RING_BUFFER_SIZE | back to summary |
|---|---|
| public static int LZ4_DECODER_RING_BUFFER_SIZE(int maxBlockSize) For static allocation; | |
| LZ4_decoderRingBufferSize | back to summary |
|---|---|
| public static native int LZ4_decoderRingBufferSize(int maxBlockSize) In a ring buffer scenario (optional), blocks are presumed decompressed next to each other up to the moment there is not enough remaining space for next
block (
| |
| LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE | back to summary |
|---|---|
| public static int LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(int decompressedSize) Note presumes that Note 2: margin is overestimated a bit, since it could use | |
| LZ4_DECOMPRESS_INPLACE_MARGIN | back to summary |
|---|---|
| public static int LZ4_DECOMPRESS_INPLACE_MARGIN(int compressedSize) | |
| LZ4_decompress_safe | back to summary |
|---|---|
| public static int LZ4_decompress_safe(ByteBuffer src, ByteBuffer dst) If destination buffer is not large enough, decoding will stop and output an error code (negative value). If the source stream is detected malformed, the function will stop decoding and return a negative result. Note 1: This function is protected against malicious data packets: it will never write outside Note 2:
| |
| LZ4_decompress_safe_continue | back to summary |
|---|---|
| public static int LZ4_decompress_safe_continue(long LZ4_streamDecode, ByteBuffer src, ByteBuffer dst) These decoding functions allow decompression of consecutive blocks in "streaming" mode. A block is an unsplittable entity, it must be presented entirely to a decompression function. Decompression functions only accept one block at a time. The last 64KB of previously decoded data must remain available and unmodified at the memory position where they were decoded. If less than 64KB of data has been decoded, all the data must be present. Special: if decompression side sets a ring buffer, it must respect one of the following conditions:
Whenever these conditions are not possible, save the last 64KB of decoded data into a safe buffer where it can't be modified during decompression, then
indicate where this data is saved using | |
| LZ4_decompress_safe_partial | back to summary |
|---|---|
| public static int LZ4_decompress_safe_partial(ByteBuffer src, ByteBuffer dst, int targetOutputSize) Decompresses an LZ4 compressed block, of size Up to Notes:
| |
| LZ4_decompress_safe_partial_usingDict | back to summary |
|---|---|
| public static int LZ4_decompress_safe_partial_usingDict(ByteBuffer src, ByteBuffer dst, int targetOutputSize, ByteBuffer dictStart) | |
| LZ4_decompress_safe_usingDict | back to summary |
|---|---|
| public static int LZ4_decompress_safe_usingDict(ByteBuffer src, ByteBuffer dst, ByteBuffer dictStart) These decoding functions work the same as a combination of Dictionary is presumed stable: it must remain accessible and unmodified during decompression. Performance tip: Decompression speed can be substantially increased when | |
| LZ4_freeStream | back to summary |
|---|---|
| public static int LZ4_freeStream(long streamPtr) Releases memory of an | |
| LZ4_freeStreamDecode | back to summary |
|---|---|
| public static int LZ4_freeStreamDecode(long LZ4_stream) Frees a streaming decompression tracking context. | |
| LZ4_initStream | back to summary |
|---|---|
| public static long LZ4_initStream(ByteBuffer buffer) An Use Note initialization fails if size and alignment conditions are not respected. In which case, the function will Note 2: An
| |
| LZ4_loadDict | back to summary |
|---|---|
| public static int LZ4_loadDict(long streamPtr, ByteBuffer dictionary) Use this function to reference a static dictionary into The dictionary must remain available during compression.
| |
| LZ4_resetStream_fast | back to summary |
|---|---|
| public static void LZ4_resetStream_fast(long streamPtr) Use this to prepare an An After init, start any new stream with
Note it's only useful to call
| |
| LZ4_saveDict | back to summary |
|---|---|
| public static int LZ4_saveDict(long streamPtr, ByteBuffer safeBuffer) If last 64KB data cannot be guaranteed to remain available at its current memory location, save it into a safer place ( This is schematically equivalent to a
| |
| LZ4_setStreamDecode | back to summary |
|---|---|
| public static boolean LZ4_setStreamDecode(long LZ4_streamDecode, ByteBuffer dictionary) An A dictionary can optionally be set. Use
| |
| LZ4_sizeofState | back to summary |
|---|---|
| public static native int LZ4_sizeofState() | |
| LZ4_versionNumber | back to summary |
|---|---|
| public static native int LZ4_versionNumber() Returns the version number. | |
| LZ4_versionString | back to summary |
|---|---|
| public static String LZ4_versionString() Returns the version string.
| |
| nLZ4_attach_dictionary | back to summary |
|---|---|
| public static native void nLZ4_attach_dictionary(long workingStream, long dictionaryStream) Unsafe version of: | |
| nLZ4_compress_default | back to summary |
|---|---|
| public static native int nLZ4_compress_default(long src, long dst, int srcSize, int dstCapacity) Unsafe version of:
| |
| nLZ4_compress_destSize | back to summary |
|---|---|
| public static native int nLZ4_compress_destSize(long src, long dst, long srcSizePtr, int targetDstSize) Unsafe version of:
| |
| nLZ4_compress_fast | back to summary |
|---|---|
| public static native int nLZ4_compress_fast(long src, long dst, int srcSize, int dstCapacity, int acceleration) Unsafe version of: | |
| nLZ4_compress_fast_continue | back to summary |
|---|---|
| public static native int nLZ4_compress_fast_continue(long streamPtr, long src, long dst, int srcSize, int dstCapacity, int acceleration) Unsafe version of: | |
| nLZ4_compress_fast_extState | back to summary |
|---|---|
| public static native int nLZ4_compress_fast_extState(long state, long src, long dst, int srcSize, int dstCapacity, int acceleration) Unsafe version of: | |
| nLZ4_compress_fast_extState_fastReset | back to summary |
|---|---|
| public static native int nLZ4_compress_fast_extState_fastReset(long state, long src, long dst, int srcSize, int dstCapacity, int acceleration) Unsafe version of: | |
| nLZ4_decompress_safe | back to summary |
|---|---|
| public static native int nLZ4_decompress_safe(long src, long dst, int compressedSize, int dstCapacity) Unsafe version of:
| |
| nLZ4_decompress_safe_continue | back to summary |
|---|---|
| public static native int nLZ4_decompress_safe_continue(long LZ4_streamDecode, long src, long dst, int srcSize, int dstCapacity) Unsafe version of: | |
| nLZ4_decompress_safe_partial | back to summary |
|---|---|
| public static native int nLZ4_decompress_safe_partial(long src, long dst, int compressedSize, int targetOutputSize, int dstCapacity) Unsafe version of: | |
| nLZ4_decompress_safe_partial_usingDict | back to summary |
|---|---|
| public static native int nLZ4_decompress_safe_partial_usingDict(long src, long dst, int compressedSize, int targetOutputSize, int maxOutputSize, long dictStart, int dictSize) Unsafe version of: | |
| nLZ4_decompress_safe_usingDict | back to summary |
|---|---|
| public static native int nLZ4_decompress_safe_usingDict(long src, long dst, int srcSize, int dstCapacity, long dictStart, int dictSize) Unsafe version of: | |
| nLZ4_freeStream | back to summary |
|---|---|
| public static native int nLZ4_freeStream(long streamPtr) Unsafe version of: | |
| nLZ4_freeStreamDecode | back to summary |
|---|---|
| public static native int nLZ4_freeStreamDecode(long LZ4_stream) Unsafe version of: | |
| nLZ4_initStream | back to summary |
|---|---|
| public static native long nLZ4_initStream(long buffer, long size) Unsafe version of: | |
| nLZ4_loadDict | back to summary |
|---|---|
| public static native int nLZ4_loadDict(long streamPtr, long dictionary, int dictSize) Unsafe version of: | |
| nLZ4_resetStream_fast | back to summary |
|---|---|
| public static native void nLZ4_resetStream_fast(long streamPtr) Unsafe version of: | |
| nLZ4_saveDict | back to summary |
|---|---|
| public static native int nLZ4_saveDict(long streamPtr, long safeBuffer, int maxDictSize) Unsafe version of: | |
| nLZ4_setStreamDecode | back to summary |
|---|---|
| public static native int nLZ4_setStreamDecode(long LZ4_streamDecode, long dictionary, int dictSize) Unsafe version of: | |
| nLZ4_versionString | back to summary |
|---|---|
| public static native long nLZ4_versionString() Unsafe version of: | |