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