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