Interface ByteRange

All Superinterfaces:
Comparable<ByteRange>
All Known Subinterfaces:
PositionedByteRange
All Known Implementing Classes:
AbstractByteRange, AbstractPositionedByteRange, SimpleByteRange, SimplePositionedByteRange, SimplePositionedMutableByteRange

public interface ByteRange extends Comparable<ByteRange>
Lightweight, reusable class for specifying ranges of byte[]'s.

ByteRange maintains an underlying byte[] and a viewport into that byte[] as a range of bytes. The ByteRange is a mutable, reusable object, so the underlying byte[] can be modified after instantiation. This is done using the set(byte[]) and unset() methods. Direct access to the byte[] is also available via getBytes(). The viewport is defined by an offset into the byte[] and a length. The range of bytes is 0-indexed, and is accessed by index via the get(int) and put(int, byte) methods.

This interface differs from ByteBuffer:

  • On-heap bytes only
  • Raw byte access only; does not encode other primitives.
  • Implements equals(Object), #hashCode(), and #compareTo(ByteRange) so that it can be used in standard java Collections. Comparison operations are lexicographic, which is native to HBase.
  • Allows the addition of simple core methods like the deep and shallow copy methods.
  • Can be reused in tight loops like a major compaction which can save significant amounts of garbage. (Without reuse, we throw off garbage like this thing.)
  • Mutable, and always evaluates #equals(Object), #hashCode(), and #compareTo(ByteRange) based on the current contents.

    Can contain convenience methods for comparing, printing, cloning, spawning new arrays, copying to other arrays, etc. Please place non-core methods into ByteRangeUtils.

    • Method Summary

      Modifier and Type
      Method
      Description
      Create a new ByteRange with new backing byte[] containing a copy of the content from this range's window.
      void
      deepCopySubRangeTo(int innerOffset, int copyLength, byte[] destination, int destinationOffset)
      Wrapper for System.arraycopy.
      void
      deepCopyTo(byte[] destination, int destinationOffset)
      Wrapper for System.arraycopy.
      byte[]
      Instantiate a new byte[] with exact length, which is at least 24 bytes + length.
      byte
      get(int index)
      Retrieve the byte at index.
      get(int index, byte[] dst)
      Fill dst with bytes from the range, starting from index.
      get(int index, byte[] dst, int offset, int length)
      Fill dst with bytes from the range, starting from index.
      byte[]
      The underlying byte[].
      int
      getInt(int index)
      Retrieve the int value at index
      int
      The length of the range.
      long
      getLong(int index)
      Retrieve the long value at index
      int
      The offset, the index into the underlying byte[] at which this range begins.
      short
      getShort(int index)
      Retrieve the short value at index
      long
      getVLong(int index)
      Retrieve the long value at index which is stored as VLong
      boolean
       
      put(int index, byte val)
      Store val at index.
      put(int index, byte[] val)
      Store val at index.
      put(int index, byte[] val, int offset, int length)
      Store length bytes from val into this range, starting at index.
      putInt(int index, int val)
      Store the int value at index
      putLong(int index, long val)
      Store the long value at index
      putShort(int index, short val)
      Store the short value at index
      int
      putVLong(int index, long val)
      Store the long value at index as a VLong
      set(byte[] bytes)
      Reuse this ByteRange over a new byte[].
      set(byte[] bytes, int offset, int length)
      Reuse this ByteRange over a new byte[].
      set(int capacity)
      Reuse this ByteRange over a new byte[].
      setLength(int length)
      Update the length of this range.
      setOffset(int offset)
      Update the beginning of this range.
      Create a new ByteRange that points at this range's byte[].
      shallowCopySubRange(int innerOffset, int copyLength)
      Create a new ByteRange that points at this range's byte[].
      Nullifies this ByteRange.

      Methods inherited from interface java.lang.Comparable

      compareTo
    • Method Details

      • getBytes

        byte[] getBytes()
        The underlying byte[].
      • unset

        ByteRange unset()
        Nullifies this ByteRange. That is, it becomes a husk, being a range over no byte[] whatsoever.
        Returns:
        this
      • set

        ByteRange set(int capacity)
        Reuse this ByteRange over a new byte[]. offset is set to 0 and length is set to capacity.
        Parameters:
        capacity - the size of a new byte[].
        Returns:
        this
      • set

        ByteRange set(byte[] bytes)
        Reuse this ByteRange over a new byte[]. offset is set to 0 and length is set to bytes.length. A null bytes IS supported, in which case this method will behave equivalently to unset().
        Parameters:
        bytes - the array to wrap.
        Returns:
        this
      • set

        ByteRange set(byte[] bytes, int offset, int length)
        Reuse this ByteRange over a new byte[]. A null bytes IS supported, in which case this method will behave equivalently to unset(), regardless of the values of offset and length.
        Parameters:
        bytes - The array to wrap.
        offset - The offset into bytes considered the beginning of this range.
        length - The length of this range.
        Returns:
        this.
      • getOffset

        int getOffset()
        The offset, the index into the underlying byte[] at which this range begins.
        See Also:
      • setOffset

        ByteRange setOffset(int offset)
        Update the beginning of this range. offset + length may not be greater than bytes.length.
        Parameters:
        offset - the new start of this range.
        Returns:
        this.
      • getLength

        int getLength()
        The length of the range.
      • setLength

        ByteRange setLength(int length)
        Update the length of this range. offset + length should not be greater than bytes.length.
        Parameters:
        length - The new length of this range.
        Returns:
        this.
      • isEmpty

        boolean isEmpty()
        Returns:
        true when this range is of zero length, false otherwise.
      • get

        byte get(int index)
        Retrieve the byte at index.
        Parameters:
        index - zero-based index into this range.
        Returns:
        single byte at index.
      • getShort

        short getShort(int index)
        Retrieve the short value at index
        Parameters:
        index - zero-based index into this range
        Returns:
        the short value at index
      • getInt

        int getInt(int index)
        Retrieve the int value at index
        Parameters:
        index - zero-based index into this range
        Returns:
        the int value at index
      • getLong

        long getLong(int index)
        Retrieve the long value at index
        Parameters:
        index - zero-based index into this range
        Returns:
        the long value at index
      • getVLong

        long getVLong(int index)
        Retrieve the long value at index which is stored as VLong
        Parameters:
        index - zero-based index into this range
        Returns:
        the long value at index which is stored as VLong
      • get

        ByteRange get(int index, byte[] dst)
        Fill dst with bytes from the range, starting from index.
        Parameters:
        index - zero-based index into this range.
        dst - the destination of the copy.
        Returns:
        this.
      • get

        ByteRange get(int index, byte[] dst, int offset, int length)
        Fill dst with bytes from the range, starting from index. length bytes are copied into dst, starting at offset.
        Parameters:
        index - zero-based index into this range.
        dst - the destination of the copy.
        offset - the offset into dst to start the copy.
        length - the number of bytes to copy into dst.
        Returns:
        this.
      • put

        ByteRange put(int index, byte val)
        Store val at index.
        Parameters:
        index - the index in the range where val is stored.
        val - the value to store.
        Returns:
        this.
      • putShort

        ByteRange putShort(int index, short val)
        Store the short value at index
        Parameters:
        index - the index in the range where val is stored
        val - the value to store
        Returns:
        this
      • putInt

        ByteRange putInt(int index, int val)
        Store the int value at index
        Parameters:
        index - the index in the range where val is stored
        val - the value to store
        Returns:
        this
      • putLong

        ByteRange putLong(int index, long val)
        Store the long value at index
        Parameters:
        index - the index in the range where val is stored
        val - the value to store
        Returns:
        this
      • putVLong

        int putVLong(int index, long val)
        Store the long value at index as a VLong
        Parameters:
        index - the index in the range where val is stored
        val - the value to store
        Returns:
        number of bytes written
      • put

        ByteRange put(int index, byte[] val)
        Store val at index.
        Parameters:
        index - the index in the range where val is stored.
        val - the value to store.
        Returns:
        this.
      • put

        ByteRange put(int index, byte[] val, int offset, int length)
        Store length bytes from val into this range, starting at index. Bytes from val are copied starting at offset into the range.
        Parameters:
        index - position in this range to start the copy.
        val - the value to store.
        offset - the offset in val from which to start copying.
        length - the number of bytes to copy from val.
        Returns:
        this.
      • deepCopyToNewArray

        byte[] deepCopyToNewArray()
        Instantiate a new byte[] with exact length, which is at least 24 bytes + length. Copy the contents of this range into it.
        Returns:
        The newly cloned byte[].
      • deepCopy

        ByteRange deepCopy()
        Create a new ByteRange with new backing byte[] containing a copy of the content from this range's window.
        Returns:
        Deep copy
      • deepCopyTo

        void deepCopyTo(byte[] destination, int destinationOffset)
        Wrapper for System.arraycopy. Copy the contents of this range into the provided array.
        Parameters:
        destination - Copy to this array
        destinationOffset - First index in the destination array.
      • deepCopySubRangeTo

        void deepCopySubRangeTo(int innerOffset, int copyLength, byte[] destination, int destinationOffset)
        Wrapper for System.arraycopy. Copy the contents of this range into the provided array.
        Parameters:
        innerOffset - Start copying from this index in this source ByteRange. First byte copied is bytes[offset + innerOffset]
        copyLength - Copy this many bytes
        destination - Copy to this array
        destinationOffset - First index in the destination array.
      • shallowCopy

        ByteRange shallowCopy()
        Create a new ByteRange that points at this range's byte[]. Modifying the shallowCopy will modify the bytes in this range's array. Pass over the hash code if it is already cached.
        Returns:
        new ByteRange object referencing this range's byte[].
      • shallowCopySubRange

        ByteRange shallowCopySubRange(int innerOffset, int copyLength)
        Create a new ByteRange that points at this range's byte[]. The new range can have different values for offset and length, but modifying the shallowCopy will modify the bytes in this range's array. Pass over the hash code if it is already cached.
        Parameters:
        innerOffset - First byte of clone will be this.offset + copyOffset.
        copyLength - Number of bytes in the clone.
        Returns:
        new ByteRange object referencing this range's byte[].