BrotliCompressorInputStream.java

  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. package org.apache.commons.compress.compressors.brotli;

  18. import java.io.IOException;
  19. import java.io.InputStream;

  20. import org.apache.commons.compress.compressors.CompressorInputStream;
  21. import org.apache.commons.compress.utils.InputStreamStatistics;
  22. import org.apache.commons.io.IOUtils;
  23. import org.apache.commons.io.input.BoundedInputStream;
  24. import org.brotli.dec.BrotliInputStream;

  25. /**
  26.  * {@link CompressorInputStream} implementation to decode Brotli encoded stream. Library relies on <a href="https://github.com/google/brotli">Google brotli</a>
  27.  *
  28.  * @since 1.14
  29.  */
  30. public class BrotliCompressorInputStream extends CompressorInputStream implements InputStreamStatistics {

  31.     private final BoundedInputStream countingInputStream;
  32.     private final BrotliInputStream brotliInputStream;

  33.     public BrotliCompressorInputStream(final InputStream inputStream) throws IOException {
  34.         brotliInputStream = new BrotliInputStream(countingInputStream = BoundedInputStream.builder().setInputStream(inputStream).get());
  35.     }

  36.     @Override
  37.     public int available() throws IOException {
  38.         return brotliInputStream.available();
  39.     }

  40.     @Override
  41.     public void close() throws IOException {
  42.         brotliInputStream.close();
  43.     }

  44.     /**
  45.      * @since 1.17
  46.      */
  47.     @Override
  48.     public long getCompressedCount() {
  49.         return countingInputStream.getCount();
  50.     }

  51.     @Override
  52.     public synchronized void mark(final int readLimit) {
  53.         brotliInputStream.mark(readLimit);
  54.     }

  55.     @Override
  56.     public boolean markSupported() {
  57.         return brotliInputStream.markSupported();
  58.     }

  59.     @Override
  60.     public int read() throws IOException {
  61.         final int ret = brotliInputStream.read();
  62.         count(ret == -1 ? 0 : 1);
  63.         return ret;
  64.     }

  65.     @Override
  66.     public int read(final byte[] b) throws IOException {
  67.         return brotliInputStream.read(b);
  68.     }

  69.     @Override
  70.     public int read(final byte[] buf, final int off, final int len) throws IOException {
  71.         final int ret = brotliInputStream.read(buf, off, len);
  72.         count(ret);
  73.         return ret;
  74.     }

  75.     @Override
  76.     public synchronized void reset() throws IOException {
  77.         brotliInputStream.reset();
  78.     }

  79.     @Override
  80.     public long skip(final long n) throws IOException {
  81.         return IOUtils.skip(brotliInputStream, n);
  82.     }

  83.     @Override
  84.     public String toString() {
  85.         return brotliInputStream.toString();
  86.     }
  87. }