public final class ImmutableByteSequence extends Object
BIG_ENDIAN
order.
Sequences can be created copying from an already existing representation of a
sequence of bytes, such as ByteBuffer
or byte[]
; or by
copying bytes from a primitive data type, such as long
, int
or short
. In the first case, bytes are assumed to be already given in
big-endian order, while in the second case big-endianness is enforced by this
class.
Modifier and Type | Class and Description |
---|---|
static class |
ImmutableByteSequence.ByteSequenceTrimException
Signals that a byte sequence cannot be trimmed.
|
Modifier and Type | Method and Description |
---|---|
byte[] |
asArray()
Creates a new byte array view of this sequence.
|
ByteBuffer |
asReadOnlyBuffer()
Returns a view of this sequence as a read-only
ByteBuffer . |
ImmutableByteSequence |
bitwiseAnd(ImmutableByteSequence other)
Returns a new byte sequence corresponding to the result of a bitwise AND
operation between this sequence and the given other, i.e.
|
ImmutableByteSequence |
bitwiseOr(ImmutableByteSequence other)
Returns a new byte sequence corresponding to the result of a bitwise OR
operation between this sequence and the given other, i.e.
|
ImmutableByteSequence |
bitwiseXor(ImmutableByteSequence other)
Returns a new byte sequence corresponding to the result of a bitwise XOR
operation between this sequence and the given other, i.e.
|
static ImmutableByteSequence |
copyFrom(byte original)
Creates a new byte sequence of 1 byte containing the given value.
|
static ImmutableByteSequence |
copyFrom(byte[] original)
Creates a new immutable byte sequence with the same content and order of
the passed byte array.
|
static ImmutableByteSequence |
copyFrom(byte[] original,
int fromIdx,
int toIdx)
Creates a new immutable byte sequence with the same content and order of
the passed byte array, from/to the given indexes (inclusive).
|
static ImmutableByteSequence |
copyFrom(ByteBuffer original)
Creates a new immutable byte sequence copying bytes from the given
ByteBuffer
ByteBuffer . |
static ImmutableByteSequence |
copyFrom(int original)
Creates a new byte sequence of 4 bytes containing the given int value.
|
static ImmutableByteSequence |
copyFrom(long original)
Creates a new byte sequence of 8 bytes containing the given long value.
|
static ImmutableByteSequence |
copyFrom(short original)
Creates a new byte sequence of 2 bytes containing the given short value.
|
boolean |
equals(Object obj) |
ImmutableByteSequence |
fit(int bitWidth)
Trims or expands a copy of this byte sequence so to fit the given
bit-width.
|
int |
hashCode() |
int |
msbIndex()
Returns the index of the most significant bit (MSB), assuming a bit
numbering scheme of type "LSB 0", i.e.
|
static ImmutableByteSequence |
ofOnes(int size)
Creates a new byte sequence of the given size where all bits are 1.
|
static ImmutableByteSequence |
ofZeros(int size)
Creates a new byte sequence of the given size where all bits are 0.
|
static ImmutableByteSequence |
prefixOnes(int size,
long prefixBits)
Creates a new byte sequence that is prefixed with specified number of
ones.
|
static ImmutableByteSequence |
prefixZeros(int size,
long prefixBits)
Creates a new byte sequence that is prefixed with specified number of
zeros.
|
int |
size()
Gets the number of bytes in this sequence.
|
String |
toString()
Returns a hexadecimal representation of this byte sequence, e.g.
|
public static ImmutableByteSequence copyFrom(byte[] original)
original
- a byte array valuepublic static ImmutableByteSequence copyFrom(byte[] original, int fromIdx, int toIdx)
original
- a byte array valuefromIdx
- starting indextoIdx
- ending indexpublic static ImmutableByteSequence copyFrom(ByteBuffer original)
ByteBuffer
. If the byte buffer order is not big-endian
bytes will be copied in reverse order.original
- a byte bufferpublic static ImmutableByteSequence copyFrom(long original)
original
- a long valuepublic static ImmutableByteSequence copyFrom(int original)
original
- an int valuepublic static ImmutableByteSequence copyFrom(short original)
original
- a short valuepublic static ImmutableByteSequence copyFrom(byte original)
original
- a byte valuepublic static ImmutableByteSequence ofZeros(int size)
size
- number of bytespublic static ImmutableByteSequence ofOnes(int size)
size
- number of bytespublic static ImmutableByteSequence prefixZeros(int size, long prefixBits)
size
- number of total bytesprefixBits
- number of bits in prefixpublic static ImmutableByteSequence prefixOnes(int size, long prefixBits)
size
- number of total bytesprefixBits
- number of bits in prefixpublic ByteBuffer asReadOnlyBuffer()
ByteBuffer
.
The returned buffer will have position 0, while limit and capacity will
be set to this sequence size()
. The buffer order will be
big-endian.
public int size()
public byte[] asArray()
public ImmutableByteSequence bitwiseAnd(ImmutableByteSequence other)
this &
other
.other
- other byte sequenceIllegalArgumentException
- if other sequence is null or its size is
different than this sequence sizepublic ImmutableByteSequence bitwiseOr(ImmutableByteSequence other)
this |
other
.other
- other byte sequenceIllegalArgumentException
- if other sequence is null or its size is
different than this sequence sizepublic ImmutableByteSequence bitwiseXor(ImmutableByteSequence other)
this ^
other
.other
- other byte sequenceIllegalArgumentException
- if other sequence is null or its size is
different than this sequence sizepublic int msbIndex()
As an example, the following conditions always hold true: ImmutableByteSequence.copyFrom(0).msbIndex() == -1
ImmutableByteSequence.copyFrom(1).msbIndex() == 0
ImmutableByteSequence.copyFrom(2).msbIndex() == 1
ImmutableByteSequence.copyFrom(3).msbIndex() == 1
ImmutableByteSequence.copyFrom(4).msbIndex() == 2
ImmutableByteSequence.copyFrom(512).msbIndex() == 9
public String toString()
public ImmutableByteSequence fit(int bitWidth) throws ImmutableByteSequence.ByteSequenceTrimException
bitWidth
> msbIndex()
, otherwise an exception will be thrown. When expanding, the
sequence will be padded with zeros. The returned byte sequence will have
minimum size to contain the given bit-width.bitWidth
- a non-zero positive integerImmutableByteSequence.ByteSequenceTrimException
- if the byte sequence cannot be fitted