Top Description Fields Constructors Methods
org.lwjgl.util.lz4

public Class LZ4

extends Object
Class Inheritance
Imports
javax.annotation.*, java.nio.*, org.lwjgl.system.*

Native bindings to LZ4, a lossless compression algorithm, providing compression speed > 500 MB/s per core, scalable with multi-cores CPU. It features an extremely fast decoder, with speed in multiple GB/s per core, typically reaching RAM speed limits on multi-core systems.

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.

In-place compression and decompression

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:

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.

Field Summary

Modifier and TypeField 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 : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; )

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.

Constructor Summary

AccessConstructor and Description
protected
LZ4()

Method Summary

Modifier and TypeMethod 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 buffer dest (necessarily ≤ maxOutputSize) or 0 if compression fails
LZ4_compress_default
(ByteBuffer src, ByteBuffer dst)

Compresses srcSize bytes from buffer src into already allocated dst buffer of size dstCapacity.

public static int

Returns:

nb bytes written into dest (necessarily ≤ targetDestSize) or 0 if compression fails
LZ4_compress_destSize
(ByteBuffer src, ByteBuffer dst, IntBuffer
will be modified to indicate how many bytes where read from source to fill dest. New value is necessarily ≤ input value.
srcSizePtr
)

Reverse the logic: compresses as much data as possible from src buffer into already allocated buffer dst of size targetDstSize.

public static int
LZ4_compress_fast(ByteBuffer src, ByteBuffer dst, int acceleration)

Same as compress_default, but allows selection of "acceleration" factor.

public static int

Returns:

size of compressed block or 0 if there is an error (typically, cannot fit into dst). After an error, the stream status is undefined (invalid), it can only be reset or freed.
LZ4_compress_fast_continue
(long streamPtr, ByteBuffer src, ByteBuffer dst, int acceleration)

Compress src content using data from previously compressed blocks, for better compression ratio.

public static int
LZ4_compress_fast_extState(ByteBuffer state, ByteBuffer src, ByteBuffer dst, int acceleration)

Same as compress_fast, using an externally allocated memory space for its state.

public static int
public static int
LZ4_COMPRESS_INPLACE_BUFFER_SIZE(int
is generally COMPRESSBOUND(inputSize), but can be set to any lower value, with the risk that compression can fail (return code 0)
maxCompressedSize
)

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)
LZ4_compressBound
(int
max supported value is MAX_INPUT_SIZE
inputSize
)

Provides the maximum size that LZ4 compression may output in a "worst case" scenario (input data not compressible).

public static native long
LZ4_createStream()

Allocates and initializes an LZ4_stream_t structure.

public static native long
LZ4_createStreamDecode()

Creates a streaming decompression tracking context.

public static int
LZ4_DECODER_RING_BUFFER_SIZE(int maxBlockSize)

For static allocation; maxBlockSize presumed valid.

public static native int

Returns:

minimum ring buffer size, or 0 if there is an error (invalid maxBlockSize)
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 (remainingSize < maxBlockSize), at which stage it resumes from beginning of ring buffer.

public static int
LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(int decompressedSize)

Note 2: margin is overestimated a bit, since it could use compressedSize instead.

public static int
LZ4_DECOMPRESS_INPLACE_MARGIN(int compressedSize)

public static int

Returns:

the number of bytes decompressed into destination buffer (necessarily ≤ dstCapacity)
LZ4_decompress_safe
(ByteBuffer src, ByteBuffer dst)

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 in dst (necessarily ≤ targetOutputSize). If source stream is detected malformed, function returns a negative result.
LZ4_decompress_safe_partial
(ByteBuffer src, ByteBuffer dst, int targetOutputSize)

Decompresses an LZ4 compressed block, of size srcSize at position src, into destination buffer dst of size dstCapacity.

public static int
public static int
LZ4_decompress_safe_usingDict(ByteBuffer src, ByteBuffer dst, ByteBuffer dictStart)

These decoding functions work the same as a combination of setStreamDecode followed by LZ4_decompress_*_continue().

public static int
LZ4_freeStream(long streamPtr)

Releases memory of an LZ4_stream_t structure.

public static int
LZ4_freeStreamDecode(long LZ4_stream)

Frees a streaming decompression tracking context.

public static long
LZ4_initStream(ByteBuffer buffer)

An LZ4_stream_t structure must be initialized at least once.

public static int

Returns:

loaded dictionary size, in bytes (necessarily ≤ 64 KB)
LZ4_loadDict
(long streamPtr, ByteBuffer dictionary)

Use this function to reference a static dictionary into LZ4_stream_t.

public static void
LZ4_resetStream_fast(long streamPtr)

Use this to prepare an LZ4_stream_t for a new chain of dependent blocks (e.g., compress_fast_continue).

public static int

Returns:

saved dictionary size in bytes (necessarily ≤ maxDictSize), or 0 if error
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 (char* safeBuffer).

public static boolean

Returns:

1 if OK, 0 if error
LZ4_setStreamDecode
(long LZ4_streamDecode, ByteBuffer dictionary)

An LZ4_streamDecode_t context can be allocated once and re-used multiple times.

public static native int
public static native int
LZ4_versionNumber()

Returns the version number.

public static String
LZ4_versionString()

Returns the version string.

public static native void
nLZ4_attach_dictionary(long workingStream, long dictionaryStream)

Unsafe version of: attach_dictionary

public static native int
nLZ4_compress_default(long src, long dst, int
max supported value is MAX_INPUT_SIZE
srcSize
,
int
size of buffer dst (which must be already allocated)
dstCapacity
)

Unsafe version of: compress_default

public static native int
nLZ4_compress_destSize(long src, long dst, long
will be modified to indicate how many bytes where read from source to fill dest. New value is necessarily ≤ input value.
srcSizePtr
,
int targetDstSize)

Unsafe version of: compress_destSize

public static native int
nLZ4_compress_fast(long src, long dst, int srcSize, int dstCapacity, int acceleration)

Unsafe version of: compress_fast

public static native int
nLZ4_compress_fast_continue(long streamPtr, long src, long dst, int srcSize, int dstCapacity, int acceleration)

Unsafe version of: compress_fast_continue

public static native int
nLZ4_compress_fast_extState(long state, long src, long dst, int srcSize, int dstCapacity, int acceleration)

Unsafe version of: compress_fast_extState

public static native int
nLZ4_compress_fast_extState_fastReset(long state, long src, long dst, int srcSize, int dstCapacity, int acceleration)

Unsafe version of: compress_fast_extState_fastReset

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: decompress_safe

public static native int
nLZ4_decompress_safe_continue(long LZ4_streamDecode, long src, long dst, int srcSize, int dstCapacity)

Unsafe version of: decompress_safe_continue

public static native int
nLZ4_decompress_safe_partial(long src, long dst, int compressedSize, int targetOutputSize, int dstCapacity)

Unsafe version of: decompress_safe_partial

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: decompress_safe_partial_usingDict

public static native int
nLZ4_decompress_safe_usingDict(long src, long dst, int srcSize, int dstCapacity, long dictStart, int dictSize)

Unsafe version of: decompress_safe_usingDict

public static native int
nLZ4_freeStream(long streamPtr)

Unsafe version of: freeStream

public static native int
nLZ4_freeStreamDecode(long LZ4_stream)

Unsafe version of: freeStreamDecode

public static native long
nLZ4_initStream(long buffer, long size)

Unsafe version of: initStream

public static native int
nLZ4_loadDict(long streamPtr, long dictionary, int dictSize)

Unsafe version of: loadDict

public static native void
nLZ4_resetStream_fast(long streamPtr)

Unsafe version of: resetStream_fast

public static native int
nLZ4_saveDict(long streamPtr, long safeBuffer, int maxDictSize)

Unsafe version of: saveDict

public static native int
nLZ4_setStreamDecode(long LZ4_streamDecode, long dictionary, int dictSize)

Unsafe version of: setStreamDecode

public static native long
nLZ4_versionString()

Unsafe version of: versionString

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

LZ4_DISTANCE_MAXback to summary
public static final int LZ4_DISTANCE_MAX

History window size; can be user-defined at compile time.

LZ4_HASH_SIZE_U32back to summary
public static final int LZ4_HASH_SIZE_U32
LZ4_HASHLOGback to summary
public static final int LZ4_HASHLOG
LZ4_HASHTABLESIZEback to summary
public static final int LZ4_HASHTABLESIZE
LZ4_MAX_INPUT_SIZEback to summary
public static final int LZ4_MAX_INPUT_SIZE

Maximum input size.

LZ4_MEMORY_USAGEback to summary
public static final int LZ4_MEMORY_USAGE

Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; )

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_DEFAULTback to summary
public static final int LZ4_MEMORY_USAGE_DEFAULT

Tuning parameters.

LZ4_MEMORY_USAGE_MAXback to summary
public static final int LZ4_MEMORY_USAGE_MAX

Tuning parameters.

LZ4_MEMORY_USAGE_MINback to summary
public static final int LZ4_MEMORY_USAGE_MIN

Tuning parameters.

LZ4_STREAM_MINSIZEback to summary
public static final int LZ4_STREAM_MINSIZE
LZ4_STREAMDECODE_MINSIZEback to summary
public static final int LZ4_STREAMDECODE_MINSIZE
LZ4_VERSION_MAJORback to summary
public static final int LZ4_VERSION_MAJOR

Version number part.

LZ4_VERSION_MINORback to summary
public static final int LZ4_VERSION_MINOR

Version number part.

LZ4_VERSION_NUMBERback to summary
public static final int LZ4_VERSION_NUMBER

Version number.

LZ4_VERSION_RELEASEback to summary
public static final int LZ4_VERSION_RELEASE

Version number part.

LZ4_VERSION_STRINGback to summary
public static final String LZ4_VERSION_STRING

Version string.

Constructor Detail

LZ4back to summary
protected LZ4()

Method Detail

LZ4_attach_dictionaryback 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 LZ4_stream_t into a working LZ4_stream_t, this function introduces a no-copy setup mechanism, in which the working stream references the dictionary stream in-place.

Several assumptions are made about the state of the dictionary stream. Currently, only streams which have been prepared by loadDict should be expected to work.

Alternatively, the provided dictionaryStream may be NULL, in which case any existing dictionary stream is unset.

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_defaultback to summary
public static int LZ4_compress_default(ByteBuffer src, ByteBuffer dst)

Compresses srcSize bytes from buffer src into already allocated dst buffer of size dstCapacity.

Compression is guaranteed to succeed if dstCapacity ≥ compressBound(srcSize). It also runs faster, so it's a recommended setting.

If the function cannot compress src into a more limited dst budget, compression stops immediately, and the function result is zero. In which case, dst content is undefined (invalid).

This function is protected against buffer overflow scenarios (never writes outside dst buffer, nor read outside src buffer).

Returns:int

the number of bytes written into buffer dest (necessarily ≤ maxOutputSize) or 0 if compression fails

LZ4_compress_destSizeback to summary
public static int LZ4_compress_destSize(ByteBuffer src, ByteBuffer dst, IntBuffer srcSizePtr)

Reverse the logic: compresses as much data as possible from src buffer into already allocated buffer dst of size targetDstSize.

This function either compresses the entire src content into dst if it's large enough, or fill dst buffer completely with as much data as possible from src.

Note

acceleration parameter is fixed to "default".

Parameters
srcSizePtr:IntBuffer

will be modified to indicate how many bytes where read from source to fill dest. New value is necessarily ≤ input value.

Returns:int

nb bytes written into dest (necessarily ≤ targetDestSize) or 0 if compression fails

LZ4_compress_fastback to summary
public static int LZ4_compress_fast(ByteBuffer src, ByteBuffer dst, int acceleration)

Same as compress_default, but allows selection of "acceleration" factor.

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 compress_default. Values ≤ 0 will be replaced by LZ4_ACCELERATION_DEFAULT (currently == 1, see lz4.c). Values > LZ4_ACCELERATION_MAX will be replaced by LZ4_ACCELERATION_MAX (currently == 65537, see lz4.c).

LZ4_compress_fast_continueback to summary
public static int LZ4_compress_fast_continue(long streamPtr, ByteBuffer src, ByteBuffer dst, int acceleration)

Compress src content using data from previously compressed blocks, for better compression ratio.

dst buffer must be already allocated. If dstCapacity ≥ compressBound(srcSize), compression is guaranteed to succeed, and runs faster.

Note 1: Each invocation to LZ4_compress_fast_continue() generates a new block. Each block has precise boundaries. Each block must be decompressed separately, calling LZ4_decompress_*() with relevant metadata. It's not possible to append blocks together and expect a single invocation of LZ4_decompress_*() to decompress them together.

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.

Returns:int

size of compressed block or 0 if there is an error (typically, cannot fit into dst). After an error, the stream status is undefined (invalid), it can only be reset or freed.

LZ4_compress_fast_extStateback to summary
public static int LZ4_compress_fast_extState(ByteBuffer state, ByteBuffer src, ByteBuffer dst, int acceleration)

Same as compress_fast, using an externally allocated memory space for its state.

Use sizeofState to know how much memory must be allocated, and allocate it on 8-bytes boundaries (using malloc() typically). Then, provide it as void* state to compression function.

LZ4_compress_fast_extState_fastResetback to summary
public static int LZ4_compress_fast_extState_fastReset(ByteBuffer state, ByteBuffer src, ByteBuffer dst, int acceleration)

A variant of compress_fast_extState.

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 resetStream_fast for a definition of "correctly initialized"). From a high level, the difference is that this function initializes the provided state with a call to something like resetStream_fast while compress_fast_extState starts with a call to initStream.

LZ4_COMPRESS_INPLACE_BUFFER_SIZEback to summary
public static int LZ4_COMPRESS_INPLACE_BUFFER_SIZE(int maxCompressedSize)
Parameters
maxCompressedSize:int

is generally COMPRESSBOUND(inputSize), but can be set to any lower value, with the risk that compression can fail (return code 0)

LZ4_COMPRESS_INPLACE_MARGINback to summary
public static int LZ4_COMPRESS_INPLACE_MARGIN()
LZ4_COMPRESSBOUNDback to summary
public static int LZ4_COMPRESSBOUND(int isize)

See compressBound.

LZ4_compressBoundback 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 COMPRESSBOUND is also provided for compilation-time evaluation (stack memory allocation for example).

Note that compress_default compresses faster when dstCapacity is ≥ compressBound(srcSize)

Parameters
inputSize:int

max supported value is MAX_INPUT_SIZE

Returns:int

maximum output size in a "worst case" scenario or 0, if input size is incorrect (too large or negative)

LZ4_createStreamback to summary
public static native long LZ4_createStream()

Allocates and initializes an LZ4_stream_t structure.

Annotations
@NativeType:LZ4_stream_t *
LZ4_createStreamDecodeback to summary
public static native long LZ4_createStreamDecode()

Creates a streaming decompression tracking context.

A tracking context can be re-used multiple times.

Annotations
@NativeType:LZ4_streamDecode_t *
LZ4_DECODER_RING_BUFFER_SIZEback to summary
public static int LZ4_DECODER_RING_BUFFER_SIZE(int maxBlockSize)

For static allocation; maxBlockSize presumed valid.

LZ4_decoderRingBufferSizeback 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 (remainingSize < maxBlockSize), at which stage it resumes from beginning of ring buffer. When setting such a ring buffer for streaming decompression, provides the minimum size of this ring buffer to be compatible with any source respecting maxBlockSize condition.

Returns:int

minimum ring buffer size, or 0 if there is an error (invalid maxBlockSize)

Since
version 1.8.2
LZ4_DECOMPRESS_INPLACE_BUFFER_SIZEback to summary
public static int LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(int decompressedSize)

Note

presumes that compressedSize < decompressedSize.

Note 2: margin is overestimated a bit, since it could use compressedSize instead.

LZ4_DECOMPRESS_INPLACE_MARGINback to summary
public static int LZ4_DECOMPRESS_INPLACE_MARGIN(int compressedSize)
LZ4_decompress_safeback 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 dst buffer, nor read outside source buffer, even if the compressed block is maliciously modified to order the decoder to do these actions. In such case, the decoder stops immediately, and considers the compressed block malformed.

Note 2: compressedSize and dstCapacity must be provided to the function, the compressed block does not contain them. The implementation is free to send / store / derive this information in whichever way is most beneficial. If there is a need for a different format which bundles together both compressed data and its metadata, consider looking at lz4frame.h instead.

Returns:int

the number of bytes decompressed into destination buffer (necessarily ≤ dstCapacity)

LZ4_decompress_safe_continueback 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:

  • Decompression buffer size is at least decoderRingBufferSize(maxBlockSize). maxBlockSize is the maximum size of any single block. It can have any value > 16 bytes. In which case, encoding and decoding buffers do not need to be synchronized. Actually, data can be produced by any source compliant with LZ4 format specification, and respecting maxBlockSize.
  • Synchronized mode: Decompression buffer size is exactly the same as compression buffer size, and follows exactly same update rule (block boundaries at same positions), and decoding function is provided with exact decompressed size of each block (exception for last block of the stream), then decoding & encoding ring buffer can have any size, including small ones ( < 64 KB).
  • Decompression buffer is larger than encoding buffer, by a minimum of maxBlockSize more bytes. In which case, encoding and decoding buffers do not need to be synchronized, and encoding ring buffer can have any size, including small ones ( < 64 KB).

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 setStreamDecode, before decompressing next block.

LZ4_decompress_safe_partialback to summary
public static int LZ4_decompress_safe_partial(ByteBuffer src, ByteBuffer dst, int targetOutputSize)

Decompresses an LZ4 compressed block, of size srcSize at position src, into destination buffer dst of size dstCapacity.

Up to targetOutputSize bytes will be decoded. The function stops decoding on reaching this objective. This can be useful to boost performance whenever only the beginning of a block is required.

Notes:

  1. result can be < targetOutputSize, if compressed block contains less data.
  2. targetOutputSize must be ≤ dstCapacity
  3. this function effectively stops decoding on reaching targetOutputSize, so dstCapacity is kind of redundant. This is because in older versions of this function, decoding operation would still write complete sequences. Therefore, there was no guarantee that it would stop writing at exactly targetOutputSize, it could write more bytes, though only up to dstCapacity. Some "margin" used to be required for this operation to work properly. Thankfully, this is no longer necessary. The function nonetheless keeps the same signature, in an effort to preserve API compatibility.
  4. if srcSize is the exact size of the block, then targetOutputSize can be any value, including larger than the block's decompressed size. The function will, at most, generate block's decompressed size.
  5. if srcSize is larger than block's compressed size, then targetOutputSize MUST be ≤ block's decompressed size. Otherwise, silent corruption will occur.
Returns:int

the number of bytes decoded in dst (necessarily ≤ targetOutputSize). If source stream is detected malformed, function returns a negative result.

LZ4_decompress_safe_partial_usingDictback to summary
public static int LZ4_decompress_safe_partial_usingDict(ByteBuffer src, ByteBuffer dst, int targetOutputSize, ByteBuffer dictStart)

See decompress_safe_usingDict.

LZ4_decompress_safe_usingDictback 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 setStreamDecode followed by LZ4_decompress_*_continue(). They are stand-alone, and don't need an LZ4_streamDecode_t structure.

Dictionary is presumed stable: it must remain accessible and unmodified during decompression.

Performance tip: Decompression speed can be substantially increased when dst == dictStart + dictSize.

LZ4_freeStreamback to summary
public static int LZ4_freeStream(long streamPtr)

Releases memory of an LZ4_stream_t structure.

LZ4_freeStreamDecodeback to summary
public static int LZ4_freeStreamDecode(long LZ4_stream)

Frees a streaming decompression tracking context.

LZ4_initStreamback to summary
public static long LZ4_initStream(ByteBuffer buffer)

An LZ4_stream_t structure must be initialized at least once. This is automatically done when invoking createStream(), but it's not when the structure is simply declared on stack (for example).

Use LZ4_initStream() to properly initialize a newly declared LZ4_stream_t. It can also initialize any arbitrary buffer of sufficient size, and will return a pointer of proper type upon initialization.

Note

initialization fails if size and alignment conditions are not respected. In which case, the function will NULL.

Note 2: An LZ4_stream_t structure guarantees correct alignment and size.

Annotations
@NativeType:LZ4_stream_t *
Since
1.9.0
LZ4_loadDictback to summary
public static int LZ4_loadDict(long streamPtr, ByteBuffer dictionary)

Use this function to reference a static dictionary into LZ4_stream_t.

The dictionary must remain available during compression. LZ4_loadDict() triggers a reset, so any previous data will be forgotten. The same dictionary will have to be loaded on decompression side for successful decoding. Dictionarys are useful for better compression of small data (KB range). While LZ4 accepts any input as dictionary, results are generally better when using Zstandard's Dictionary Builder. Loading a size of 0 is allowed, and is the same as reset.

Returns:int

loaded dictionary size, in bytes (necessarily ≤ 64 KB)

LZ4_resetStream_fastback to summary
public static void LZ4_resetStream_fast(long streamPtr)

Use this to prepare an LZ4_stream_t for a new chain of dependent blocks (e.g., compress_fast_continue).

An LZ4_stream_t must be initialized once before usage. This is automatically done when created by createStream. However, should the LZ4_stream_t be simply declared on stack (for example), it's necessary to initialize it first, using initStream.

After init, start any new stream with LZ4_resetStream_fast(). A same LZ4_stream_t can be re-used multiple times consecutively and compress multiple streams, provided that it starts each new stream with LZ4_resetStream_fast().

LZ4_resetStream_fast() is much faster than LZ4_initStream(), but is not compatible with memory regions containing garbage data.

Note

it's only useful to call LZ4_resetStream_fast() in the context of streaming compression. The extState functions perform their own resets. Invoking LZ4_resetStream_fast() before is redundant, and even counterproductive.

Since
version 1.9.0
LZ4_saveDictback 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 (char* safeBuffer).

This is schematically equivalent to a memcpy() followed by loadDict, but is much faster, because LZ4_saveDict() doesn't need to rebuild tables.

Returns:int

saved dictionary size in bytes (necessarily ≤ maxDictSize), or 0 if error

LZ4_setStreamDecodeback to summary
public static boolean LZ4_setStreamDecode(long LZ4_streamDecode, ByteBuffer dictionary)

An LZ4_streamDecode_t context can be allocated once and re-used multiple times. Use this function to start decompression of a new stream of blocks.

A dictionary can optionally be set. Use NULL or size 0 for a reset order. Dictionary is presumed stable: it must remain accessible and unmodified during next decompression.

Returns:boolean

1 if OK, 0 if error

Annotations
@NativeType:int
LZ4_sizeofStateback to summary
public static native int LZ4_sizeofState()
LZ4_versionNumberback to summary
public static native int LZ4_versionNumber()

Returns the version number.

LZ4_versionStringback to summary
public static String LZ4_versionString()

Returns the version string.

Annotations
@NativeType:char const *
nLZ4_attach_dictionaryback to summary
public static native void nLZ4_attach_dictionary(long workingStream, long dictionaryStream)

Unsafe version of: attach_dictionary

nLZ4_compress_defaultback to summary
public static native int nLZ4_compress_default(long src, long dst, int srcSize, int dstCapacity)

Unsafe version of: compress_default

Parameters
srcSize:int

max supported value is MAX_INPUT_SIZE

dstCapacity:int

size of buffer dst (which must be already allocated)

nLZ4_compress_destSizeback to summary
public static native int nLZ4_compress_destSize(long src, long dst, long srcSizePtr, int targetDstSize)

Unsafe version of: compress_destSize

Parameters
srcSizePtr:long

will be modified to indicate how many bytes where read from source to fill dest. New value is necessarily ≤ input value.

nLZ4_compress_fastback to summary
public static native int nLZ4_compress_fast(long src, long dst, int srcSize, int dstCapacity, int acceleration)

Unsafe version of: compress_fast

nLZ4_compress_fast_continueback 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: compress_fast_continue

nLZ4_compress_fast_extStateback 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: compress_fast_extState

nLZ4_compress_fast_extState_fastResetback 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: compress_fast_extState_fastReset

nLZ4_decompress_safeback to summary
public static native int nLZ4_decompress_safe(long src, long dst, int compressedSize, int dstCapacity)

Unsafe version of: decompress_safe

Parameters
compressedSize:int

is the exact complete size of the compressed block

dstCapacity:int

is the size of destination buffer (which must be already allocated), presumed an upper bound of decompressed size

nLZ4_decompress_safe_continueback to summary
public static native int nLZ4_decompress_safe_continue(long LZ4_streamDecode, long src, long dst, int srcSize, int dstCapacity)

Unsafe version of: decompress_safe_continue

nLZ4_decompress_safe_partialback to summary
public static native int nLZ4_decompress_safe_partial(long src, long dst, int compressedSize, int targetOutputSize, int dstCapacity)

Unsafe version of: decompress_safe_partial

nLZ4_decompress_safe_partial_usingDictback 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: decompress_safe_partial_usingDict

nLZ4_decompress_safe_usingDictback 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: decompress_safe_usingDict

nLZ4_freeStreamback to summary
public static native int nLZ4_freeStream(long streamPtr)

Unsafe version of: freeStream

nLZ4_freeStreamDecodeback to summary
public static native int nLZ4_freeStreamDecode(long LZ4_stream)

Unsafe version of: freeStreamDecode

nLZ4_initStreamback to summary
public static native long nLZ4_initStream(long buffer, long size)

Unsafe version of: initStream

nLZ4_loadDictback to summary
public static native int nLZ4_loadDict(long streamPtr, long dictionary, int dictSize)

Unsafe version of: loadDict

nLZ4_resetStream_fastback to summary
public static native void nLZ4_resetStream_fast(long streamPtr)

Unsafe version of: resetStream_fast

nLZ4_saveDictback to summary
public static native int nLZ4_saveDict(long streamPtr, long safeBuffer, int maxDictSize)

Unsafe version of: saveDict

nLZ4_setStreamDecodeback to summary
public static native int nLZ4_setStreamDecode(long LZ4_streamDecode, long dictionary, int dictSize)

Unsafe version of: setStreamDecode

nLZ4_versionStringback to summary
public static native long nLZ4_versionString()

Unsafe version of: versionString