001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io;
019
020import java.io.InputStream;
021import org.apache.hadoop.hbase.nio.ByteBuff;
022import org.apache.yetus.audience.InterfaceAudience;
023
024/**
025 * Not thread safe!
026 * <p>
027 * Please note that the reads will cause position movement on wrapped ByteBuff.
028 */
029@InterfaceAudience.Private
030public class ByteBuffInputStream extends InputStream {
031
032  private ByteBuff buf;
033
034  public ByteBuffInputStream(ByteBuff buf) {
035    this.buf = buf;
036  }
037
038  /**
039   * Reads the next byte of data from this input stream. The value byte is returned as an
040   * <code>int</code> in the range <code>0</code> to <code>255</code>. If no byte is available
041   * because the end of the stream has been reached, the value <code>-1</code> is returned.
042   * @return the next byte of data, or <code>-1</code> if the end of the stream has been reached.
043   */
044  @Override
045  public int read() {
046    if (this.buf.hasRemaining()) {
047      return (this.buf.get() & 0xff);
048    }
049    return -1;
050  }
051
052  /**
053   * Reads up to next <code>len</code> bytes of data from buffer into passed array(starting from
054   * given offset).
055   * @param b   the array into which the data is read.
056   * @param off the start offset in the destination array <code>b</code>
057   * @param len the maximum number of bytes to read.
058   * @return the total number of bytes actually read into the buffer, or <code>-1</code> if not even
059   *         1 byte can be read because the end of the stream has been reached.
060   */
061  @Override
062  public int read(byte b[], int off, int len) {
063    int avail = available();
064    if (avail <= 0) {
065      return -1;
066    }
067    if (len <= 0) {
068      return 0;
069    }
070
071    if (len > avail) {
072      len = avail;
073    }
074    this.buf.get(b, off, len);
075    return len;
076  }
077
078  /**
079   * Skips <code>n</code> bytes of input from this input stream. Fewer bytes might be skipped if the
080   * end of the input stream is reached. The actual number <code>k</code> of bytes to be skipped is
081   * equal to the smaller of <code>n</code> and remaining bytes in the stream.
082   * @param n the number of bytes to be skipped.
083   * @return the actual number of bytes skipped.
084   */
085  @Override
086  public long skip(long n) {
087    long k = Math.min(n, available());
088    if (k <= 0) {
089      return 0;
090    }
091    this.buf.skip((int) k);
092    return k;
093  }
094
095  /**
096   * @return the number of remaining bytes that can be read (or skipped over) from this input
097   *         stream.
098   */
099  @Override
100  public int available() {
101    return this.buf.remaining();
102  }
103}