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.
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.
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.
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.MalformedInputException rather than ArrayIndexOutOfBoundsException or
IllegalStateException.MemorySegment API, no native implementation, no Hadoop codecs.Compressed output is byte-identical to aircompressor's.
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):
| compress | decompress | ratio | |
|---|---|---|---|
| this library, nodes | 0.6x zstd-jni | 0.73x zstd-jni | 5.80 (zstd-jni 5.82) |
| this library, corpus | 0.64x zstd-jni | 0.5x zstd-jni | 3.55 (zstd-jni 3.57) |
| this library vs aircompressor (Unsafe), nodes | 1.35-1.45x | ~1x | same bytes |
test/compat.clj checks, against zstd-jni: round trips in both directions at
levels 1/3/9/19 with and without checksums, the streaming classes, decompressed
size, byte-identical output to aircompressor (also with eight threads sharing a
compressor), and mutated or truncated frames, which must decode or raise
MalformedInputException.
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 oracle; :smoke runs on 17.
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
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |