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