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