HBASE-23892 SecureTestCluster should allow its subclasses to pass their Class referen...
[hbase.git] / hbase-common / src / main / java / org / apache / hadoop / hbase / io / ByteBufferInputStream.java
blob2811b1b8155a1e9ee5964ddf7e27138eb27f9e71
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase.io;
20 import java.io.InputStream;
21 import java.nio.ByteBuffer;
23 import org.apache.hadoop.hbase.util.ByteBufferUtils;
24 import org.apache.yetus.audience.InterfaceAudience;
26 /**
27 * Not thread safe!
28 * <p>
29 * Please note that the reads will cause position movement on wrapped ByteBuffer.
31 @InterfaceAudience.Private
32 public class ByteBufferInputStream extends InputStream {
34 private ByteBuffer buf;
36 public ByteBufferInputStream(ByteBuffer buf) {
37 this.buf = buf;
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.
46 @Override
47 public int read() {
48 if (this.buf.hasRemaining()) {
49 return (this.buf.get() & 0xff);
51 return -1;
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.
63 @Override
64 public int read(byte[] b, int off, int len) {
65 int avail = available();
66 if (avail <= 0) {
67 return -1;
70 if (len > avail) {
71 len = avail;
73 if (len <= 0) {
74 return 0;
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;
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.
89 @Override
90 public long skip(long n) {
91 long k = Math.min(n, available());
92 if (k < 0) {
93 k = 0;
95 this.buf.position((int) (this.buf.position() + k));
96 return k;
99 /**
100 * @return the number of remaining bytes that can be read (or skipped
101 * over) from this input stream.
103 @Override
104 public int available() {
105 return this.buf.remaining();