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  /**
045   * Create a compression input stream that reads
046   * the decompressed bytes from the given stream.
047   * 
048   * @param in The input stream to be compressed.
049   * @throws IOException
050   */
051  protected CompressionInputStream(InputStream in) throws IOException {
052    if (!(in instanceof Seekable) || !(in instanceof PositionedReadable)) {
053        this.maxAvailableData = in.available();
054    }
055    this.in = in;
056  }
057
058  public void close() throws IOException {
059    in.close();
060  }
061  
062  /**
063   * Read bytes from the stream.
064   * Made abstract to prevent leakage to underlying stream.
065   */
066  public abstract int read(byte[] b, int off, int len) throws IOException;
067
068  /**
069   * Reset the decompressor to its initial state and discard any buffered data,
070   * as the underlying stream may have been repositioned.
071   */
072  public abstract void resetState() throws IOException;
073  
074  /**
075   * This method returns the current position in the stream.
076   *
077   * @return Current position in stream as a long
078   */
079  public long getPos() throws IOException {
080    if (!(in instanceof Seekable) || !(in instanceof PositionedReadable)){
081      //This way of getting the current position will not work for file
082      //size which can be fit in an int and hence can not be returned by
083      //available method.
084      return (this.maxAvailableData - this.in.available());
085    }
086    else{
087      return ((Seekable)this.in).getPos();
088    }
089
090  }
091
092  /**
093   * This method is current not supported.
094   *
095   * @throws UnsupportedOperationException
096   */
097
098  public void seek(long pos) throws UnsupportedOperationException {
099    throw new UnsupportedOperationException();
100  }
101
102  /**
103   * This method is current not supported.
104   *
105   * @throws UnsupportedOperationException
106   */
107  public boolean seekToNewSource(long targetPos) throws UnsupportedOperationException {
108    throw new UnsupportedOperationException();
109  }
110}