1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.compress.compressors.zstandard;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22
23 import org.apache.commons.compress.compressors.CompressorInputStream;
24 import org.apache.commons.compress.utils.InputStreamStatistics;
25 import org.apache.commons.io.input.BoundedInputStream;
26
27 import com.github.luben.zstd.BufferPool;
28 import com.github.luben.zstd.ZstdInputStream;
29
30
31
32
33
34
35
36 public class ZstdCompressorInputStream extends CompressorInputStream implements InputStreamStatistics {
37
38 private final BoundedInputStream countingStream;
39 private final ZstdInputStream decIS;
40
41 public ZstdCompressorInputStream(final InputStream in) throws IOException {
42 this.decIS = new ZstdInputStream(countingStream = BoundedInputStream.builder().setInputStream(in).get());
43 }
44
45
46
47
48
49
50
51
52
53 public ZstdCompressorInputStream(final InputStream in, final BufferPool bufferPool) throws IOException {
54 this.decIS = new ZstdInputStream(countingStream = BoundedInputStream.builder().setInputStream(in).get(), bufferPool);
55 }
56
57 @Override
58 public int available() throws IOException {
59 return decIS.available();
60 }
61
62 @Override
63 public void close() throws IOException {
64 decIS.close();
65 }
66
67
68
69
70 @Override
71 public long getCompressedCount() {
72 return countingStream.getCount();
73 }
74
75 @Override
76 public synchronized void mark(final int readLimit) {
77 decIS.mark(readLimit);
78 }
79
80 @Override
81 public boolean markSupported() {
82 return decIS.markSupported();
83 }
84
85 @Override
86 public int read() throws IOException {
87 final int ret = decIS.read();
88 count(ret == -1 ? 0 : 1);
89 return ret;
90 }
91
92 @Override
93 public int read(final byte[] b) throws IOException {
94 return read(b, 0, b.length);
95 }
96
97 @Override
98 public int read(final byte[] buf, final int off, final int len) throws IOException {
99 if (len == 0) {
100 return 0;
101 }
102 final int ret = decIS.read(buf, off, len);
103 count(ret);
104 return ret;
105 }
106
107 @Override
108 public synchronized void reset() throws IOException {
109 decIS.reset();
110 }
111
112 @Override
113 public long skip(final long n) throws IOException {
114 return org.apache.commons.io.IOUtils.skip(decIS, n);
115 }
116
117 @Override
118 public String toString() {
119 return decIS.toString();
120 }
121 }