public final class ParsableBitArray
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
byte[] |
data |
Constructor and Description |
---|
ParsableBitArray()
Creates a new instance that initially has no backing data.
|
ParsableBitArray(byte[] data)
Creates a new instance that wraps an existing array.
|
ParsableBitArray(byte[] data,
int limit)
Creates a new instance that wraps an existing array.
|
Modifier and Type | Method and Description |
---|---|
int |
bitsLeft()
Returns the number of bits yet to be read.
|
void |
byteAlign()
Aligns the position to the next byte boundary.
|
int |
getBytePosition()
Returns the current byte offset.
|
int |
getPosition()
Returns the current bit offset.
|
void |
putInt(int value,
int numBits)
Overwrites
numBits from this array using the numBits least significant bits
from value . |
boolean |
readBit()
Reads a single bit.
|
void |
readBits(byte[] buffer,
int offset,
int numBits)
Reads
numBits bits into buffer . |
int |
readBits(int numBits)
Reads up to 32 bits.
|
void |
readBytes(byte[] buffer,
int offset,
int length)
Reads the next
length bytes into buffer . |
void |
reset(byte[] data)
Updates the instance to wrap
data , and resets the position to zero. |
void |
reset(byte[] data,
int limit)
Updates the instance to wrap
data , and resets the position to zero. |
void |
reset(ParsableByteArray parsableByteArray)
Sets this instance's data, position and limit to match the provided
parsableByteArray . |
void |
setPosition(int position)
Sets the current bit offset.
|
void |
skipBit()
Skips a single bit.
|
void |
skipBits(int numBits)
Skips bits and moves current reading position forward.
|
void |
skipBytes(int length)
Skips the next
length bytes. |
public ParsableBitArray()
public ParsableBitArray(byte[] data)
data
- The data to wrap.public ParsableBitArray(byte[] data, int limit)
data
- The data to wrap.limit
- The limit in bytes.public void reset(byte[] data)
data
, and resets the position to zero.data
- The array to wrap.public void reset(ParsableByteArray parsableByteArray)
parsableByteArray
.
Any modifications to the underlying data array will be visible in both instancesparsableByteArray
- The ParsableByteArray
.public void reset(byte[] data, int limit)
data
, and resets the position to zero.data
- The array to wrap.limit
- The limit in bytes.public int bitsLeft()
public int getPosition()
public int getBytePosition()
java.lang.IllegalStateException
- If the position isn't byte aligned.public void setPosition(int position)
position
- The position to set.public void skipBit()
public void skipBits(int numBits)
numBits
- The number of bits to skip.public boolean readBit()
public int readBits(int numBits)
numBits
- The number of bits to read.public void readBits(byte[] buffer, int offset, int numBits)
numBits
bits into buffer
.buffer
- The array into which the read data should be written. The trailing
numBits % 8
bits are written into the most significant bits of the last modified
buffer
byte. The remaining ones are unmodified.offset
- The offset in buffer
at which the read data should be written.numBits
- The number of bits to read.public void byteAlign()
public void readBytes(byte[] buffer, int offset, int length)
length
bytes into buffer
. Must only be called when the position
is byte aligned.buffer
- The array into which the read data should be written.offset
- The offset in buffer
at which the read data should be written.length
- The number of bytes to read.java.lang.IllegalStateException
- If the position isn't byte aligned.System.arraycopy(Object, int, Object, int, int)
public void skipBytes(int length)
length
bytes. Must only be called when the position is byte aligned.length
- The number of bytes to read.java.lang.IllegalStateException
- If the position isn't byte aligned.public void putInt(int value, int numBits)
numBits
from this array using the numBits
least significant bits
from value
. Bits are written in order from most significant to least significant. The
read position is advanced by numBits
.value
- The integer whose numBits
least significant bits are written into data
.numBits
- The number of bits to write.