source: proiecte/HadoopJUnit/hadoop-0.20.1/src/core/org/apache/hadoop/fs/BufferedFSInputStream.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.8 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.BufferedInputStream;
21import java.io.IOException;
22
23
24/**
25 * A class optimizes reading from FSInputStream by bufferring
26 */
27
28
29public class BufferedFSInputStream extends BufferedInputStream
30implements Seekable, PositionedReadable {
31  /**
32   * Creates a <code>BufferedFSInputStream</code>
33   * with the specified buffer size,
34   * and saves its  argument, the input stream
35   * <code>in</code>, for later use.  An internal
36   * buffer array of length  <code>size</code>
37   * is created and stored in <code>buf</code>.
38   *
39   * @param   in     the underlying input stream.
40   * @param   size   the buffer size.
41   * @exception IllegalArgumentException if size <= 0.
42   */
43  public BufferedFSInputStream(FSInputStream in, int size) {
44    super(in, size);
45  }
46
47  public long getPos() throws IOException {
48    return ((FSInputStream)in).getPos()-(count-pos);
49  }
50
51  public long skip(long n) throws IOException {
52    if (n <= 0) {
53      return 0;
54    }
55
56    seek(getPos()+n);
57    return n;
58  }
59
60  public void seek(long pos) throws IOException {
61    if( pos<0 ) {
62      return;
63    }
64    // optimize: check if the pos is in the buffer
65    long end = ((FSInputStream)in).getPos();
66    long start = end - count;
67    if( pos>=start && pos<end) {
68      this.pos = (int)(pos-start);
69      return;
70    }
71
72    // invalidate buffer
73    this.pos = 0;
74    this.count = 0;
75
76    ((FSInputStream)in).seek(pos);
77  }
78
79  public boolean seekToNewSource(long targetPos) throws IOException {
80    pos = 0;
81    count = 0;
82    return ((FSInputStream)in).seekToNewSource(targetPos);
83  }
84
85  public int read(long position, byte[] buffer, int offset, int length) throws IOException {
86    return ((FSInputStream)in).read(position, buffer, offset, length) ;
87  }
88
89  public void readFully(long position, byte[] buffer, int offset, int length) throws IOException {
90    ((FSInputStream)in).readFully(position, buffer, offset, length);
91  }
92
93  public void readFully(long position, byte[] buffer) throws IOException {
94    ((FSInputStream)in).readFully(position, buffer);
95  }
96}
Note: See TracBrowser for help on using the repository browser.