View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * {@link CompressorInputStream} implementation to decode Brotli encoded stream. Library relies on <a href="https://github.com/google/brotli">Google brotli</a>
33   *
34   * @since 1.14
35   */
36  public class BrotliCompressorInputStream extends CompressorInputStream implements InputStreamStatistics {
37  
38      private final BoundedInputStream countingInputStream;
39      private final BrotliInputStream brotliInputStream;
40  
41      /**
42       * Construct a new instance.
43       *
44       * @param inputStream underlying data source.
45       * @throws IOException in case of corrupted data or source stream problems.
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       * @since 1.17
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 }