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    package org.apache.hadoop.hdfs.client;
019    
020    import java.io.InputStream;
021    import java.io.IOException;
022    import java.util.List;
023    
024    import org.apache.hadoop.classification.InterfaceAudience;
025    import org.apache.hadoop.classification.InterfaceStability;
026    import org.apache.hadoop.fs.FSDataInputStream;
027    import org.apache.hadoop.crypto.CryptoInputStream;
028    import org.apache.hadoop.hdfs.DFSInputStream;
029    import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
030    import org.apache.hadoop.hdfs.protocol.ExtendedBlock;
031    import org.apache.hadoop.hdfs.protocol.LocatedBlock;
032    
033    import com.google.common.base.Preconditions;
034    
035    /**
036     * The Hdfs implementation of {@link FSDataInputStream}.
037     */
038    @InterfaceAudience.Public
039    @InterfaceStability.Evolving
040    public class HdfsDataInputStream extends FSDataInputStream {
041      public HdfsDataInputStream(DFSInputStream in) throws IOException {
042        super(in);
043      }
044    
045      public HdfsDataInputStream(CryptoInputStream in) throws IOException {
046        super(in);
047        Preconditions.checkArgument(in.getWrappedStream() instanceof DFSInputStream,
048            "CryptoInputStream should wrap a DFSInputStream");
049      }
050    
051      private DFSInputStream getDFSInputStream() {
052        if (in instanceof CryptoInputStream) {
053          return (DFSInputStream) ((CryptoInputStream) in).getWrappedStream();
054        }
055        return (DFSInputStream) in;
056      }
057    
058      /**
059       * Get a reference to the wrapped output stream. We always want to return the
060       * actual underlying InputStream, even when we're using a CryptoStream. e.g.
061       * in the delegated methods below.
062       *
063       * @return the underlying output stream
064       */
065      public InputStream getWrappedStream() {
066          return in;
067      }
068    
069      /**
070       * Get the datanode from which the stream is currently reading.
071       */
072      public DatanodeInfo getCurrentDatanode() {
073        return getDFSInputStream().getCurrentDatanode();
074      }
075    
076      /**
077       * Get the block containing the target position.
078       */
079      public ExtendedBlock getCurrentBlock() {
080        return getDFSInputStream().getCurrentBlock();
081      }
082    
083      /**
084       * Get the collection of blocks that has already been located.
085       */
086      public synchronized List<LocatedBlock> getAllBlocks() throws IOException {
087        return getDFSInputStream().getAllBlocks();
088      }
089    
090      /**
091       * Get the visible length of the file. It will include the length of the last
092       * block even if that is in UnderConstruction state.
093       * 
094       * @return The visible length of the file.
095       */
096      public long getVisibleLength() throws IOException {
097        return getDFSInputStream().getFileLength();
098      }
099    
100      /**
101       * Get statistics about the reads which this DFSInputStream has done.
102       * Note that because HdfsDataInputStream is buffered, these stats may
103       * be higher than you would expect just by adding up the number of
104       * bytes read through HdfsDataInputStream.
105       */
106      public synchronized DFSInputStream.ReadStatistics getReadStatistics() {
107        return getDFSInputStream().getReadStatistics();
108      }
109    }