public final class ImmutableByteSequence
extends java.lang.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.
|
java.nio.ByteBuffer |
asReadOnlyBuffer()
Returns a view of this sequence as a read-only
ByteBuffer . |
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(java.nio.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(java.lang.Object obj) |
static ImmutableByteSequence |
fit(ImmutableByteSequence original,
int bitWidth)
Trims or expands the given byte sequence so to fit a 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.
|
java.lang.String |
toString() |
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(java.nio.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 java.nio.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 int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public 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 java.lang.String toString()
toString
in class java.lang.Object
public static ImmutableByteSequence fit(ImmutableByteSequence original, 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.original
- a byte sequencebitWidth
- a non-zero positive integerImmutableByteSequence.ByteSequenceTrimException
- if the byte sequence cannot be fitted