    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.InputStreamStatistics;
    025import org.apache.commons.io.input.CountingInputStream;
    026import org.brotli.dec.BrotliInputStream;
    027
    028/**
    029 * {@link CompressorInputStream} implementation to decode Brotli encoded stream. Library relies on <a href="https://github.com/google/brotli">Google brotli</a>
    030 *
    031 * @since 1.14
    032 */
    033public class BrotliCompressorInputStream extends CompressorInputStream implements InputStreamStatistics {
    034
    035    private final CountingInputStream countingInputStream;
    036    private final BrotliInputStream brotliInputStream;
    037
    038    public BrotliCompressorInputStream(final InputStream inputStream) throws IOException {
    039        brotliInputStream = new BrotliInputStream(countingInputStream = new CountingInputStream(inputStream));
    040    }
    041
    042    @Override
    043    public int available() throws IOException {
    044        return brotliInputStream.available();
    045    }
    046
    047    @Override
    048    public void close() throws IOException {
    049        brotliInputStream.close();
    050    }
    051
    052    /**
    053     * @since 1.17
    054     */
    055    @Override
    056    public long getCompressedCount() {
    057        return countingInputStream.getByteCount();
    058    }
    059
    060    @Override
    061    public synchronized void mark(final int readLimit) {
    062        brotliInputStream.mark(readLimit);
    063    }
    064
    065    @Override
    066    public boolean markSupported() {
    067        return brotliInputStream.markSupported();
    068    }
    069
    070    @Override
    071    public int read() throws IOException {
    072        final int ret = brotliInputStream.read();
    073        count(ret == -1 ? 0 : 1);
    074        return ret;
    075    }
    076
    077    @Override
    078    public int read(final byte[] b) throws IOException {
    079        return brotliInputStream.read(b);
    080    }
    081
    082    @Override
    083    public int read(final byte[] buf, final int off, final int len) throws IOException {
    084        final int ret = brotliInputStream.read(buf, off, len);
    085        count(ret);
    086        return ret;
    087    }
    088
    089    @Override
    090    public synchronized void reset() throws IOException {
    091        brotliInputStream.reset();
    092    }
    093
    094    @Override
    095    public long skip(final long n) throws IOException {
    096        return org.apache.commons.io.IOUtils.skip(brotliInputStream, n);
    097    }
    098
    099    @Override
    100    public String toString() {
    101        return brotliInputStream.toString();
    102    }
    103}