Class LibdeflateNative
java.lang.Object
com.ljarocki.parallelzip.internal.LibdeflateNative
Bindings to a bundled native libdeflate build. libdeflate has no streaming
API (whole-buffer in, whole-buffer out), so this is only usable for the
in-memory fast path; the spilled/streamed path keeps using the JDK's
Deflater. Only linux-x64, linux-arm64, windows-x64, windows-arm64,
macos-arm64 and macos-x64 natives are bundled (the platforms this project's
CI builds and tests); every other platform/arch, or any failure while
loading, permanently falls back to pure Java with no behavioural change.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intSentinel returned bycompress(long, byte[], int, int, byte[], int, int, long[])/compressBatch(long, byte[][], byte[][], int[], long[], int)(as the entry's length) when the native incompressibility sniff judged a large input to be already compressed: the caller should STORE the whole entry instead of compressing it. -
Method Summary
Modifier and TypeMethodDescriptionstatic longallocCompressor(int level) Allocates a native compressor for the given DEFLATE level (1..12), to be reused across manycompress(long, byte[], int, int, byte[], int, int, long[])/compressBatch(long, byte[][], byte[][], int[], long[], int)calls -- typically one per worker thread, held for the life of a write.static booleanstatic intcompress(long handle, byte[] in, int inOff, int inLen, byte[] out, int outOff, int outCap, long[] crcOut) Compressesin[inOff, inOff+inLen)intoout[outOff, outOff+outCap)using raw DEFLATE, with the compressor identified byhandle.static voidcompressBatch(long handle, byte[][] ins, byte[][] outs, int[] outLens, long[] crcs, int count) Compressescountindependent buffers in one native call, reusinghandlefor all of them.static intcompressDirect(long handle, ByteBuffer in, int inLen, byte[] out, int outOff, int outCap, long[] crcOut) Compresses directly from a direct (typically memory-mapped)ByteBuffer--in[0, inLen)-- intoout[outOff, outOff+outCap), without ever copying the input through a JVM heapbyte[].static voidfreeCompressor(long handle) Releases a handle returned byallocCompressor(int).
-
Field Details
-
STORE_SENTINEL
public static final int STORE_SENTINELSentinel returned bycompress(long, byte[], int, int, byte[], int, int, long[])/compressBatch(long, byte[][], byte[][], int[], long[], int)(as the entry's length) when the native incompressibility sniff judged a large input to be already compressed: the caller should STORE the whole entry instead of compressing it. Distinct from the-1"call failed, use the JDK Deflater" signal.- See Also:
-
-
Method Details
-
available
public static boolean available() -
allocCompressor
public static long allocCompressor(int level) Allocates a native compressor for the given DEFLATE level (1..12), to be reused across manycompress(long, byte[], int, int, byte[], int, int, long[])/compressBatch(long, byte[][], byte[][], int[], long[], int)calls -- typically one per worker thread, held for the life of a write. Returns0on failure (e.g. out of memory); callers must treat that as "native unavailable" and fall back to the JDKDeflaterinstead of passing the zero handle through. -
freeCompressor
public static void freeCompressor(long handle) Releases a handle returned byallocCompressor(int). A no-op ifhandleis 0. -
compress
public static int compress(long handle, byte[] in, int inOff, int inLen, byte[] out, int outOff, int outCap, long[] crcOut) Compressesin[inOff, inOff+inLen)intoout[outOff, outOff+outCap)using raw DEFLATE, with the compressor identified byhandle. Returns the compressed length;STORE_SENTINEL(-2) if the sniff judged the input already compressed and it should be STOREd; or-1if the native call failed for any reason (e.g. the output buffer was too small), in which case callers should fall back to the JDKDeflater.crcOut[0]always receives the CRC-32 of the input, computed natively while the array is already pinned for compression -- regardless of the return value -- so callers never need a separateCRC32pass over the same bytes. -
compressBatch
public static void compressBatch(long handle, byte[][] ins, byte[][] outs, int[] outLens, long[] crcs, int count) Compressescountindependent buffers in one native call, reusinghandlefor all of them.ins[i]is compressed in full intoouts[i];outLens[i]receives the compressed length,STORE_SENTINEL(-2) if the sniff says to STORE it, or-1if that one entry failed and needs a JDKDeflaterfallback -- one entry's failure does not affect the others in the batch.crcs[i]always receives the CRC-32 ofins[i], computed natively alongside compression, regardless ofoutLens[i]. -
compressDirect
public static int compressDirect(long handle, ByteBuffer in, int inLen, byte[] out, int outOff, int outCap, long[] crcOut) Compresses directly from a direct (typically memory-mapped)ByteBuffer--in[0, inLen)-- intoout[outOff, outOff+outCap), without ever copying the input through a JVM heapbyte[]. Used for the large-entry fast path, whereinis aMappedByteBufferbacked by the OS page cache instead of abyte[]read viaFiles.readAllBytes(java.nio.file.Path). Same return convention ascompress(long, byte[], int, int, byte[], int, int, long[]), including the fusedcrcOut[0].inmust be direct; a non-direct buffer causes a native failure (-1), not a crash.
-