Interface ByteRange
- All Superinterfaces:
Comparable<ByteRange>
- All Known Subinterfaces:
PositionedByteRange
- All Known Implementing Classes:
AbstractByteRange,AbstractPositionedByteRange,SimpleByteRange,SimpleMutableByteRange,SimplePositionedByteRange,SimplePositionedMutableByteRange
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
byteaccess 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 TypeMethodDescriptiondeepCopy()Create a newByteRangewith new backing byte[] containing a copy of the content fromthisrange's window.voiddeepCopySubRangeTo(int innerOffset, int copyLength, byte[] destination, int destinationOffset) Wrapper for System.arraycopy.voiddeepCopyTo(byte[] destination, int destinationOffset) Wrapper for System.arraycopy.byte[]Instantiate a new byte[] with exact length, which is at least 24 bytes + length.byteget(int index) Retrieve the byte atindex.get(int index, byte[] dst) Filldstwith bytes from the range, starting fromindex.get(int index, byte[] dst, int offset, int length) Filldstwith bytes from the range, starting fromindex.byte[]getBytes()The underlying byte[].intgetInt(int index) Retrieve the int value atindexintThe length of the range.longgetLong(int index) Retrieve the long value atindexintThe offset, the index into the underlying byte[] at which this range begins.shortgetShort(int index) Retrieve the short value atindexlonggetVLong(int index) Retrieve the long value atindexwhich is stored as VLongbooleanisEmpty()Returns true when this range is of zero length, false otherwise.put(int index, byte val) Storevalatindex.put(int index, byte[] val) Storevalatindex.put(int index, byte[] val, int offset, int length) Storelengthbytes fromvalinto this range, starting atindex.putInt(int index, int val) Store the int value atindexputLong(int index, long val) Store the long value atindexputShort(int index, short val) Store the short value atindexintputVLong(int index, long val) Store the long value atindexas a VLongset(byte[] bytes) Reuse thisByteRangeover a new byte[].set(byte[] bytes, int offset, int length) Reuse thisByteRangeover a new byte[].set(int capacity) Reuse thisByteRangeover a new byte[].setLength(int length) Update the length of this range.setOffset(int offset) Update the beginning of this range.Create a newByteRangethat points at this range's byte[].shallowCopySubRange(int innerOffset, int copyLength) Create a newByteRangethat points at this range's byte[].unset()Nullifies this ByteRange.Methods inherited from interface java.lang.Comparable
compareTo
-
Method Details
-
getBytes
byte[] getBytes()The underlying byte[]. -
unset
Nullifies this ByteRange. That is, it becomes a husk, being a range over no byte[] whatsoever. -
set
Reuse thisByteRangeover a new byte[].offsetis set to 0 andlengthis set tocapacity.- Parameters:
capacity- the size of a new byte[].
-
set
Reuse thisByteRangeover a new byte[].offsetis set to 0 andlengthis set tobytes.length. A nullbytesIS supported, in which case this method will behave equivalently tounset().- Parameters:
bytes- the array to wrap.
-
set
Reuse thisByteRangeover a new byte[]. A nullbytesIS supported, in which case this method will behave equivalently tounset(), regardless of the values ofoffsetandlength.- Parameters:
bytes- The array to wrap.offset- The offset intobytesconsidered 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
Update the beginning of this range.offset + lengthmay not be greater thanbytes.length.- Parameters:
offset- the new start of this range.- Returns:
- this.
-
getLength
int getLength()The length of the range. -
setLength
Update the length of this range.offset + lengthshould not be greater thanbytes.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
Retrieve the byte atindex.- Parameters:
index- zero-based index into this range.- Returns:
- single byte at index.
-
getShort
Retrieve the short value atindex- Parameters:
index- zero-based index into this range- Returns:
- the short value at
index
-
getInt
Retrieve the int value atindex- Parameters:
index- zero-based index into this range- Returns:
- the int value at
index
-
getLong
Retrieve the long value atindex- Parameters:
index- zero-based index into this range- Returns:
- the long value at
index
-
getVLong
Retrieve the long value atindexwhich is stored as VLong- Parameters:
index- zero-based index into this range- Returns:
- the long value at
indexwhich is stored as VLong
-
get
Filldstwith bytes from the range, starting fromindex.- Parameters:
index- zero-based index into this range.dst- the destination of the copy.- Returns:
- this.
-
get
Filldstwith bytes from the range, starting fromindex.lengthbytes are copied intodst, starting atoffset.- Parameters:
index- zero-based index into this range.dst- the destination of the copy.offset- the offset intodstto start the copy.length- the number of bytes to copy intodst.- Returns:
- this.
-
put
Storevalatindex.- Parameters:
index- the index in the range wherevalis stored.val- the value to store.- Returns:
- this.
-
putShort
Store the short value atindex- Parameters:
index- the index in the range wherevalis storedval- the value to store
-
putInt
Store the int value atindex- Parameters:
index- the index in the range wherevalis storedval- the value to store
-
putLong
Store the long value atindex- Parameters:
index- the index in the range wherevalis storedval- the value to store
-
putVLong
Store the long value atindexas a VLong- Parameters:
index- the index in the range wherevalis storedval- the value to store- Returns:
- number of bytes written
-
put
Storevalatindex.- Parameters:
index- the index in the range wherevalis stored.val- the value to store.- Returns:
- this.
-
put
Storelengthbytes fromvalinto this range, starting atindex. Bytes fromvalare copied starting atoffsetinto the range.- Parameters:
index- position in this range to start the copy.val- the value to store.offset- the offset invalfrom which to start copying.length- the number of bytes to copy fromval.- 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
Create a newByteRangewith new backing byte[] containing a copy of the content fromthisrange's window.- Returns:
- Deep copy
-
deepCopyTo
Wrapper for System.arraycopy. Copy the contents of this range into the provided array.- Parameters:
destination- Copy to this arraydestinationOffset- First index in the destination array.
-
deepCopySubRangeTo
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 bytesdestination- Copy to this arraydestinationOffset- First index in the destination array.
-
shallowCopy
Create a newByteRangethat 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
ByteRangeobject referencing this range's byte[].
-
shallowCopySubRange
Create a newByteRangethat 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
ByteRangeobject referencing this range's byte[].
-