public class ReadOnlyByteArray
extends java.lang.Object
base
array and an
interval [offset, offset+length]
contained within
[0, base.length]
, an instance of this class provides read
access to the elements from base[offset]
to
base[offset+length-1]
. However, you access the elements by
specifying relative indeces to the get
method so
get(0)
returns base[offset]
,
get(1)
returns base[offset+1]
, and so on. One
thing to bear in mind is that changes to the original base
array will be reflected to the corresponding ReadOnlyByteArray
object as this class simply keeps a reference to the original
base
array, without making an internal copy. This can be
useful in those situations when you want to emulate a memory pointer.Modifier and Type | Field and Description |
---|---|
protected byte[] |
base
The original array.
|
int |
length
The length of the slice.
|
protected int |
offset
Marks the start of the slice.
|
Constructor and Description |
---|
ReadOnlyByteArray(byte[] base,
int offset,
int length)
Creates a read-only slice of
base . |
Modifier and Type | Method and Description |
---|---|
protected void |
checkIndex(int i)
Makes sure that
i is in [0, length) . |
byte |
get(int index)
Reads the element at the
index position within this slice. |
protected final byte[] base
protected final int offset
public final int length
public ReadOnlyByteArray(byte[] base, int offset, int length)
base
. The
offset
argument marks the start of the slice, at
base[offset]
. The length
argument defines
the length of the slice, the last element being
base[offset+length-1]
. Obviously enough, these two
arguments must define an interval [offset, offset+length]
in [0, base.length]
.base
- The original array.offset
- The start of the slice.length
- The length of the slice.protected void checkIndex(int i)
i
is in [0, length)
.
Throws an exception if this constraint is not met.i
- The index to verify.public byte get(int index)
index
position within this slice.index
- The index. Must be in the [0, length
)
interval.index
position in this slice.