Liking cljdoc? Tell your friends :D

zstd-java

A pure-Java Zstandard compressor and decompressor without sun.misc.Unsafe and without native code. It runs on Java 17+, on any platform and byte order, and in GraalVM native images.

It is a port of aircompressor's Java implementation. aircompressor reads memory through sun.misc.Unsafe, whose memory-access methods are deprecated for removal (JEP 471/498: they warn on JDK 24+ and throw by default from JDK 26), and requires Java 22+ and a little endian platform. This port reads through VarHandle byte-array views instead, with the codec otherwise unchanged.

Frames are standard Zstandard frames: they are read by the reference C library (zstd-jni), Node's built-in zlib zstd and any other conforming decoder, and this library reads theirs.

Installation

Clojars Project

org.replikativ/zstd-java {:mvn/version "RELEASE"}
<repository><id>clojars</id><url>https://repo.clojars.org/</url></repository>
<dependency>
  <groupId>org.replikativ</groupId>
  <artifactId>zstd-java</artifactId>
  <version><!-- see Clojars --></version>
</dependency>

Java 17 or newer. No other dependencies.

Usage

import org.replikativ.zstd.ZstdJavaCompressor;
import org.replikativ.zstd.ZstdJavaDecompressor;

ZstdJavaCompressor compressor = new ZstdJavaCompressor();   // thread safe, reuse it
byte[] out = new byte[compressor.maxCompressedLength(input.length)];
int compressedSize = compressor.compress(input, 0, input.length, out, 0, out.length);

ZstdJavaDecompressor decompressor = new ZstdJavaDecompressor();
byte[] restored = new byte[input.length];
decompressor.decompress(out, 0, compressedSize, restored, 0, restored.length);

ZstdJavaOutputStream and ZstdJavaInputStream implement the streaming format. For many small blocks prefer the block API: a stream allocates its window buffers per instance.

The compressor implements zstd level 3 (zstd's default); the decompressor reads frames of any level.

Differences from aircompressor

  • Memory replaces sun.misc.Unsafe: a buffer is a byte[] and an address is an index into it. Multi-byte values are little endian on every platform.
  • ZstdJavaCompressor pools its compression contexts (hash tables, sequence buffers) instead of allocating them per call. An instance is safe to share between threads.
  • Huffman leaves are bucket sorted and byte histograms are counted with four interleaved arrays.
  • No MemorySegment API, no native implementation, no Hadoop codecs.

Fixes over aircompressor 3.16:

  • The compressor could write frames no decoder accepts: a Huffman table built for a block that is then stored raw was later reused (0.1.14).
  • Byte 0 is sorted with the other symbols when building Huffman codes; aircompressor gave it one of the shortest codes even when it never occurs. Output is about 0.4 % smaller on text and database nodes (0.1.15).
  • When the sequences of a block do not fit, the block is stored raw instead of throwing; a table of more than 128 raw Huffman weights is never written (0.1.15).
  • The decoder forgets the Huffman table between frames, accepts the two-byte form of a zero sequence count (aircompressor#422) and rejects data after it, checks the decoded size against the frame's content size, and rejects offset 0, the reserved header bit and oversized raw or RLE blocks (0.1.15).
  • Blocks are limited to the frame's window (as libzstd 1.5.7 does), and the stream decoder skips skippable frames and rejects data after the last frame (0.1.15).
  • Every corrupt-input failure, in the one-shot and the stream decoder, is a MalformedInputException (0.1.15).

Compressed output is therefore no longer byte-identical to aircompressor's.

Performance

Measured on 2,964 index nodes of a Datahike database (6.2 KB each) and the Canterbury and Calgary corpora, JDK 25, interleaved runs on a loaded machine (expect ±20 % between runs):

compressdecompressratio
this library, nodes0.6x zstd-jni0.73x zstd-jni5.80 (zstd-jni 5.82)
this library, corpus0.64x zstd-jni0.5x zstd-jni3.55 (zstd-jni 3.57)
this library vs aircompressor (Unsafe), nodes1.35-1.45x~1x0.5 % smaller

Testing

test/compat.clj uses zstd-jni (libzstd) as the oracle:

  • round trips in both directions at levels 1/3/9/19, with and without checksums;
  • the streaming classes and decompressed size;
  • zstd's own golden vectors (test/golden, BSD);
  • a directed harness that steers blocks to the boundary where a Huffman table is built but the block is stored raw. It still finds aircompressor's bug;
  • the same frames from eight threads sharing a compressor;
  • compressed size, which must not grow over aircompressor's;
  • mutated or truncated frames, which must decode or raise MalformedInputException, one-shot and streamed.
clojure -T:build javac
clojure -M:test -m compat                                   # generated inputs (CI)
clojure -M:test -m compat <corpus dirs...> <dir of .raw blobs>
clojure -M:smoke                                            # Java 17: zstd-jni round trips only
clojure -M:test -m bench <dir of .raw blobs> <corpus dir>

The full suite needs Java 22+ for its aircompressor size reference; :smoke runs on 17.

License

Apache License 2.0, as aircompressor. See LICENSE, NOTICE and NOTICE-aircompressor.md.

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close