| 6 | import org.jetbrains.annotations.NotNull; |
| 7 | |
| 8 | public final class CompressZstd implements Codec, Closeable { |
| 9 | public static final int DEFAULT_SRC_BUF_SIZE = 1024; |
| 10 | |
| 11 | private final @NotNull Codec sink; |
| 12 | private final @NotNull ZstdCompressStream cs; |
| 13 | private final byte @NotNull [] srcBuf; |
| 14 | private int srcBufLen; |
| 15 | |
| 16 | public CompressZstd(@NotNull Codec sink) { |
| 17 | this.sink = sink; |
| 18 | cs = ZstdFactory.newCompressStream(); |
| 19 | srcBuf = new byte[DEFAULT_SRC_BUF_SIZE]; |
| 20 | } |
| 21 | |
| 22 | public CompressZstd(@NotNull Codec sink, int srcBufSize, int dstBufSize, int compressLevel, int windowLog) { |
| 23 | this.sink = sink; |
| 24 | cs = ZstdFactory.newCompressStream(dstBufSize, compressLevel, windowLog); |
| 25 | srcBuf = new byte[Math.max(srcBufSize, 1)]; |
| 26 | } |
| 27 | |
| 28 | @Override |
| 29 | public void update(byte c) throws CodecException { |
| 30 | srcBuf[srcBufLen++] = c; |
| 31 | if (srcBufLen == srcBuf.length) { |
| 32 | cs.compress(srcBuf, 0, srcBufLen, sink); |
| 33 | srcBufLen = 0; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | public void update(byte @NotNull [] data, int off, int len) throws CodecException { |
| 39 | if (srcBufLen > 0) { |
| 40 | int n = Math.min(srcBuf.length - srcBufLen, len); |
| 41 | System.arraycopy(data, off, srcBuf, srcBufLen, n); |
| 42 | if ((srcBufLen += n) >= srcBuf.length) { |
| 43 | cs.compress(srcBuf, 0, srcBufLen, sink); |
| 44 | srcBufLen = 0; |
| 45 | } |
| 46 | if ((len -= n) <= 0) |
| 47 | return; |
| 48 | off += n; |
| 49 | } |
| 50 | if (len < srcBuf.length) { |
| 51 | System.arraycopy(data, off, srcBuf, 0, len); |
| 52 | srcBufLen = len; |
| 53 | } else |
| 54 | cs.compress(data, off, off + len, sink); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public void flush() throws CodecException { |
| 59 | if (srcBufLen > 0) { |
| 60 | cs.compress(srcBuf, 0, srcBufLen, sink); |
| 61 | srcBufLen = 0; |
| 62 | } |
| 63 | cs.flush(sink); |
| 64 | } |
| 65 |
nothing calls this directly
no outgoing calls
no test coverage detected