Class ImmutableByteSequence
- java.lang.Object
-
- org.onlab.util.ImmutableByteSequence
-
public final class ImmutableByteSequence extends java.lang.ObjectImmutable sequence of bytes, assumed to represent a value inBIG_ENDIANorder.Sequences can be created copying from an already existing representation of a sequence of bytes, such as
ByteBufferorbyte[]; or by copying bytes from a primitive data type, such aslong,intorshort. 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classImmutableByteSequence.ByteSequenceTrimExceptionSignals that a byte sequence cannot be trimmed.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]asArray()Creates a new byte array view of this sequence.java.nio.ByteBufferasReadOnlyBuffer()Returns a view of this sequence as a read-onlyByteBuffer.ImmutableByteSequencebitwiseAnd(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.ImmutableByteSequencebitwiseOr(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.ImmutableByteSequencebitwiseXor(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 ImmutableByteSequencecopyFrom(byte original)Creates a new byte sequence of 1 byte containing the given value.static ImmutableByteSequencecopyFrom(byte[] original)Creates a new immutable byte sequence with the same content and order of the passed byte array.static ImmutableByteSequencecopyFrom(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 ImmutableByteSequencecopyFrom(int original)Creates a new byte sequence of 4 bytes containing the given int value.static ImmutableByteSequencecopyFrom(long original)Creates a new byte sequence of 8 bytes containing the given long value.static ImmutableByteSequencecopyFrom(short original)Creates a new byte sequence of 2 bytes containing the given short value.static ImmutableByteSequencecopyFrom(java.nio.ByteBuffer original)Creates a new immutable byte sequence copying bytes from the given ByteBufferByteBuffer.booleanequals(java.lang.Object obj)ImmutableByteSequencefit(int bitWidth)Trims or expands a copy of this byte sequence so to fit the given bit-width.inthashCode()intmsbIndex()Returns the index of the most significant bit (MSB), assuming a bit numbering scheme of type "LSB 0", i.e.static ImmutableByteSequenceofOnes(int size)Creates a new byte sequence of the given size where all bits are 1.static ImmutableByteSequenceofZeros(int size)Creates a new byte sequence of the given size where all bits are 0.static ImmutableByteSequenceprefixOnes(int size, long prefixBits)Creates a new byte sequence that is prefixed with specified number of ones.static ImmutableByteSequenceprefixZeros(int size, long prefixBits)Creates a new byte sequence that is prefixed with specified number of zeros.intsize()Gets the number of bytes in this sequence.java.lang.StringtoString()Returns a hexadecimal representation of this byte sequence, e.g.
-
-
-
Method Detail
-
copyFrom
public static ImmutableByteSequence copyFrom(byte[] original)
Creates a new immutable byte sequence with the same content and order of the passed byte array.- Parameters:
original- a byte array value- Returns:
- a new immutable byte sequence
-
copyFrom
public 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).- Parameters:
original- a byte array valuefromIdx- starting indextoIdx- ending index- Returns:
- a new immutable byte sequence
-
copyFrom
public static ImmutableByteSequence copyFrom(java.nio.ByteBuffer original)
Creates a new immutable byte sequence copying bytes from the given ByteBufferByteBuffer. If the byte buffer order is not big-endian bytes will be copied in reverse order.- Parameters:
original- a byte buffer- Returns:
- a new byte buffer object
-
copyFrom
public static ImmutableByteSequence copyFrom(long original)
Creates a new byte sequence of 8 bytes containing the given long value.- Parameters:
original- a long value- Returns:
- a new immutable byte sequence
-
copyFrom
public static ImmutableByteSequence copyFrom(int original)
Creates a new byte sequence of 4 bytes containing the given int value.- Parameters:
original- an int value- Returns:
- a new immutable byte sequence
-
copyFrom
public static ImmutableByteSequence copyFrom(short original)
Creates a new byte sequence of 2 bytes containing the given short value.- Parameters:
original- a short value- Returns:
- a new immutable byte sequence
-
copyFrom
public static ImmutableByteSequence copyFrom(byte original)
Creates a new byte sequence of 1 byte containing the given value.- Parameters:
original- a byte value- Returns:
- a new immutable byte sequence
-
ofZeros
public static ImmutableByteSequence ofZeros(int size)
Creates a new byte sequence of the given size where all bits are 0.- Parameters:
size- number of bytes- Returns:
- a new immutable byte sequence
-
ofOnes
public static ImmutableByteSequence ofOnes(int size)
Creates a new byte sequence of the given size where all bits are 1.- Parameters:
size- number of bytes- Returns:
- a new immutable byte sequence
-
prefixZeros
public static ImmutableByteSequence prefixZeros(int size, long prefixBits)
Creates a new byte sequence that is prefixed with specified number of zeros.- Parameters:
size- number of total bytesprefixBits- number of bits in prefix- Returns:
- new immutable byte sequence
-
prefixOnes
public static ImmutableByteSequence prefixOnes(int size, long prefixBits)
Creates a new byte sequence that is prefixed with specified number of ones.- Parameters:
size- number of total bytesprefixBits- number of bits in prefix- Returns:
- new immutable byte sequence
-
asReadOnlyBuffer
public java.nio.ByteBuffer asReadOnlyBuffer()
Returns a view of this sequence as a read-onlyByteBuffer.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.- Returns:
- a read-only byte buffer
-
size
public int size()
Gets the number of bytes in this sequence.- Returns:
- an integer value
-
asArray
public byte[] asArray()
Creates a new byte array view of this sequence.- Returns:
- a new byte array
-
bitwiseAnd
public 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.this & other.- Parameters:
other- other byte sequence- Returns:
- new byte sequence
- Throws:
java.lang.IllegalArgumentException- if other sequence is null or its size is different than this sequence size
-
bitwiseOr
public 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.this | other.- Parameters:
other- other byte sequence- Returns:
- new byte sequence
- Throws:
java.lang.IllegalArgumentException- if other sequence is null or its size is different than this sequence size
-
bitwiseXor
public 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.this ^ other.- Parameters:
other- other byte sequence- Returns:
- new byte sequence
- Throws:
java.lang.IllegalArgumentException- if other sequence is null or its size is different than this sequence size
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
msbIndex
public int msbIndex()
Returns the index of the most significant bit (MSB), assuming a bit numbering scheme of type "LSB 0", i.e. the bit numbering starts at zero for the least significant bit (LSB). The MSB index of a byte sequence of zeros will be -1.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- Returns:
- index of the MSB, -1 if the sequence has all bytes set to 0
-
toString
public java.lang.String toString()
Returns a hexadecimal representation of this byte sequence, e.g. 0xbeef. The length of the returned string is not representative of the length of the byte sequence, as all padding zeros are removed.- Overrides:
toStringin classjava.lang.Object- Returns:
- hexadecimal representation
-
fit
public ImmutableByteSequence fit(int bitWidth) throws ImmutableByteSequence.ByteSequenceTrimException
Trims or expands a copy of this byte sequence so to fit the given bit-width. When trimming, the operations is deemed to be safe only if the trimmed bits are zero, i.e. it is safe to trim only whenbitWidth > 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.- Parameters:
bitWidth- a non-zero positive integer- Returns:
- a new byte sequence
- Throws:
ImmutableByteSequence.ByteSequenceTrimException- if the byte sequence cannot be fitted
-
-