source: proiecte/HadoopJUnit/hadoop-0.20.1/src/core/org/apache/hadoop/fs/FSInputStream.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 2.5 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18package org.apache.hadoop.fs;
19
20import java.io.*;
21
22/****************************************************************
23 * FSInputStream is a generic old InputStream with a little bit
24 * of RAF-style seek ability.
25 *
26 *****************************************************************/
27public abstract class FSInputStream extends InputStream
28    implements Seekable, PositionedReadable {
29  /**
30   * Seek to the given offset from the start of the file.
31   * The next read() will be from that location.  Can't
32   * seek past the end of the file.
33   */
34  public abstract void seek(long pos) throws IOException;
35
36  /**
37   * Return the current offset from the start of the file
38   */
39  public abstract long getPos() throws IOException;
40
41  /**
42   * Seeks a different copy of the data.  Returns true if
43   * found a new source, false otherwise.
44   */
45  public abstract boolean seekToNewSource(long targetPos) throws IOException;
46
47  public int read(long position, byte[] buffer, int offset, int length)
48    throws IOException {
49    synchronized (this) {
50      long oldPos = getPos();
51      int nread = -1;
52      try {
53        seek(position);
54        nread = read(buffer, offset, length);
55      } finally {
56        seek(oldPos);
57      }
58      return nread;
59    }
60  }
61   
62  public void readFully(long position, byte[] buffer, int offset, int length)
63    throws IOException {
64    int nread = 0;
65    while (nread < length) {
66      int nbytes = read(position+nread, buffer, offset+nread, length-nread);
67      if (nbytes < 0) {
68        throw new EOFException("End of file reached before reading fully.");
69      }
70      nread += nbytes;
71    }
72  }
73   
74  public void readFully(long position, byte[] buffer)
75    throws IOException {
76    readFully(position, buffer, 0, buffer.length);
77  }
78}
Note: See TracBrowser for help on using the repository browser.