Class MultiByteBuff

java.lang.Object
org.apache.hadoop.hbase.nio.ByteBuff
org.apache.hadoop.hbase.nio.MultiByteBuff
All Implemented Interfaces:
HBaseReferenceCounted, org.apache.hbase.thirdparty.io.netty.util.ReferenceCounted

@Private public class MultiByteBuff extends ByteBuff
Provides a unified view of all the underlying ByteBuffers and will look as if a bigger sequential buffer. This class provides similar APIs as in ByteBuffer to put/get int, short, long etc and doing operations like mark, reset, slice etc. This has to be used when data is split across multiple byte buffers and we don't want copy them to single buffer for reading from it.
  • Field Details

  • Constructor Details

  • Method Details

    • array

      public byte[] array()
      Description copied from class: ByteBuff
      Returns the byte[] if the underlying BB has single BB and hasArray true
      Specified by:
      array in class ByteBuff
      Throws:
      UnsupportedOperationException - MBB does not support array based operations
    • arrayOffset

      public int arrayOffset()
      Description copied from class: ByteBuff
      Returns the arrayOffset of the byte[] incase of a single BB backed ByteBuff
      Specified by:
      arrayOffset in class ByteBuff
      Throws:
      UnsupportedOperationException - MBB does not support array based operations
    • hasArray

      public boolean hasArray()
      Returns false. MBB does not support array based operations
      Specified by:
      hasArray in class ByteBuff
    • capacity

      public int capacity()
      Returns the total capacity of this MultiByteBuffer.
      Specified by:
      capacity in class ByteBuff
    • get

      public byte get(int index)
      Fetches the byte at the given index. Does not change position of the underlying ByteBuffers
      Specified by:
      get in class ByteBuff
      Returns:
      the byte at the given index
    • getByteAfterPosition

      public byte getByteAfterPosition(int offset)
      Description copied from class: ByteBuff
      Fetches the byte at the given offset from current position. Does not change position of the underlying ByteBuffers.
      Specified by:
      getByteAfterPosition in class ByteBuff
      Returns:
      the byte value at the given index.
    • getItemIndex

      private int getItemIndex(int elemIndex)
    • getItemIndexFromCurItemIndex

      private int getItemIndexFromCurItemIndex(int elemIndex)
    • getInt

      public int getInt(int index)
      Fetches the int at the given index. Does not change position of the underlying ByteBuffers
      Specified by:
      getInt in class ByteBuff
      Returns:
      the int value at the given index
    • getIntAfterPosition

      public int getIntAfterPosition(int offset)
      Description copied from class: ByteBuff
      Fetches the int value at the given offset from current position. Does not change position of the underlying ByteBuffers.
      Specified by:
      getIntAfterPosition in class ByteBuff
    • getShort

      public short getShort(int index)
      Fetches the short at the given index. Does not change position of the underlying ByteBuffers
      Specified by:
      getShort in class ByteBuff
      Returns:
      the short value at the given index
    • getShortAfterPosition

      public short getShortAfterPosition(int offset)
      Description copied from class: ByteBuff
      Fetches the short value at the given offset from current position. Does not change position of the underlying ByteBuffers.
      Specified by:
      getShortAfterPosition in class ByteBuff
      Returns:
      the short value at the given index.
    • getInt

      private int getInt(int index, int itemIndex)
    • getShort

      private short getShort(int index, int itemIndex)
    • getLong

      private long getLong(int index, int itemIndex)
    • getLong

      public long getLong(int index)
      Fetches the long at the given index. Does not change position of the underlying ByteBuffers
      Specified by:
      getLong in class ByteBuff
      Returns:
      the long value at the given index
    • getLongAfterPosition

      public long getLongAfterPosition(int offset)
      Description copied from class: ByteBuff
      Fetches the long value at the given offset from current position. Does not change position of the underlying ByteBuffers.
      Specified by:
      getLongAfterPosition in class ByteBuff
      Returns:
      the long value at the given index.
    • position

      public int position()
      Returns this MBB's current position
      Specified by:
      position in class ByteBuff
    • position

      public MultiByteBuff position(int position)
      Sets this MBB's position to the given value.
      Specified by:
      position in class ByteBuff
      Returns:
      this object
    • rewind

      Rewinds this MBB and the position is set to 0
      Specified by:
      rewind in class ByteBuff
      Returns:
      this object
    • mark

      public MultiByteBuff mark()
      Marks the current position of the MBB
      Specified by:
      mark in class ByteBuff
      Returns:
      this object
    • reset

      public MultiByteBuff reset()
      Similar to ByteBuffer.reset(), ensures that this MBB is reset back to last marked position.
      Specified by:
      reset in class ByteBuff
      Returns:
      This MBB
    • remaining

      public int remaining()
      Returns the number of elements between the current position and the limit.
      Specified by:
      remaining in class ByteBuff
      Returns:
      the remaining elements in this MBB
    • hasRemaining

      public final boolean hasRemaining()
      Returns true if there are elements between the current position and the limt
      Specified by:
      hasRemaining in class ByteBuff
      Returns:
      true if there are elements, false otherwise
    • get

      public byte get()
      A relative method that returns byte at the current position. Increments the current position by the size of a byte.
      Specified by:
      get in class ByteBuff
      Returns:
      the byte at the current position
    • getShort

      public short getShort()
      Returns the short value at the current position. Also advances the position by the size of short
      Specified by:
      getShort in class ByteBuff
      Returns:
      the short value at the current position
    • getInt

      public int getInt()
      Returns the int value at the current position. Also advances the position by the size of int
      Specified by:
      getInt in class ByteBuff
      Returns:
      the int value at the current position
    • getLong

      public long getLong()
      Returns the long value at the current position. Also advances the position by the size of long
      Specified by:
      getLong in class ByteBuff
      Returns:
      the long value at the current position
    • get

      public void get(byte[] dst)
      Copies the content from this MBB's current position to the byte array and fills it. Also advances the position of the MBB by the length of the byte[].
      Specified by:
      get in class ByteBuff
      Parameters:
      dst - the byte[] to which the ByteBuff's content is to be copied
    • get

      public void get(byte[] dst, int offset, int length)
      Copies the specified number of bytes from this MBB's current position to the byte[]'s offset. Also advances the position of the MBB by the given length.
      Specified by:
      get in class ByteBuff
      Parameters:
      dst - the byte[] to which the ByteBuff's content is to be copied
      offset - within the current array
      length - upto which the bytes to be copied
    • get

      public void get(int sourceOffset, byte[] dst, int offset, int length)
      Description copied from class: ByteBuff
      Copies the specified number of bytes from this ByteBuff's given position to the byte[]'s offset. The position of the ByteBuff remains in the current position only
      Specified by:
      get in class ByteBuff
      Parameters:
      sourceOffset - the offset in this ByteBuff from where the copy should happen
      dst - the byte[] to which the ByteBuff's content is to be copied
      offset - within the current array
      length - upto which the bytes to be copied
    • limit

      public MultiByteBuff limit(int limit)
      Marks the limit of this MBB.
      Specified by:
      limit in class ByteBuff
      Returns:
      This MBB
    • limit

      public int limit()
      Returns the limit of this MBB
      Specified by:
      limit in class ByteBuff
      Returns:
      limit of the MBB
    • slice

      public MultiByteBuff slice()
      Returns an MBB which is a sliced version of this MBB. The position, limit and mark of the new MBB will be independent than that of the original MBB. The content of the new MBB will start at this MBB's current position
      Specified by:
      slice in class ByteBuff
      Returns:
      a sliced MBB
    • duplicate

      Returns an MBB which is a duplicate version of this MBB. The position, limit and mark of the new MBB will be independent than that of the original MBB. The content of the new MBB will start at this MBB's current position The position, limit and mark of the new MBB would be identical to this MBB in terms of values.
      Specified by:
      duplicate in class ByteBuff
      Returns:
      a duplicated MBB
    • put

      public MultiByteBuff put(byte b)
      Writes a byte to this MBB at the current position and increments the position
      Specified by:
      put in class ByteBuff
      Returns:
      this object
    • put

      public MultiByteBuff put(int index, byte b)
      Writes a byte to this MBB at the given index and won't affect the position of any of the buffers.
      Specified by:
      put in class ByteBuff
      Returns:
      this object
      Throws:
      IndexOutOfBoundsException - If index is negative or not smaller than the limit
    • put

      public MultiByteBuff put(int destOffset, ByteBuff src, int srcOffset, int length)
      Copies from a src BB to this MBB. This will be absolute positional copying and won't affect the position of any of the buffers.
      Specified by:
      put in class ByteBuff
      Parameters:
      destOffset - the position in this MBB to which the copy should happen
      src - the src MBB
      srcOffset - the offset in the src MBB from where the elements should be read
      length - the length upto which the copy should happen
      Throws:
      BufferUnderflowException - If there are fewer than length bytes remaining in src ByteBuff.
      BufferOverflowException - If there is insufficient available space in this MBB for length bytes.
    • getItemByteBuffer

      private static ByteBuffer getItemByteBuffer(ByteBuff buf, int byteBufferIndex)
    • getItemIndexForByteBuff

      private static int getItemIndexForByteBuff(ByteBuff byteBuff, int offset, int length)
    • getRelativeOffsetForByteBuff

      private static int getRelativeOffsetForByteBuff(ByteBuff byteBuff, int globalOffset, int itemIndex)
    • getRelativeOffset

      private int getRelativeOffset(int globalOffset, int itemIndex)
    • getItemByteBufferCount

      private static int getItemByteBufferCount(ByteBuff buf)
    • putInt

      public MultiByteBuff putInt(int val)
      Writes an int to this MBB at its current position. Also advances the position by size of int
      Specified by:
      putInt in class ByteBuff
      Parameters:
      val - Int value to write
      Returns:
      this object
    • int3

      private static byte int3(int x)
    • int2

      private static byte int2(int x)
    • int1

      private static byte int1(int x)
    • int0

      private static byte int0(int x)
    • put

      public final MultiByteBuff put(byte[] src)
      Copies from the given byte[] to this MBB
      Specified by:
      put in class ByteBuff
      Parameters:
      src - source byte array
      Returns:
      this ByteBuff
    • put

      public MultiByteBuff put(byte[] src, int offset, int length)
      Copies from the given byte[] to this MBB.
      Specified by:
      put in class ByteBuff
      Parameters:
      src - source byte array
      offset - the position in the byte array from which the copy should be done
      length - the length upto which the copy should happen
      Returns:
      this ByteBuff
    • putLong

      public MultiByteBuff putLong(long val)
      Writes a long to this MBB at its current position. Also advances the position by size of long
      Specified by:
      putLong in class ByteBuff
      Parameters:
      val - Long value to write
      Returns:
      this object
    • long7

      private static byte long7(long x)
    • long6

      private static byte long6(long x)
    • long5

      private static byte long5(long x)
    • long4

      private static byte long4(long x)
    • long3

      private static byte long3(long x)
    • long2

      private static byte long2(long x)
    • long1

      private static byte long1(long x)
    • long0

      private static byte long0(long x)
    • skip

      public MultiByteBuff skip(int length)
      Jumps the current position of this MBB by specified length.
      Specified by:
      skip in class ByteBuff
      Parameters:
      length - the length to be skipped
    • moveBack

      public MultiByteBuff moveBack(int length)
      Jumps back the current position of this MBB by specified length.
      Specified by:
      moveBack in class ByteBuff
      Parameters:
      length - the length to move back
    • asSubByteBuffer

      public ByteBuffer asSubByteBuffer(int length)
      Returns bytes from current position till length specified, as a single ByteBuffer. When all these bytes happen to be in a single ByteBuffer, which this object wraps, that ByteBuffer item as such will be returned. So users are warned not to change the position or limit of this returned ByteBuffer. The position of the returned byte buffer is at the begin of the required bytes. When the required bytes happen to span across multiple ByteBuffers, this API will copy the bytes to a newly created ByteBuffer of required size and return that.
      Specified by:
      asSubByteBuffer in class ByteBuff
      Parameters:
      length - number of bytes required.
      Returns:
      bytes from current position till length specified, as a single ByteButter.
    • asSubByteBuffer

      public void asSubByteBuffer(int offset, int length, ObjectIntPair<ByteBuffer> pair)
      Returns bytes from given offset till length specified, as a single ByteBuffer. When all these bytes happen to be in a single ByteBuffer, which this object wraps, that ByteBuffer item as such will be returned (with offset in this ByteBuffer where the bytes starts). So users are warned not to change the position or limit of this returned ByteBuffer. When the required bytes happen to span across multiple ByteBuffers, this API will copy the bytes to a newly created ByteBuffer of required size and return that.
      Specified by:
      asSubByteBuffer in class ByteBuff
      Parameters:
      offset - the offset in this MBB from where the subBuffer should be created
      length - the length of the subBuffer
      pair - a pair that will have the bytes from the current position till length specified, as a single ByteBuffer and offset in that Buffer where the bytes starts. The method would set the values on the pair that is passed in by the caller
    • get

      public void get(ByteBuffer out, int sourceOffset, int length)
      Copies the content from an this MBB to a ByteBuffer
      Specified by:
      get in class ByteBuff
      Parameters:
      out - the ByteBuffer to which the copy has to happen, its position will be advanced.
      sourceOffset - the offset in the MBB from which the elements has to be copied
      length - the length in the MBB upto which the elements has to be copied
    • toBytes

      public byte[] toBytes(int offset, int length)
      Copy the content from this MBB to a byte[] based on the given offset and length the position from where the copy should start the length upto which the copy has to be done
      Specified by:
      toBytes in class ByteBuff
      Returns:
      byte[] with the copied contents from this MBB.
    • internalRead

      private int internalRead(ReadableByteChannel channel, long offset, ByteBuff.ChannelReader reader) throws IOException
      Throws:
      IOException
    • read

      public int read(ReadableByteChannel channel) throws IOException
      Description copied from class: ByteBuff
      Reads bytes from the given channel into this ByteBuf.
      Specified by:
      read in class ByteBuff
      Throws:
      IOException
    • read

      public int read(FileChannel channel, long offset) throws IOException
      Description copied from class: ByteBuff
      Reads bytes from FileChannel into this ByteBuff
      Specified by:
      read in class ByteBuff
      Throws:
      IOException
    • write

      public int write(FileChannel channel, long offset) throws IOException
      Description copied from class: ByteBuff
      Write this ByteBuff's data into target file
      Specified by:
      write in class ByteBuff
      Throws:
      IOException
    • nioByteBuffers

      Specified by:
      nioByteBuffers in class ByteBuff
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • retain