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.net.Socket;
022    
023    import org.apache.hadoop.fs.ByteBufferReadable;
024    import org.apache.hadoop.hdfs.protocol.datatransfer.IOStreamPair;
025    
026    /**
027     * A BlockReader is responsible for reading a single block
028     * from a single datanode.
029     */
030    public interface BlockReader extends ByteBufferReadable {
031    
032      /* same interface as inputStream java.io.InputStream#read()
033       * used by DFSInputStream#read()
034       * This violates one rule when there is a checksum error:
035       * "Read should not modify user buffer before successful read"
036       * because it first reads the data to user buffer and then checks
037       * the checksum.
038       */
039      int read(byte[] buf, int off, int len) throws IOException;
040    
041      /**
042       * Skip the given number of bytes
043       */
044      long skip(long n) throws IOException;
045    
046      void close() throws IOException;
047    
048      /**
049       * Read exactly the given amount of data, throwing an exception
050       * if EOF is reached before that amount
051       */
052      void readFully(byte[] buf, int readOffset, int amtToRead) throws IOException;
053    
054      /**
055       * Similar to {@link #readFully(byte[], int, int)} except that it will
056       * not throw an exception on EOF. However, it differs from the simple
057       * {@link #read(byte[], int, int)} call in that it is guaranteed to
058       * read the data if it is available. In other words, if this call
059       * does not throw an exception, then either the buffer has been
060       * filled or the next call will return EOF.
061       */
062      int readAll(byte[] buf, int offset, int len) throws IOException;
063    
064      /**
065       * Take the socket used to talk to the DN.
066       */
067      Socket takeSocket();
068    
069      /**
070       * Whether the BlockReader has reached the end of its input stream
071       * and successfully sent a status code back to the datanode.
072       */
073      boolean hasSentStatusCode();
074    
075      /**
076       * @return a reference to the streams this block reader is using.
077       */
078      IOStreamPair getStreams();
079    }