MCPcopy Create free account
hub / github.com/e2wugui/zeze / DecompressZstd

Class DecompressZstd

ZezeJava/ZezeJava/src/main/java/Zeze/Net/DecompressZstd.java:7–69  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected