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