View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to you under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  
18  package org.apache.hadoop.hbase.io.hfile;
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  
23  import org.apache.hadoop.fs.FSDataInputStream;
24  
25  /**
26   * BoundedRangeFIleInputStream abstracts a contiguous region of a Hadoop
27   * FSDataInputStream as a regular input stream. One can create multiple
28   * BoundedRangeFileInputStream on top of the same FSDataInputStream and they
29   * would not interfere with each other.
30   * Copied from hadoop-335 tfile.
31   */
32  class BoundedRangeFileInputStream  extends InputStream {
33  
34    private FSDataInputStream in;
35    private long pos;
36    private long end;
37    private long mark;
38    private final byte[] oneByte = new byte[1];
39    private final boolean pread;
40  
41    /**
42     * Constructor
43     *
44     * @param in
45     *          The FSDataInputStream we connect to.
46     * @param offset
47     *          Beginning offset of the region.
48     * @param length
49     *          Length of the region.
50     * @param pread If true, use Filesystem positional read rather than seek+read.
51     *
52     *          The actual length of the region may be smaller if (off_begin +
53     *          length) goes beyond the end of FS input stream.
54     */
55    public BoundedRangeFileInputStream(FSDataInputStream in, long offset,
56        long length, final boolean pread) {
57      if (offset < 0 || length < 0) {
58        throw new IndexOutOfBoundsException("Invalid offset/length: " + offset
59            + "/" + length);
60      }
61  
62      this.in = in;
63      this.pos = offset;
64      this.end = offset + length;
65      this.mark = -1;
66      this.pread = pread;
67    }
68  
69    @Override
70    public int available() throws IOException {
71      int avail = in.available();
72      if (pos + avail > end) {
73        avail = (int) (end - pos);
74      }
75  
76      return avail;
77    }
78  
79    @Override
80    public int read() throws IOException {
81      int ret = read(oneByte);
82      if (ret == 1) return oneByte[0] & 0xff;
83      return -1;
84    }
85  
86    @Override
87    public int read(byte[] b) throws IOException {
88      return read(b, 0, b.length);
89    }
90  
91    @Override
92    public int read(byte[] b, int off, int len) throws IOException {
93      if ((off | len | (off + len) | (b.length - (off + len))) < 0) {
94        throw new IndexOutOfBoundsException();
95      }
96  
97      int n = (int) Math.min(Integer.MAX_VALUE, Math.min(len, (end - pos)));
98      if (n == 0) return -1;
99      int ret = 0;
100     if (this.pread) {
101       ret = in.read(pos, b, off, n);
102     } else {
103       synchronized (in) {
104         in.seek(pos);
105         ret = in.read(b, off, n);
106       }
107     }
108     if (ret < 0) {
109       end = pos;
110       return -1;
111     }
112     pos += ret;
113     return ret;
114   }
115 
116   @Override
117   /*
118    * We may skip beyond the end of the file.
119    */
120   public long skip(long n) throws IOException {
121     long len = Math.min(n, end - pos);
122     pos += len;
123     return len;
124   }
125 
126   @Override
127   public void mark(int readlimit) {
128     mark = pos;
129   }
130 
131   @Override
132   public void reset() throws IOException {
133     if (mark < 0) throw new IOException("Resetting to invalid mark");
134     pos = mark;
135   }
136 
137   @Override
138   public boolean markSupported() {
139     return true;
140   }
141 
142   @Override
143   public void close() {
144     // Invalidate the state of the stream.
145     in = null;
146     pos = end;
147     mark = -1;
148   }
149 }