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.input.CountingInputStream;
26  import org.brotli.dec.BrotliInputStream;
27  
28  /**
29   * {@link CompressorInputStream} implementation to decode Brotli encoded stream. Library relies on <a href="https://github.com/google/brotli">Google brotli</a>
30   *
31   * @since 1.14
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       * @since 1.17
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 }