Class LibdeflateNative

java.lang.Object
com.ljarocki.parallelzip.internal.LibdeflateNative

public final class LibdeflateNative extends Object
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 Details

  • 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 many compress(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. Returns 0 on failure (e.g. out of memory); callers must treat that as "native unavailable" and fall back to the JDK Deflater instead of passing the zero handle through.
    • freeCompressor

      public static void freeCompressor(long handle)
      Releases a handle returned by allocCompressor(int). A no-op if handle is 0.
    • compress

      public static int compress(long handle, byte[] in, int inOff, int inLen, byte[] out, int outOff, int outCap, long[] crcOut)
      Compresses in[inOff, inOff+inLen) into out[outOff, outOff+outCap) using raw DEFLATE, with the compressor identified by handle. Returns the compressed length; STORE_SENTINEL (-2) if the sniff judged the input already compressed and it should be STOREd; or -1 if the native call failed for any reason (e.g. the output buffer was too small), in which case callers should fall back to the JDK Deflater. 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 separate CRC32 pass over the same bytes.
    • compressBatch

      public static void compressBatch(long handle, byte[][] ins, byte[][] outs, int[] outLens, long[] crcs, int count)
      Compresses count independent buffers in one native call, reusing handle for all of them. ins[i] is compressed in full into outs[i]; outLens[i] receives the compressed length, STORE_SENTINEL (-2) if the sniff says to STORE it, or -1 if that one entry failed and needs a JDK Deflater fallback -- one entry's failure does not affect the others in the batch. crcs[i] always receives the CRC-32 of ins[i], computed natively alongside compression, regardless of outLens[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) -- into out[outOff, outOff+outCap), without ever copying the input through a JVM heap byte[]. Used for the large-entry fast path, where in is a MappedByteBuffer backed by the OS page cache instead of a byte[] read via Files.readAllBytes(java.nio.file.Path). Same return convention as compress(long, byte[], int, int, byte[], int, int, long[]), including the fused crcOut[0]. in must be direct; a non-direct buffer causes a native failure (-1), not a crash.