View Javadoc

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   */
18  package org.apache.hadoop.hbase.io;
19  
20  import java.io.InputStream;
21  import java.nio.ByteBuffer;
22  
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  
25  /**
26   * Not thread safe!
27   * <p>
28   * Please note that the reads will cause position movement on wrapped ByteBuffer.
29   */
30  @InterfaceAudience.Private
31  public class ByteBufferInputStream extends InputStream {
32  
33    private ByteBuffer buf;
34  
35    public ByteBufferInputStream(ByteBuffer buf) {
36        this.buf = buf;
37    }
38  
39    /**
40     * Reads the next byte of data from this input stream. The value byte is returned as an
41     * <code>int</code> in the range <code>0</code> to <code>255</code>. If no byte is available
42     * because the end of the stream has been reached, the value <code>-1</code> is returned.
43     * @return the next byte of data, or <code>-1</code> if the end of the stream has been reached.
44     */
45    public int read() {
46      if (this.buf.hasRemaining()) {
47        return (this.buf.get() & 0xff);
48      }
49      return -1;
50    }
51  
52    /**
53     * Reads up to next <code>len</code> bytes of data from buffer into passed array(starting from
54     * given offset).
55     * @param b the array into which the data is read.
56     * @param off the start offset in the destination array <code>b</code>
57     * @param len the maximum number of bytes to read.
58     * @return the total number of bytes actually read into the buffer, or <code>-1</code> if not even
59     *         1 byte can be read because the end of the stream has been reached.
60     */
61    public int read(byte b[], int off, int len) {
62      int avail = available();
63      if (avail <= 0) {
64        return -1;
65      }
66  
67      if (len > avail) {
68        len = avail;
69      }
70      if (len <= 0) {
71        return 0;
72      }
73  
74      this.buf.get(b, off, len);
75      return len;
76    }
77  
78    /**
79     * Skips <code>n</code> bytes of input from this input stream. Fewer bytes might be skipped if the
80     * end of the input stream is reached. The actual number <code>k</code> of bytes to be skipped is
81     * equal to the smaller of <code>n</code> and remaining bytes in the stream.
82     * @param n the number of bytes to be skipped.
83     * @return the actual number of bytes skipped.
84     */
85    public long skip(long n) {
86      long k = Math.min(n, available());
87      if (k < 0) {
88        k = 0;
89      }
90      this.buf.position((int) (this.buf.position() + k));
91      return k;
92    }
93  
94    /**
95     * @return  the number of remaining bytes that can be read (or skipped
96     *          over) from this input stream.
97     */
98    public int available() {
99      return this.buf.remaining();
100   }
101 }