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.client.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 */ 055 int available() throws IOException; 056 057 /** 058 * Close the block reader. 059 * 060 * @throws IOException 061 */ 062 void close() throws IOException; 063 064 /** 065 * Read exactly the given amount of data, throwing an exception 066 * if EOF is reached before that amount 067 */ 068 void readFully(byte[] buf, int readOffset, int amtToRead) throws IOException; 069 070 /** 071 * Similar to {@link #readFully(byte[], int, int)} except that it will 072 * not throw an exception on EOF. However, it differs from the simple 073 * {@link #read(byte[], int, int)} call in that it is guaranteed to 074 * read the data if it is available. In other words, if this call 075 * does not throw an exception, then either the buffer has been 076 * filled or the next call will return EOF. 077 */ 078 int readAll(byte[] buf, int offset, int len) throws IOException; 079 080 /** 081 * @return true only if this is a local read. 082 */ 083 boolean isLocal(); 084 085 /** 086 * @return true only if this is a short-circuit read. 087 * All short-circuit reads are also local. 088 */ 089 boolean isShortCircuit(); 090 091 /** 092 * Get a ClientMmap object for this BlockReader. 093 * 094 * @param opts The read options to use. 095 * @return The ClientMmap object, or null if mmap is not 096 * supported. 097 */ 098 ClientMmap getClientMmap(EnumSet<ReadOption> opts); 099 }