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.deflate64; 019 020import java.io.IOException; 021import java.io.InputStream; 022 023import org.apache.commons.compress.compressors.CompressorInputStream; 024import org.apache.commons.compress.utils.InputStreamStatistics; 025 026import static org.apache.commons.compress.utils.IOUtils.closeQuietly; 027 028/** 029 * Deflate64 decompressor. 030 * 031 * @since 1.16 032 * @NotThreadSafe 033 */ 034public class Deflate64CompressorInputStream extends CompressorInputStream implements InputStreamStatistics { 035 private InputStream originalStream; 036 private HuffmanDecoder decoder; 037 private long compressedBytesRead; 038 private final byte[] oneByte = new byte[1]; 039 040 /** 041 * Constructs a Deflate64CompressorInputStream. 042 * 043 * @param in the stream to read from 044 */ 045 public Deflate64CompressorInputStream(InputStream in) { 046 this(new HuffmanDecoder(in)); 047 originalStream = in; 048 } 049 050 Deflate64CompressorInputStream(HuffmanDecoder decoder) { 051 this.decoder = decoder; 052 } 053 054 /** 055 * @throws java.io.EOFException if the underlying stream is exhausted before the end of defalted data was reached. 056 */ 057 @Override 058 public int read() throws IOException { 059 while (true) { 060 int r = read(oneByte); 061 switch (r) { 062 case 1: 063 return oneByte[0] & 0xFF; 064 case -1: 065 return -1; 066 case 0: 067 continue; 068 default: 069 throw new IllegalStateException("Invalid return value from read: " + r); 070 } 071 } 072 } 073 074 /** 075 * @throws java.io.EOFException if the underlying stream is exhausted before the end of deflated data was reached. 076 */ 077 @Override 078 public int read(byte[] b, int off, int len) throws IOException { 079 if (len == 0) { 080 return 0; 081 } 082 int read = -1; 083 if (decoder != null) { 084 read = decoder.decode(b, off, len); 085 compressedBytesRead = decoder.getBytesRead(); 086 count(read); 087 if (read == -1) { 088 closeDecoder(); 089 } 090 } 091 return read; 092 } 093 094 @Override 095 public int available() throws IOException { 096 return decoder != null ? decoder.available() : 0; 097 } 098 099 @Override 100 public void close() throws IOException { 101 try { 102 closeDecoder(); 103 } finally { 104 if (originalStream != null) { 105 originalStream.close(); 106 originalStream = null; 107 } 108 } 109 } 110 111 /** 112 * @since 1.17 113 */ 114 @Override 115 public long getCompressedCount() { 116 return compressedBytesRead; 117 } 118 119 private void closeDecoder() { 120 closeQuietly(decoder); 121 decoder = null; 122 } 123}