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 /** 056 * @since 1.17 057 */ 058 @Override 059 public long getCompressedCount() { 060 return countingStream.getBytesRead(); 061 } 062 063 @Override 064 public synchronized void mark(final int readlimit) { 065 decIS.mark(readlimit); 066 } 067 068 @Override 069 public boolean markSupported() { 070 return decIS.markSupported(); 071 } 072 073 @Override 074 public int read() throws IOException { 075 final int ret = decIS.read(); 076 count(ret == -1 ? 0 : 1); 077 return ret; 078 } 079 080 @Override 081 public int read(final byte[] b) throws IOException { 082 return decIS.read(b); 083 } 084 085 @Override 086 public int read(final byte[] buf, final int off, final int len) throws IOException { 087 final int ret = decIS.read(buf, off, len); 088 count(ret); 089 return ret; 090 } 091 092 @Override 093 public synchronized void reset() throws IOException { 094 decIS.reset(); 095 } 096 097 @Override 098 public long skip(final long n) throws IOException { 099 return IOUtils.skip(decIS, n); 100 } 101 102 @Override 103 public String toString() { 104 return decIS.toString(); 105 } 106}