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