1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.compress.compressors.brotli;
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.IOUtils;
26 import org.apache.commons.io.input.BoundedInputStream;
27 import org.brotli.dec.BrotliInputStream;
28
29
30
31
32
33
34 public class BrotliCompressorInputStream extends CompressorInputStream implements InputStreamStatistics {
35
36 private final BoundedInputStream countingInputStream;
37 private final BrotliInputStream brotliInputStream;
38
39 public BrotliCompressorInputStream(final InputStream inputStream) throws IOException {
40 brotliInputStream = new BrotliInputStream(countingInputStream = BoundedInputStream.builder().setInputStream(inputStream).get());
41 }
42
43 @Override
44 public int available() throws IOException {
45 return brotliInputStream.available();
46 }
47
48 @Override
49 public void close() throws IOException {
50 brotliInputStream.close();
51 }
52
53
54
55
56 @Override
57 public long getCompressedCount() {
58 return countingInputStream.getCount();
59 }
60
61 @Override
62 public synchronized void mark(final int readLimit) {
63 brotliInputStream.mark(readLimit);
64 }
65
66 @Override
67 public boolean markSupported() {
68 return brotliInputStream.markSupported();
69 }
70
71 @Override
72 public int read() throws IOException {
73 final int ret = brotliInputStream.read();
74 count(ret == -1 ? 0 : 1);
75 return ret;
76 }
77
78 @Override
79 public int read(final byte[] b) throws IOException {
80 return brotliInputStream.read(b);
81 }
82
83 @Override
84 public int read(final byte[] buf, final int off, final int len) throws IOException {
85 final int ret = brotliInputStream.read(buf, off, len);
86 count(ret);
87 return ret;
88 }
89
90 @Override
91 public synchronized void reset() throws IOException {
92 brotliInputStream.reset();
93 }
94
95 @Override
96 public long skip(final long n) throws IOException {
97 return IOUtils.skip(brotliInputStream, n);
98 }
99
100 @Override
101 public String toString() {
102 return brotliInputStream.toString();
103 }
104 }