View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * {@link CompressorInputStream} implementation to decode Brotli encoded stream. Library relies on <a href="https://github.com/google/brotli">Google brotli</a>
31   *
32   * @since 1.14
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       * @since 1.17
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 }