java.lang.Object
net.i2p.data.ByteArray
- All Implemented Interfaces:
Serializable, Comparable<ByteArray>
Wrap up an array of bytes so that they can be compared and placed in hashes, maps, and the like.
- See Also:
Field Summary
Constructor Summary
Method Summary
Modifier and TypeMethodDescriptionprivate static final booleancompare(byte[] lhs, int loff, int llen, byte[] rhs, int roff, int rlen) True when the two ranges hold the same bytes.final intCompares this ByteArray with another ByteArray.final booleanCompares this ByteArray with another object for equality.byte[]getData()Returns the underlying byte array.intReturns the offset into the byte array.intgetValid()Count how many of the bytes in the array are 'valid'.final inthashCode()Hash code based on the underlying byte array data.voidsetData(byte[] data) Sets the underlying byte array.voidsetOffset(int offset) Offset into the byte array.voidsetValid(int valid) Number of valid bytes.final StringtoBase64()Returns this ByteArray as a Base64 encoded string.toString()Returns a string representation of this ByteArray.
Field Details
_data
private byte[] _dataUnderlying byte array data._valid
private int _validNumber of valid bytes in the array._offset
private int _offsetOffset into the byte array where valid data begins.
Constructor Details
ByteArray
public ByteArray()Creates an empty ByteArray.ByteArray
public ByteArray(byte[] data) Sets valid = data.length, unless data is null Sets offset = 0.- Parameters:
data- the byte array, may be null
ByteArray
public ByteArray(byte[] data, int offset, int length) Wrap the given range of a byte array.- Parameters:
data- the byte array, may be null but why would you do thatoffset- the starting offsetlength- the valid length
Method Details
getData
public byte[] getData()Returns the underlying byte array.- Returns:
- the backing array itself, not a copy; mutating it changes what this object holds
setData
public void setData(byte[] data) Sets the underlying byte array. Warning, does not set valid.- Parameters:
data- the new byte array
getValid
public int getValid()Count how many of the bytes in the array are 'valid'. this property does not necessarily have meaning for all byte arrays.- Returns:
- the number of valid bytes
setValid
public void setValid(int valid) Number of valid bytes.- Parameters:
valid- the new valid count
getOffset
public int getOffset()Returns the offset into the byte array.- Returns:
- the index of the first live byte within
getData()
setOffset
public void setOffset(int offset) Offset into the byte array.- Parameters:
offset- the new offset
equals
compare
private static final boolean compare(byte[] lhs, int loff, int llen, byte[] rhs, int roff, int rlen) True when the two ranges hold the same bytes.- Parameters:
lhs- the first arrayloff- where its range startsllen- its range lengthrhs- the second arrayroff- where its range startsrlen- its range length- Returns:
- true if the ranges are equal
compareTo
Compares this ByteArray with another ByteArray.- Specified by:
compareToin interfaceComparable<ByteArray>
hashCode
toString
toBase64
Returns this ByteArray as a Base64 encoded string.- Returns:
- the Base64 encoded string