001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.apache.hadoop.io.compress; 020 021import java.io.IOException; 022import java.io.InputStream; 023 024import org.apache.hadoop.classification.InterfaceAudience; 025import org.apache.hadoop.classification.InterfaceStability; 026import org.apache.hadoop.fs.PositionedReadable; 027import org.apache.hadoop.fs.Seekable; 028/** 029 * A compression input stream. 030 * 031 * <p>Implementations are assumed to be buffered. This permits clients to 032 * reposition the underlying input stream then call {@link #resetState()}, 033 * without having to also synchronize client buffers. 034 */ 035@InterfaceAudience.Public 036@InterfaceStability.Evolving 037public abstract class CompressionInputStream extends InputStream implements Seekable { 038 /** 039 * The input stream to be compressed. 040 */ 041 protected final InputStream in; 042 protected long maxAvailableData = 0L; 043 044 private Decompressor trackedDecompressor; 045 046 /** 047 * Create a compression input stream that reads 048 * the decompressed bytes from the given stream. 049 * 050 * @param in The input stream to be compressed. 051 * @throws IOException 052 */ 053 protected CompressionInputStream(InputStream in) throws IOException { 054 if (!(in instanceof Seekable) || !(in instanceof PositionedReadable)) { 055 this.maxAvailableData = in.available(); 056 } 057 this.in = in; 058 } 059 060 @Override 061 public void close() throws IOException { 062 try { 063 in.close(); 064 } finally { 065 if (trackedDecompressor != null) { 066 CodecPool.returnDecompressor(trackedDecompressor); 067 trackedDecompressor = null; 068 } 069 } 070 } 071 072 /** 073 * Read bytes from the stream. 074 * Made abstract to prevent leakage to underlying stream. 075 */ 076 @Override 077 public abstract int read(byte[] b, int off, int len) throws IOException; 078 079 /** 080 * Reset the decompressor to its initial state and discard any buffered data, 081 * as the underlying stream may have been repositioned. 082 */ 083 public abstract void resetState() throws IOException; 084 085 /** 086 * This method returns the current position in the stream. 087 * 088 * @return Current position in stream as a long 089 */ 090 @Override 091 public long getPos() throws IOException { 092 if (!(in instanceof Seekable) || !(in instanceof PositionedReadable)){ 093 //This way of getting the current position will not work for file 094 //size which can be fit in an int and hence can not be returned by 095 //available method. 096 return (this.maxAvailableData - this.in.available()); 097 } 098 else{ 099 return ((Seekable)this.in).getPos(); 100 } 101 102 } 103 104 /** 105 * This method is current not supported. 106 * 107 * @throws UnsupportedOperationException 108 */ 109 110 @Override 111 public void seek(long pos) throws UnsupportedOperationException { 112 throw new UnsupportedOperationException(); 113 } 114 115 /** 116 * This method is current not supported. 117 * 118 * @throws UnsupportedOperationException 119 */ 120 @Override 121 public boolean seekToNewSource(long targetPos) throws UnsupportedOperationException { 122 throw new UnsupportedOperationException(); 123 } 124 125 void setTrackedDecompressor(Decompressor decompressor) { 126 trackedDecompressor = decompressor; 127 } 128}