001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.compress.compressors.brotli;
019
020import java.io.IOException;
021import java.io.InputStream;
022
023import org.apache.commons.compress.compressors.CompressorInputStream;
024import org.apache.commons.compress.utils.CountingInputStream;
025import org.apache.commons.compress.utils.IOUtils;
026import org.apache.commons.compress.utils.InputStreamStatistics;
027import org.brotli.dec.BrotliInputStream;
028
029/**
030 * {@link CompressorInputStream} implementation to decode Brotli encoded stream.
031 * Library relies on <a href="https://github.com/google/brotli">Google brotli</a>
032 *
033 * @since 1.14
034 */
035public class BrotliCompressorInputStream extends CompressorInputStream
036    implements InputStreamStatistics {
037
038    private final CountingInputStream countingStream;
039    private final BrotliInputStream decIS;
040
041    public BrotliCompressorInputStream(final InputStream in) throws IOException {
042        decIS = new BrotliInputStream(countingStream = new CountingInputStream(in));
043    }
044
045    @Override
046    public int available() throws IOException {
047        return decIS.available();
048    }
049
050    @Override
051    public void close() throws IOException {
052        decIS.close();
053    }
054
055    @Override
056    public int read(final byte[] b) throws IOException {
057        return decIS.read(b);
058    }
059
060    @Override
061    public long skip(final long n) throws IOException {
062        return IOUtils.skip(decIS, n);
063    }
064
065    @Override
066    public void mark(final int readlimit) {
067        decIS.mark(readlimit);
068    }
069
070    @Override
071    public boolean markSupported() {
072        return decIS.markSupported();
073    }
074
075    @Override
076    public int read() throws IOException {
077        final int ret = decIS.read();
078        count(ret == -1 ? 0 : 1);
079        return ret;
080    }
081
082    @Override
083    public int read(final byte[] buf, final int off, final int len) throws IOException {
084        final int ret = decIS.read(buf, off, len);
085        count(ret);
086        return ret;
087    }
088
089    @Override
090    public String toString() {
091        return decIS.toString();
092    }
093
094    @Override
095    public void reset() throws IOException {
096        decIS.reset();
097    }
098
099    /**
100     * @since 1.17
101     */
102    @Override
103    public long getCompressedCount() {
104        return countingStream.getBytesRead();
105    }
106}