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
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 TypeMethodDescriptiondeepCopy()
Create a newByteRange
with new backing byte[] containing a copy of the content fromthis
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 atindex
.get
(int index, byte[] dst) Filldst
with bytes from the range, starting fromindex
.get
(int index, byte[] dst, int offset, int length) Filldst
with bytes from the range, starting fromindex
.byte[]
getBytes()
The underlying byte[].int
getInt
(int index) Retrieve the int value atindex
int
The length of the range.long
getLong
(int index) Retrieve the long value atindex
int
The offset, the index into the underlying byte[] at which this range begins.short
getShort
(int index) Retrieve the short value atindex
long
getVLong
(int index) Retrieve the long value atindex
which is stored as VLongboolean
isEmpty()
Returns true when this range is of zero length, false otherwise.put
(int index, byte val) Storeval
atindex
.put
(int index, byte[] val) Storeval
atindex
.put
(int index, byte[] val, int offset, int length) Storelength
bytes fromval
into this range, starting atindex
.putInt
(int index, int val) Store the int value atindex
putLong
(int index, long val) Store the long value atindex
putShort
(int index, short val) Store the short value atindex
int
putVLong
(int index, long val) Store the long value atindex
as a VLongset
(byte[] bytes) Reuse thisByteRange
over a new byte[].set
(byte[] bytes, int offset, int length) Reuse thisByteRange
over a new byte[].set
(int capacity) Reuse thisByteRange
over a new byte[].setLength
(int length) Update the length of this range.setOffset
(int offset) Update the beginning of this range.Create a newByteRange
that points at this range's byte[].shallowCopySubRange
(int innerOffset, int copyLength) Create a newByteRange
that 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 thisByteRange
over a new byte[].offset
is set to 0 andlength
is set tocapacity
.- Parameters:
capacity
- the size of a new byte[].
-
set
Reuse thisByteRange
over a new byte[].offset
is set to 0 andlength
is set tobytes.length
. A nullbytes
IS supported, in which case this method will behave equivalently tounset()
.- Parameters:
bytes
- the array to wrap.
-
set
Reuse thisByteRange
over a new byte[]. A nullbytes
IS supported, in which case this method will behave equivalently tounset()
, regardless of the values ofoffset
andlength
.- Parameters:
bytes
- The array to wrap.offset
- The offset intobytes
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
Update the beginning of this range.offset + length
may 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 + length
should 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 atindex
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
Filldst
with bytes from the range, starting fromindex
.- Parameters:
index
- zero-based index into this range.dst
- the destination of the copy.- Returns:
- this.
-
get
Filldst
with bytes from the range, starting fromindex
.length
bytes are copied intodst
, starting atoffset
.- Parameters:
index
- zero-based index into this range.dst
- the destination of the copy.offset
- the offset intodst
to start the copy.length
- the number of bytes to copy intodst
.- Returns:
- this.
-
put
Storeval
atindex
.- Parameters:
index
- the index in the range whereval
is stored.val
- the value to store.- Returns:
- this.
-
putShort
Store the short value atindex
- Parameters:
index
- the index in the range whereval
is storedval
- the value to store
-
putInt
Store the int value atindex
- Parameters:
index
- the index in the range whereval
is storedval
- the value to store
-
putLong
Store the long value atindex
- Parameters:
index
- the index in the range whereval
is storedval
- the value to store
-
putVLong
Store the long value atindex
as a VLong- Parameters:
index
- the index in the range whereval
is storedval
- the value to store- Returns:
- number of bytes written
-
put
Storeval
atindex
.- Parameters:
index
- the index in the range whereval
is stored.val
- the value to store.- Returns:
- this.
-
put
Storelength
bytes fromval
into this range, starting atindex
. Bytes fromval
are copied starting atoffset
into the range.- Parameters:
index
- position in this range to start the copy.val
- the value to store.offset
- the offset inval
from 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 newByteRange
with new backing byte[] containing a copy of the content fromthis
range'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 newByteRange
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
Create a newByteRange
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[].
-