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