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;
019    
020    import java.io.IOException;
021    import java.util.EnumSet;
022    
023    import org.apache.hadoop.fs.ByteBufferReadable;
024    import org.apache.hadoop.fs.ReadOption;
025    import org.apache.hadoop.hdfs.shortcircuit.ClientMmap;
026    
027    /**
028     * A BlockReader is responsible for reading a single block
029     * from a single datanode.
030     */
031    public interface BlockReader extends ByteBufferReadable {
032      
033    
034      /* same interface as inputStream java.io.InputStream#read()
035       * used by DFSInputStream#read()
036       * This violates one rule when there is a checksum error:
037       * "Read should not modify user buffer before successful read"
038       * because it first reads the data to user buffer and then checks
039       * the checksum.
040       * Note: this must return -1 on EOF, even in the case of a 0-byte read.
041       * See HDFS-5762 for details.
042       */
043      int read(byte[] buf, int off, int len) throws IOException;
044    
045      /**
046       * Skip the given number of bytes
047       */
048      long skip(long n) throws IOException;
049    
050      /**
051       * Returns an estimate of the number of bytes that can be read
052       * (or skipped over) from this input stream without performing
053       * network I/O.
054       * This may return more than what is actually present in the block.
055       */
056      int available() throws IOException;
057    
058      /**
059       * Close the block reader.
060       *
061       * @throws IOException
062       */
063      void close() throws IOException;
064    
065      /**
066       * Read exactly the given amount of data, throwing an exception
067       * if EOF is reached before that amount
068       */
069      void readFully(byte[] buf, int readOffset, int amtToRead) throws IOException;
070    
071      /**
072       * Similar to {@link #readFully(byte[], int, int)} except that it will
073       * not throw an exception on EOF. However, it differs from the simple
074       * {@link #read(byte[], int, int)} call in that it is guaranteed to
075       * read the data if it is available. In other words, if this call
076       * does not throw an exception, then either the buffer has been
077       * filled or the next call will return EOF.
078       */
079      int readAll(byte[] buf, int offset, int len) throws IOException;
080    
081      /**
082       * @return              true only if this is a local read.
083       */
084      boolean isLocal();
085      
086      /**
087       * @return              true only if this is a short-circuit read.
088       *                      All short-circuit reads are also local.
089       */
090      boolean isShortCircuit();
091    
092      /**
093       * Get a ClientMmap object for this BlockReader.
094       *
095       * @param opts          The read options to use.
096       * @return              The ClientMmap object, or null if mmap is not
097       *                      supported.
098       */
099      ClientMmap getClientMmap(EnumSet<ReadOption> opts);
100    }