Interface ZstdLibrary
public interface ZstdLibrary
FFM binding for a subset of libzstd.
Java long parameters and return values map to C size_t, which is 8 bytes on every
64-bit platform Elasticsearch ships on. Binding these as int would leave the upper bits of
the argument register undefined and cause libzstd to read garbage as part of the size — see
#150690 for context.
The *Heap variants bind the same C symbol with @Critical so the linker
can use the fast path when the caller passes on-heap memory segments.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionlongcompress(MemorySegment dst, long dstCap, MemorySegment src, long srcSize, int level) Compresssrcas a single zstd frame intodst.longcompressBound(long srcSize) Maximum compressed size for a given source size, in the worst-case single-pass scenario.longcompressHeap(MemorySegment dst, long dstCap, MemorySegment src, long srcSize, int level) Heap-friendly variant ofcompress(java.lang.foreign.MemorySegment, long, java.lang.foreign.MemorySegment, long, int): same C symbol bound with thecriticallinker option so on-heapMemorySegmentarguments avoid a JNI copy.Allocate a streaming decompression context (ZSTD_DStream).longdecompress(MemorySegment dst, long dstCap, MemorySegment src, long srcSize) Decompress one or more complete zstd frames insrcintodst.longdecompressHeap(MemorySegment dst, long dstCap, MemorySegment src, long srcSize) Heap-friendly variant ofdecompress(java.lang.foreign.MemorySegment, long, java.lang.foreign.MemorySegment, long): same C symbol bound with thecriticallinker option so on-heapMemorySegmentarguments avoid a JNI copy.longdecompressStream(MemorySegment dstream, MemorySegment output, MemorySegment input) Drive one step of streaming decompression.longRecommended input buffer size for streaming decompression.longRecommended output buffer size for streaming decompression.longfreeDStream(MemorySegment dstream) Release a streaming decompression context previously obtained fromcreateDStream().getErrorName(long code) Returns a human-readable name for the error encoded incode, or"No Error"whencodeis not an error.booleanisError(long code) Tests whether asize_treturn value from the one-shot or streaming APIs encodes an error.
-
Method Details
-
compressBound
long compressBound(long srcSize) Maximum compressed size for a given source size, in the worst-case single-pass scenario.- See Also:
-
compress
Compresssrcas a single zstd frame intodst.- Returns:
- the compressed size written into
dst(≤dstCap), or an error code testable withisError(long). - See Also:
-
compressHeap
Heap-friendly variant ofcompress(java.lang.foreign.MemorySegment, long, java.lang.foreign.MemorySegment, long, int): same C symbol bound with thecriticallinker option so on-heapMemorySegmentarguments avoid a JNI copy. On JDK 21 the critical linker option is unavailable, so the binding is wrapped byZstdHeapFallback.compressHeap(java.lang.invoke.MethodHandle, java.lang.foreign.MemorySegment, long, java.lang.foreign.MemorySegment, long, int)which stages the heap segments off-heap before the libzstd call. -
decompress
Decompress one or more complete zstd frames insrcintodst.srcSizemust be the exact size of the compressed input.- Returns:
- the number of bytes written into
dst(≤dstCap), or an error code testable withisError(long). - See Also:
-
decompressHeap
Heap-friendly variant ofdecompress(java.lang.foreign.MemorySegment, long, java.lang.foreign.MemorySegment, long): same C symbol bound with thecriticallinker option so on-heapMemorySegmentarguments avoid a JNI copy. On JDK 21 the critical linker option is unavailable, so the binding is wrapped byZstdHeapFallback.decompressHeap(java.lang.invoke.MethodHandle, java.lang.foreign.MemorySegment, long, java.lang.foreign.MemorySegment, long)which stages the heap segments off-heap before the libzstd call. -
isError
boolean isError(long code) Tests whether asize_treturn value from the one-shot or streaming APIs encodes an error.- See Also:
-
getErrorName
Returns a human-readable name for the error encoded incode, or"No Error"whencodeis not an error.- See Also:
-
createDStream
MemorySegment createDStream()Allocate a streaming decompression context (ZSTD_DStream). ReturnsNULL(i.e. a zero address) on allocation failure. The returned segment must be released withfreeDStream(MemorySegment).- See Also:
-
freeDStream
Release a streaming decompression context previously obtained fromcreateDStream(). Accepts aNULLaddress as a no-op.- See Also:
-
dStreamInSize
long dStreamInSize()Recommended input buffer size for streaming decompression.- See Also:
-
dStreamOutSize
long dStreamOutSize()Recommended output buffer size for streaming decompression. libzstd guarantees this is enough to flush at least one complete decompressed block.- See Also:
-
decompressStream
Drive one step of streaming decompression. Called repeatedly with the same context, advancing theposfields on theZSTD_outBuffer/ZSTD_inBufferstructs.- Returns:
0when a frame is completely decoded and fully flushed, an error code testable withisError(long), or any other positive value, which is a hint at how many more input bytes are expected to complete the current frame.- See Also:
-