more friendly public API

This commit is contained in:
2020-04-26 22:14:31 +01:00
parent 01ac499e59
commit 82ea2f0175
17 changed files with 157 additions and 102 deletions

View File

@@ -3,7 +3,7 @@ package net.woggioni.jzstd;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor @RequiredArgsConstructor
public enum ZSTD_cParameter { public enum CompressionParameter {
compressionLevel(100), compressionLevel(100),
windowLog(101), windowLog(101),
hashLog(102), hashLog(102),

View File

@@ -3,10 +3,10 @@ package net.woggioni.jzstd;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor @RequiredArgsConstructor
public enum ZSTD_EndDirective { public enum EndDirective {
ZSTD_e_continue(0), Continue(0),
ZSTD_e_flush(1), Flush(1),
ZSTD_e_end(2); End(2);
public final int value; public final int value;
} }

View File

@@ -0,0 +1,32 @@
package net.woggioni.jzstd;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public enum ErrorCode {
error_no_error(0),
error_GENERIC(1),
error_prefix_unknown(10),
error_version_unsupported(12),
error_frameParameter_unsupported(14),
error_frameParameter_windowTooLarge(16),
error_corruption_detected(20),
error_checksum_wrong(22),
error_dictionary_corrupted(30),
error_dictionary_wrong(32),
error_dictionaryCreation_failed(34),
error_parameter_unsupported(40),
error_parameter_outOfBound(42),
error_tableLog_tooLarge(44),
error_maxSymbolValue_tooLarge(46),
error_maxSymbolValue_tooSmall(48),
error_stage_wrong(60),
error_init_missing(62),
error_memory_allocation(64),
error_workSpace_tooSmall(66),
error_dstSize_tooSmall(70),
error_srcSize_wrong(72),
error_dstBuffer_null(74);
public final int value;
}

View File

@@ -0,0 +1,13 @@
package net.woggioni.jzstd;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public enum ResetDirective {
reset_session_only(1),
reset_parameters(2),
reset_session_and_parameters(3);
public final int value;
}

View File

@@ -1,13 +0,0 @@
package net.woggioni.jzstd;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public enum ZSTD_ResetDirective {
ZSTD_reset_session_only(1),
ZSTD_reset_parameters(2),
ZSTD_reset_session_and_parameters(3);
public final int value;
}

View File

@@ -0,0 +1,22 @@
package net.woggioni.jzstd;
import net.woggioni.jzstd.internal.ZSTD_CCtx;
import net.woggioni.jzstd.internal.ZstdLibrary;
public class ZstdCompressionCtx implements AutoCloseable {
final ZSTD_CCtx ctx = ZstdLibrary.ZSTD_createCCtx();
public void setParameter(CompressionParameter compressionParameter, int value) {
ZstdLibrary.CCtx_setParameter(ctx, compressionParameter, value);
}
public void init() {
ZstdLibrary.CCtx_reset(ctx, ResetDirective.reset_session_only);
ZstdLibrary.CCtx_refCDict(ctx, null);
}
@Override
public void close() {
ZstdLibrary.freeCStream(ctx);
}
}

View File

@@ -0,0 +1,18 @@
package net.woggioni.jzstd;
import net.woggioni.jzstd.internal.ZSTD_DCtx;
import net.woggioni.jzstd.internal.ZstdLibrary;
public class ZstdDecompressionCtx implements AutoCloseable {
final ZSTD_DCtx ctx = ZstdLibrary.ZSTD_createDStream();
public void init() {
ZstdLibrary.DCtx_reset(ctx, ResetDirective.reset_session_only);
ZstdLibrary.DCtx_refDDict(ctx, null);
}
@Override
public void close() {
ZstdLibrary.freeDStream(ctx);
}
}

View File

@@ -0,0 +1,11 @@
package net.woggioni.jzstd;
public class ZstdException extends RuntimeException {
public ZstdException(String message) {
super(message);
}
public ZstdException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -10,7 +10,7 @@ import java.io.InputStream;
public class ZstdInputStream extends InputStream { public class ZstdInputStream extends InputStream {
private final InputStream source; private final InputStream source;
private final ZSTD_DCtx ctx; private final ZstdDecompressionCtx ctx;
private final boolean ctx_owner; private final boolean ctx_owner;
private final ZSTD_inBuffer input; private final ZSTD_inBuffer input;
@@ -26,7 +26,7 @@ public class ZstdInputStream extends InputStream {
private static final int outputBufferSize = ZstdLibrary.DStreamOutSize(); private static final int outputBufferSize = ZstdLibrary.DStreamOutSize();
private ZstdInputStream(InputStream source, private ZstdInputStream(InputStream source,
ZSTD_DCtx ctx, ZstdDecompressionCtx ctx,
boolean ctx_owner) { boolean ctx_owner) {
this.source = source; this.source = source;
this.ctx = ctx; this.ctx = ctx;
@@ -36,11 +36,11 @@ public class ZstdInputStream extends InputStream {
} }
public static ZstdInputStream from(InputStream source) { public static ZstdInputStream from(InputStream source) {
ZSTD_DCtx ctx = ZstdLibrary.ZSTD_createDStream(); ZstdDecompressionCtx ctx = new ZstdDecompressionCtx();
return new ZstdInputStream(source, ctx, true); return new ZstdInputStream(source, ctx, true);
} }
public static ZstdInputStream from(InputStream source, ZSTD_DCtx ctx) { public static ZstdInputStream from(InputStream source, ZstdDecompressionCtx ctx) {
return new ZstdInputStream(source, ctx, false); return new ZstdInputStream(source, ctx, false);
} }
@@ -67,7 +67,7 @@ public class ZstdInputStream extends InputStream {
input.size = new size_t(input.src.position()); input.size = new size_t(input.src.position());
output.pos = zero; output.pos = zero;
} }
int rc = ZstdLibrary.decompressStream(ctx, output, input); int rc = ZstdLibrary.decompressStream(ctx.ctx, output, input);
if(rc == 0) { if(rc == 0) {
state = State.CTX_FLUSHED; state = State.CTX_FLUSHED;
break; break;
@@ -105,6 +105,6 @@ public class ZstdInputStream extends InputStream {
@Override @Override
public void close() { public void close() {
if (ctx_owner) ZstdLibrary.freeDStream(ctx); if (ctx_owner) ctx.close();
} }
} }

View File

@@ -1,26 +1,23 @@
package net.woggioni.jzstd; package net.woggioni.jzstd;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import net.woggioni.jzstd.internal.ZSTD_inBuffer; import net.woggioni.jzstd.internal.*;
import net.woggioni.jzstd.internal.ZSTD_outBuffer;
import net.woggioni.jzstd.internal.ZstdLibrary;
import net.woggioni.jzstd.internal.size_t;
import java.io.OutputStream; import java.io.OutputStream;
public class ZstdOutputStream extends OutputStream { public class ZstdOutputStream extends OutputStream {
private final OutputStream sink; private final OutputStream sink;
private final ZSTD_CCtx ctx; private final ZstdCompressionCtx ctx;
private final boolean ctx_owner; private final boolean ctx_owner;
private final byte[] buffer; private final byte[] buffer;
private final ZSTD_inBuffer input; private final ZSTD_inBuffer input;
private final ZSTD_outBuffer output; private final ZSTD_outBuffer output;
private int pos; private int pos;
private ZSTD_EndDirective flag; private EndDirective flag;
private ZstdOutputStream(OutputStream sink, private ZstdOutputStream(OutputStream sink,
ZSTD_CCtx ctx, ZstdCompressionCtx ctx,
boolean ctx_owner, boolean ctx_owner,
int bufferSize) { int bufferSize) {
this.sink = sink; this.sink = sink;
@@ -30,20 +27,19 @@ public class ZstdOutputStream extends OutputStream {
this.input = new ZSTD_inBuffer(bufferSize); this.input = new ZSTD_inBuffer(bufferSize);
this.output = new ZSTD_outBuffer(bufferSize); this.output = new ZSTD_outBuffer(bufferSize);
this.pos = 0; this.pos = 0;
this.flag = ZSTD_EndDirective.ZSTD_e_continue; this.flag = EndDirective.Continue;
} }
public static ZstdOutputStream from(OutputStream sink, public static ZstdOutputStream from(OutputStream sink,
ZSTD_CCtx ctx) { ZstdCompressionCtx ctx) {
return from(sink, ctx, 0x10000); return from(sink, ctx, 0x10000);
} }
public static ZstdOutputStream from( public static ZstdOutputStream from(
OutputStream sink, OutputStream sink,
ZSTD_CCtx ctx, ZstdCompressionCtx ctx,
int bufferSize) { int bufferSize) {
ZstdLibrary.CCtx_reset(ctx, ZSTD_ResetDirective.ZSTD_reset_session_only); ctx.init();
ZstdLibrary.CCtx_refCDict(ctx, null);
return new ZstdOutputStream(sink, ctx, false, bufferSize); return new ZstdOutputStream(sink, ctx, false, bufferSize);
} }
@@ -59,14 +55,13 @@ public class ZstdOutputStream extends OutputStream {
public static ZstdOutputStream from(OutputStream sink, public static ZstdOutputStream from(OutputStream sink,
int compressionLevel, int compressionLevel,
int bufferSize) { int bufferSize) {
ZSTD_bounds range = ZstdLibrary.cParam_getBounds(ZSTD_cParameter.compressionLevel); ZSTD_bounds range = ZstdLibrary.cParam_getBounds(CompressionParameter.compressionLevel);
if (compressionLevel < range.lowerBound || compressionLevel > range.upperBound) { if (compressionLevel < range.lowerBound || compressionLevel > range.upperBound) {
throw new IllegalArgumentException("Compression level must be between " + throw new IllegalArgumentException("Compression level must be between " +
range.lowerBound + " and " + range.upperBound); range.lowerBound + " and " + range.upperBound);
} }
ZSTD_CCtx ctx = ZstdLibrary.ZSTD_createCCtx(); ZstdCompressionCtx ctx = new ZstdCompressionCtx();
ZstdLibrary.CCtx_setParameter(ctx, ctx.setParameter(CompressionParameter.compressionLevel, compressionLevel);
ZSTD_cParameter.compressionLevel, compressionLevel);
return new ZstdOutputStream(sink, ctx, true, bufferSize); return new ZstdOutputStream(sink, ctx, true, bufferSize);
} }
@@ -80,16 +75,16 @@ public class ZstdOutputStream extends OutputStream {
input.size = new size_t(pos); input.size = new size_t(pos);
pos = 0; pos = 0;
while (true) { while (true) {
size_t rc = ZstdLibrary.compressStream2(ctx, output, input, flag); size_t rc = ZstdLibrary.compressStream2(ctx.ctx, output, input, flag);
if (output.pos.longValue() > 0) { if (output.pos.longValue() > 0) {
output.dst.get(buffer); output.dst.get(buffer);
output.dst.position(0); output.dst.position(0);
sink.write(buffer, 0, output.pos.intValue()); sink.write(buffer, 0, output.pos.intValue());
output.pos = new size_t(0); output.pos = new size_t(0);
} }
if (input.pos.longValue() == input.size.longValue() && flag == ZSTD_EndDirective.ZSTD_e_continue) { if (input.pos.longValue() == input.size.longValue() && flag == EndDirective.Continue) {
break; break;
} else if (flag == ZSTD_EndDirective.ZSTD_e_end && rc.intValue() == 0) { } else if (flag == EndDirective.End && rc.intValue() == 0) {
break; break;
} }
} }
@@ -116,9 +111,9 @@ public class ZstdOutputStream extends OutputStream {
@Override @Override
@SneakyThrows @SneakyThrows
public void close() { public void close() {
flag = ZSTD_EndDirective.ZSTD_e_end; flag = EndDirective.End;
flush(); flush();
sink.close(); sink.close();
if (ctx_owner) ZstdLibrary.ZSTD_freeCCtx(ctx); if (ctx_owner) ctx.close();
} }
} }

View File

@@ -1,4 +1,4 @@
package net.woggioni.jzstd; package net.woggioni.jzstd.internal;
import com.sun.jna.PointerType; import com.sun.jna.PointerType;

View File

@@ -1,4 +1,4 @@
package net.woggioni.jzstd; package net.woggioni.jzstd.internal;
import com.sun.jna.PointerType; import com.sun.jna.PointerType;

View File

@@ -1,4 +1,4 @@
package net.woggioni.jzstd; package net.woggioni.jzstd.internal;
import com.sun.jna.PointerType; import com.sun.jna.PointerType;

View File

@@ -1,4 +1,4 @@
package net.woggioni.jzstd; package net.woggioni.jzstd.internal;
import com.sun.jna.PointerType; import com.sun.jna.PointerType;

View File

@@ -1,32 +0,0 @@
package net.woggioni.jzstd.internal;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public enum ZSTD_ErrorCode {
ZSTD_error_no_error(0),
ZSTD_error_GENERIC(1),
ZSTD_error_prefix_unknown(10),
ZSTD_error_version_unsupported(12),
ZSTD_error_frameParameter_unsupported(14),
ZSTD_error_frameParameter_windowTooLarge(16),
ZSTD_error_corruption_detected(20),
ZSTD_error_checksum_wrong(22),
ZSTD_error_dictionary_corrupted(30),
ZSTD_error_dictionary_wrong(32),
ZSTD_error_dictionaryCreation_failed(34),
ZSTD_error_parameter_unsupported(40),
ZSTD_error_parameter_outOfBound(42),
ZSTD_error_tableLog_tooLarge(44),
ZSTD_error_maxSymbolValue_tooLarge(46),
ZSTD_error_maxSymbolValue_tooSmall(48),
ZSTD_error_stage_wrong(60),
ZSTD_error_init_missing(62),
ZSTD_error_memory_allocation(64),
ZSTD_error_workSpace_tooSmall(66),
ZSTD_error_dstSize_tooSmall(70),
ZSTD_error_srcSize_wrong(72),
ZSTD_error_dstBuffer_null(74);
public final int value;
}

View File

@@ -1,7 +1,6 @@
package net.woggioni.jzstd; package net.woggioni.jzstd.internal;
import com.sun.jna.Structure; import com.sun.jna.Structure;
import net.woggioni.jzstd.internal.size_t;
@Structure.FieldOrder({"error", "lowerBound", "upperBound"}) @Structure.FieldOrder({"error", "lowerBound", "upperBound"})
public class ZSTD_bounds extends Structure { public class ZSTD_bounds extends Structure {

View File

@@ -16,28 +16,28 @@ public class ZstdLibrary {
ZSTD_CCtx cctx, ZSTD_CCtx cctx,
ZSTD_outBuffer output, ZSTD_outBuffer output,
ZSTD_inBuffer input, ZSTD_inBuffer input,
ZSTD_EndDirective endOp) { EndDirective endOp) {
size_t rc = ZSTD_compressStream2(cctx, output, input, endOp.value); size_t rc = ZSTD_compressStream2(cctx, output, input, endOp.value);
checkReturnCode(rc); checkReturnCode(rc);
return rc; return rc;
} }
public static ZSTD_bounds cParam_getBounds(ZSTD_cParameter cParam) { public static ZSTD_bounds cParam_getBounds(CompressionParameter cParam) {
ZSTD_bounds bounds = ZSTD_cParam_getBounds(cParam.value); ZSTD_bounds bounds = ZSTD_cParam_getBounds(cParam.value);
checkReturnCode(bounds.error); checkReturnCode(bounds.error);
return bounds; return bounds;
} }
public static void CCtx_setParameter(ZSTD_CCtx cctx, ZSTD_cParameter param, int value) { public static void CCtx_setParameter(ZSTD_CCtx cctx, CompressionParameter param, int value) {
size_t rc = ZSTD_CCtx_setParameter(cctx, param.value, value); size_t rc = ZSTD_CCtx_setParameter(cctx, param.value, value);
if (ZSTD_isError(rc)) { if (ZSTD_isError(rc)) {
throw new RuntimeException(getErrorString(rc)); throw new ZstdException(getErrorString(rc));
} }
} }
public static ZSTD_ErrorCode getErrorCode(size_t functionResult) { public static ErrorCode getErrorCode(size_t functionResult) {
int code = ZSTD_getErrorCode(functionResult); int code = ZSTD_getErrorCode(functionResult);
for (ZSTD_ErrorCode result : ZSTD_ErrorCode.values()) { for (ErrorCode result : ErrorCode.values()) {
if (code == result.value) return result; if (code == result.value) return result;
} }
throw new IllegalArgumentException("Unknown error code " + code); throw new IllegalArgumentException("Unknown error code " + code);
@@ -47,7 +47,7 @@ public class ZstdLibrary {
return ZSTD_getErrorString(getErrorCode(functionResult).value); return ZSTD_getErrorString(getErrorCode(functionResult).value);
} }
public static void CCtx_reset(ZSTD_CCtx ctx, ZSTD_ResetDirective resetDirective) { public static void CCtx_reset(ZSTD_CCtx ctx, ResetDirective resetDirective) {
checkReturnCode(ZSTD_CCtx_reset(ctx, resetDirective.value)); checkReturnCode(ZSTD_CCtx_reset(ctx, resetDirective.value));
} }
@@ -55,14 +55,21 @@ public class ZstdLibrary {
checkReturnCode(ZSTD_CCtx_refCDict(ctx, cdict)); checkReturnCode(ZSTD_CCtx_refCDict(ctx, cdict));
} }
public static native size_t ZSTD_CCtx_setParameter(ZSTD_CCtx cctx, int param, int value); public static void freeCStream(ZSTD_CCtx ctx) {
freeCCtx(ctx);
}
public static void freeCCtx(ZSTD_CCtx ctx) {
checkReturnCode(ZSTD_freeCStream(ctx));
}
private static native size_t ZSTD_CCtx_setParameter(ZSTD_CCtx cctx, int param, int value);
public static native ZSTD_CCtx ZSTD_createCCtx(); public static native ZSTD_CCtx ZSTD_createCCtx();
public static native size_t ZSTD_freeCCtx(ZSTD_CCtx cctx); public static native size_t ZSTD_freeCCtx(ZSTD_CCtx cctx);
public static native size_t ZSTD_initCStream(ZSTD_CCtx ctx, int compressionLevel); public static native size_t ZSTD_initCStream(ZSTD_CCtx ctx, int compressionLevel);
public static native ZSTD_CCtx ZSTD_createCStream(); public static native ZSTD_CCtx ZSTD_createCStream();
public static native size_t ZSTD_freeCStream(ZSTD_CCtx ctx); private static native size_t ZSTD_freeCStream(ZSTD_CCtx ctx);
public static native size_t ZSTD_compressStream2( private static native size_t ZSTD_compressStream2(
ZSTD_CCtx cctx, ZSTD_CCtx cctx,
ZSTD_outBuffer output, ZSTD_outBuffer output,
ZSTD_inBuffer input, ZSTD_inBuffer input,
@@ -70,32 +77,35 @@ public class ZstdLibrary {
public static native ZSTD_bounds.ByValue ZSTD_cParam_getBounds(int cParam); public static native ZSTD_bounds.ByValue ZSTD_cParam_getBounds(int cParam);
public static native boolean ZSTD_isError(size_t code); public static native boolean ZSTD_isError(size_t code);
public static native int ZSTD_getErrorCode(size_t functionResult); private static native int ZSTD_getErrorCode(size_t functionResult);
public static native String ZSTD_getErrorString(int code); public static native String ZSTD_getErrorString(int code);
public static native size_t ZSTD_CCtx_reset(ZSTD_CCtx ctx, int reset_directive); private static native size_t ZSTD_CCtx_reset(ZSTD_CCtx ctx, int reset_directive);
public static native size_t ZSTD_CCtx_refCDict(ZSTD_CCtx ctx, ZSTD_CDict cdict); private static native size_t ZSTD_CCtx_refCDict(ZSTD_CCtx ctx, ZSTD_CDict cdict);
public static native ZSTD_DCtx ZSTD_createDStream(); public static native ZSTD_DCtx ZSTD_createDStream();
public static native size_t ZSTD_freeDStream(ZSTD_DCtx ctx); public static native size_t ZSTD_freeDStream(ZSTD_DCtx ctx);
public static native size_t ZSTD_initDStream(ZSTD_DCtx ctx); public static native size_t ZSTD_initDStream(ZSTD_DCtx ctx);
public static native size_t ZSTD_DCtx_reset(ZSTD_DCtx ctx, int reset_directive); public static native size_t ZSTD_DCtx_reset(ZSTD_DCtx ctx, int reset_directive);
public static native size_t ZSTD_DCtx_refDDict(ZSTD_CCtx ctx, ZSTD_DDict ddict); public static native size_t ZSTD_DCtx_refDDict(ZSTD_DCtx ctx, ZSTD_DDict ddict);
public static native size_t ZSTD_decompressStream(ZSTD_DCtx ctx, ZSTD_outBuffer output, ZSTD_inBuffer input); private static native size_t ZSTD_decompressStream(ZSTD_DCtx ctx, ZSTD_outBuffer output, ZSTD_inBuffer input);
public static native size_t ZSTD_DStreamInSize(); private static native size_t ZSTD_DStreamInSize();
public static native size_t ZSTD_DStreamOutSize(); private static native size_t ZSTD_DStreamOutSize();
public static void freeDStream(ZSTD_DCtx ctx) { public static void freeDStream(ZSTD_DCtx ctx) {
freeDCtx(ctx);
}
public static void freeDCtx(ZSTD_DCtx ctx) {
checkReturnCode(ZSTD_freeDStream(ctx)); checkReturnCode(ZSTD_freeDStream(ctx));
} }
public static void ZSTD_DCtx_reset(ZSTD_DCtx ctx, ZSTD_ResetDirective reset_directive) { public static void DCtx_reset(ZSTD_DCtx ctx, ResetDirective reset_directive) {
checkReturnCode(ZSTD_DCtx_reset(ctx, reset_directive.value)); checkReturnCode(ZSTD_DCtx_reset(ctx, reset_directive.value));
} }
public static void refDDict(ZSTD_CCtx ctx, ZSTD_DDict ddict) { public static void DCtx_refDDict(ZSTD_DCtx ctx, ZSTD_DDict ddict) {
checkReturnCode(ZSTD_DCtx_refDDict(ctx, ddict)); checkReturnCode(ZSTD_DCtx_refDDict(ctx, ddict));
} }