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 019package org.apache.hadoop.fs; 020 021import java.io.Closeable; 022import java.io.IOException; 023 024import org.apache.avro.file.SeekableInput; 025import org.apache.hadoop.classification.InterfaceAudience; 026import org.apache.hadoop.classification.InterfaceStability; 027 028/** Adapts an {@link FSDataInputStream} to Avro's SeekableInput interface. */ 029@InterfaceAudience.Public 030@InterfaceStability.Stable 031public class AvroFSInput implements Closeable, SeekableInput { 032 private final FSDataInputStream stream; 033 private final long len; 034 035 /** Construct given an {@link FSDataInputStream} and its length. */ 036 public AvroFSInput(final FSDataInputStream in, final long len) { 037 this.stream = in; 038 this.len = len; 039 } 040 041 /** Construct given a {@link FileContext} and a {@link Path}. */ 042 public AvroFSInput(final FileContext fc, final Path p) throws IOException { 043 FileStatus status = fc.getFileStatus(p); 044 this.len = status.getLen(); 045 this.stream = fc.open(p); 046 } 047 048 @Override 049 public long length() { 050 return len; 051 } 052 053 @Override 054 public int read(byte[] b, int off, int len) throws IOException { 055 return stream.read(b, off, len); 056 } 057 058 @Override 059 public void seek(long p) throws IOException { 060 stream.seek(p); 061 } 062 063 @Override 064 public long tell() throws IOException { 065 return stream.getPos(); 066 } 067 068 @Override 069 public void close() throws IOException { 070 stream.close(); 071 } 072}