removed redundant buffer
This commit is contained in:
@@ -1,12 +0,0 @@
|
|||||||
package net.woggioni.jzstd;
|
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public enum EndDirective {
|
|
||||||
Continue(0),
|
|
||||||
Flush(1),
|
|
||||||
End(2);
|
|
||||||
|
|
||||||
public final int value;
|
|
||||||
}
|
|
@@ -0,0 +1,9 @@
|
|||||||
|
package net.woggioni.jzstd;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class ParameterBoundaries {
|
||||||
|
public final int min;
|
||||||
|
public final int max;
|
||||||
|
}
|
@@ -1,6 +1,7 @@
|
|||||||
package net.woggioni.jzstd;
|
package net.woggioni.jzstd;
|
||||||
|
|
||||||
import net.woggioni.jzstd.internal.ZSTD_CCtx;
|
import net.woggioni.jzstd.internal.ZSTD_CCtx;
|
||||||
|
import net.woggioni.jzstd.internal.ZSTD_bounds;
|
||||||
import net.woggioni.jzstd.internal.ZstdLibrary;
|
import net.woggioni.jzstd.internal.ZstdLibrary;
|
||||||
|
|
||||||
public class ZstdCompressionCtx implements AutoCloseable {
|
public class ZstdCompressionCtx implements AutoCloseable {
|
||||||
@@ -19,4 +20,9 @@ public class ZstdCompressionCtx implements AutoCloseable {
|
|||||||
public void close() {
|
public void close() {
|
||||||
ZstdLibrary.freeCStream(ctx);
|
ZstdLibrary.freeCStream(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ParameterBoundaries getParameterBoundaries(CompressionParameter param) {
|
||||||
|
ZSTD_bounds bounds = ZstdLibrary.cParam_getBounds(param);
|
||||||
|
return new ParameterBoundaries(bounds.lowerBound, bounds.upperBound);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -10,37 +10,30 @@ public class ZstdOutputStream extends OutputStream {
|
|||||||
private final ZstdCompressionCtx ctx;
|
private final ZstdCompressionCtx ctx;
|
||||||
private final boolean ctx_owner;
|
private final boolean ctx_owner;
|
||||||
|
|
||||||
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 ZSTD_EndDirective flag;
|
||||||
private EndDirective flag;
|
|
||||||
|
private static final int inputBufferSize = ZstdLibrary.CStreamInSize();
|
||||||
|
private static final int outputBufferSize = ZstdLibrary.CStreamOutSize();
|
||||||
|
|
||||||
|
|
||||||
private ZstdOutputStream(OutputStream sink,
|
private ZstdOutputStream(OutputStream sink,
|
||||||
ZstdCompressionCtx ctx,
|
ZstdCompressionCtx ctx,
|
||||||
boolean ctx_owner,
|
boolean ctx_owner) {
|
||||||
int bufferSize) {
|
|
||||||
this.sink = sink;
|
this.sink = sink;
|
||||||
this.ctx = ctx;
|
this.ctx = ctx;
|
||||||
this.ctx_owner = ctx_owner;
|
this.ctx_owner = ctx_owner;
|
||||||
this.buffer = new byte[bufferSize];
|
this.input = new ZSTD_inBuffer(inputBufferSize);
|
||||||
this.input = new ZSTD_inBuffer(bufferSize);
|
this.output = new ZSTD_outBuffer(outputBufferSize);
|
||||||
this.output = new ZSTD_outBuffer(bufferSize);
|
this.flag = ZSTD_EndDirective.ZSTD_e_continue;
|
||||||
this.pos = 0;
|
|
||||||
this.flag = EndDirective.Continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ZstdOutputStream from(OutputStream sink,
|
|
||||||
ZstdCompressionCtx ctx) {
|
|
||||||
return from(sink, ctx, 0x10000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ZstdOutputStream from(
|
public static ZstdOutputStream from(
|
||||||
OutputStream sink,
|
OutputStream sink,
|
||||||
ZstdCompressionCtx ctx,
|
ZstdCompressionCtx ctx) {
|
||||||
int bufferSize) {
|
|
||||||
ctx.init();
|
ctx.init();
|
||||||
return new ZstdOutputStream(sink, ctx, false, bufferSize);
|
return new ZstdOutputStream(sink, ctx, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ZstdOutputStream from(OutputStream sink) {
|
public static ZstdOutputStream from(OutputStream sink) {
|
||||||
@@ -49,12 +42,6 @@ public class ZstdOutputStream extends OutputStream {
|
|||||||
|
|
||||||
public static ZstdOutputStream from(OutputStream sink,
|
public static ZstdOutputStream from(OutputStream sink,
|
||||||
int compressionLevel) {
|
int compressionLevel) {
|
||||||
return from(sink, compressionLevel, 0x1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ZstdOutputStream from(OutputStream sink,
|
|
||||||
int compressionLevel,
|
|
||||||
int bufferSize) {
|
|
||||||
ZSTD_bounds range = ZstdLibrary.cParam_getBounds(CompressionParameter.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 " +
|
||||||
@@ -62,29 +49,31 @@ public class ZstdOutputStream extends OutputStream {
|
|||||||
}
|
}
|
||||||
ZstdCompressionCtx ctx = new ZstdCompressionCtx();
|
ZstdCompressionCtx ctx = new ZstdCompressionCtx();
|
||||||
ctx.setParameter(CompressionParameter.compressionLevel, compressionLevel);
|
ctx.setParameter(CompressionParameter.compressionLevel, compressionLevel);
|
||||||
return new ZstdOutputStream(sink, ctx, true, bufferSize);
|
return new ZstdOutputStream(sink, ctx, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public void flush() {
|
public void flush() {
|
||||||
input.src.put(buffer, 0, pos);
|
size_t zero = new size_t(0);
|
||||||
|
input.size = new size_t(input.src.position());
|
||||||
input.src.position(0);
|
input.src.position(0);
|
||||||
input.pos = new size_t(0);
|
input.pos = zero;
|
||||||
input.size = new size_t(pos);
|
|
||||||
pos = 0;
|
|
||||||
while (true) {
|
while (true) {
|
||||||
size_t rc = ZstdLibrary.compressStream2(ctx.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);
|
int limit = output.pos.intValue();
|
||||||
|
while(output.dst.position() < limit) {
|
||||||
|
byte b = output.dst.get();
|
||||||
|
sink.write(b);
|
||||||
|
}
|
||||||
|
output.pos = zero;
|
||||||
output.dst.position(0);
|
output.dst.position(0);
|
||||||
sink.write(buffer, 0, output.pos.intValue());
|
|
||||||
output.pos = new size_t(0);
|
|
||||||
}
|
}
|
||||||
if (input.pos.longValue() == input.size.longValue() && flag == EndDirective.Continue) {
|
if (input.pos.longValue() == input.size.longValue() && flag == ZSTD_EndDirective.ZSTD_e_continue) {
|
||||||
break;
|
break;
|
||||||
} else if (flag == EndDirective.End && rc.intValue() == 0) {
|
} else if (flag == ZSTD_EndDirective.ZSTD_e_end && rc.intValue() == 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -92,18 +81,17 @@ public class ZstdOutputStream extends OutputStream {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(int b) {
|
public void write(int b) {
|
||||||
if (pos == buffer.length) flush();
|
if (input.src.position() == input.src.capacity()) flush();
|
||||||
buffer[pos++] = (byte) b;
|
input.src.put((byte) b);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(byte[] arr, int off, int len) {
|
public void write(byte[] arr, int off, int len) {
|
||||||
var written = 0;
|
var written = 0;
|
||||||
while (written < len - off) {
|
while (written < len - off) {
|
||||||
int writeSize = Math.min(len, buffer.length - pos);
|
int writeSize = Math.min(len, input.src.capacity() - input.src.position());
|
||||||
System.arraycopy(arr, off, buffer, pos, writeSize);
|
input.src.put(arr, off, writeSize);
|
||||||
pos += writeSize;
|
if (input.src.position() == input.src.capacity()) flush();
|
||||||
if (pos == buffer.length) flush();
|
|
||||||
written += writeSize;
|
written += writeSize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -111,7 +99,7 @@ public class ZstdOutputStream extends OutputStream {
|
|||||||
@Override
|
@Override
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public void close() {
|
public void close() {
|
||||||
flag = EndDirective.End;
|
flag = ZSTD_EndDirective.ZSTD_e_end;
|
||||||
flush();
|
flush();
|
||||||
sink.close();
|
sink.close();
|
||||||
if (ctx_owner) ctx.close();
|
if (ctx_owner) ctx.close();
|
||||||
|
@@ -0,0 +1,11 @@
|
|||||||
|
package net.woggioni.jzstd.internal;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public enum ZSTD_EndDirective {
|
||||||
|
ZSTD_e_continue(0),
|
||||||
|
ZSTD_e_flush(1),
|
||||||
|
ZSTD_e_end(2);
|
||||||
|
public final int value;
|
||||||
|
}
|
@@ -16,7 +16,7 @@ public class ZstdLibrary {
|
|||||||
ZSTD_CCtx cctx,
|
ZSTD_CCtx cctx,
|
||||||
ZSTD_outBuffer output,
|
ZSTD_outBuffer output,
|
||||||
ZSTD_inBuffer input,
|
ZSTD_inBuffer input,
|
||||||
EndDirective endOp) {
|
ZSTD_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;
|
||||||
@@ -55,6 +55,15 @@ public class ZstdLibrary {
|
|||||||
checkReturnCode(ZSTD_CCtx_refCDict(ctx, cdict));
|
checkReturnCode(ZSTD_CCtx_refCDict(ctx, cdict));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static int CStreamInSize() {
|
||||||
|
return ZSTD_CStreamInSize().intValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int CStreamOutSize() {
|
||||||
|
return ZSTD_CStreamOutSize().intValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void freeCStream(ZSTD_CCtx ctx) {
|
public static void freeCStream(ZSTD_CCtx ctx) {
|
||||||
freeCCtx(ctx);
|
freeCCtx(ctx);
|
||||||
}
|
}
|
||||||
@@ -62,6 +71,9 @@ public class ZstdLibrary {
|
|||||||
checkReturnCode(ZSTD_freeCStream(ctx));
|
checkReturnCode(ZSTD_freeCStream(ctx));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static native size_t ZSTD_CStreamInSize();
|
||||||
|
private static native size_t ZSTD_CStreamOutSize();
|
||||||
|
|
||||||
private static native size_t ZSTD_CCtx_setParameter(ZSTD_CCtx cctx, int param, int value);
|
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);
|
||||||
|
Reference in New Issue
Block a user