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  public long length() {
049    return len;
050  }
051
052  public int read(byte[] b, int off, int len) throws IOException {
053    return stream.read(b, off, len);
054  }
055
056  public void seek(long p) throws IOException {
057    stream.seek(p);
058  }
059
060  public long tell() throws IOException {
061    return stream.getPos();
062  }
063
064  public void close() throws IOException {
065    stream.close();
066  }
067}