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