Skip to content

Commit

Permalink
Add ZstdCompression based on Apache Commons Compress
Browse files Browse the repository at this point in the history
Note tests will fail since the optional dependency zstd-jni
is not a dependency.
  • Loading branch information
mkitti committed Dec 3, 2023
1 parent 79f065d commit a4a5ac9
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
90 changes: 90 additions & 0 deletions src/main/java/org/janelia/saalfeldlab/n5/ZstdCompression.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.janelia.saalfeldlab.n5;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.compress.compressors.zstandard.ZstdCompressorInputStream;
import org.apache.commons.compress.compressors.zstandard.ZstdCompressorOutputStream;
import org.janelia.saalfeldlab.n5.Compression.CompressionType;

/**
* Zstandard compression for N5
*
* Implementation wrapper around Apache Commons Compress
*
* Note that zstd-jni is an optional dependency of Apache Commons Compress.
* However, it is required for this class, ZstdCompression, to work
* https://github.com/luben/zstd-jni
*
* Add the following dependency entry under to the Maven pom.xml
*
* <dependency>
* <groupId>com.github.luben</groupId>
* <artifactId>zstd-jni</artifactId>
* <version>1.5.5-10</version>
* </dependency>
*
* See the Zstandard manual for details on parameters.
* https://facebook.github.io/zstd/zstd_manual.html
*
* @author mkitti
*
*/
@CompressionType("zstd")
public class ZstdCompression implements DefaultBlockReader, DefaultBlockWriter, Compression {

/**
*
*/
private static final long serialVersionUID = 8592416400988371189L;

/*
* Compression level
*
* Standard compression level is between 1 and 22
* Negative compression levels offer speed
*
* Note that zarr-developers/numcodecs defaults to 1
*
* Default: 3 (see ZSTD_CLEVEL_DEFAULT)
*/
@CompressionParameter
private int level = 3;

/*
* Default compression level from zstd.h
*/
public static final int ZSTD_CLEVEL_DEFAULT = 3;

public ZstdCompression() {
this.level = ZSTD_CLEVEL_DEFAULT;
}

public ZstdCompression(int level) {
this.level = level;
}

@Override
public BlockReader getReader() {
return this;
}

@Override
public BlockWriter getWriter() {
return this;
}

@Override
public OutputStream getOutputStream(OutputStream out) throws IOException {
ZstdCompressorOutputStream zstdOut = new ZstdCompressorOutputStream(out, level);
return zstdOut;
}

@Override
public InputStream getInputStream(InputStream in) throws IOException {
ZstdCompressorInputStream zstdIn = new ZstdCompressorInputStream(in);
return zstdIn;
}

}
3 changes: 2 additions & 1 deletion src/test/java/org/janelia/saalfeldlab/n5/AbstractN5Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ protected Compression[] getCompressions() {
new GzipCompression(),
new GzipCompression(5, true),
new Lz4Compression(),
new XzCompression()
new XzCompression(),
new ZstdCompression()
};
}

Expand Down

0 comments on commit a4a5ac9

Please sign in to comment.