ParsableBitArray
@UnstableApi
@CheckReturnValue
public final class ParsableBitArray
Wraps a byte array, providing methods that allow it to be read as a bitstream.
Summary
Public fields |
|
|---|---|
byte[] |
Public constructors |
|---|
|
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. |
Public methods |
|
|---|---|
int |
bitsLeft()Returns the number of bits yet to be read. |
void |
Aligns the position to the next byte boundary. |
int |
Returns the current byte offset. |
int |
Returns the current bit offset. |
void |
putInt(int value, int numBits)Overwrites |
boolean |
readBit()Reads a single bit. |
int |
readBits(int numBits)Reads up to 32 bits. |
void |
readBits(byte[] buffer, int offset, int numBits)Reads |
long |
readBitsToLong(int numBits)Reads up to 64 bits. |
void |
readBytes(byte[] buffer, int offset, int length)Reads the next |
String |
readBytesAsString(int length)Reads the next |
String |
readBytesAsString(int length, Charset charset)Reads the next |
void |
reset(byte[] data)Updates the instance to wrap |
void |
reset(ParsableByteArray parsableByteArray)Sets this instance's data, position and limit to match the provided |
void |
reset(byte[] data, int limit)Updates the instance to wrap |
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 |
Public fields
Public constructors
ParsableBitArray
public ParsableBitArray()
Creates a new instance that initially has no backing data.
ParsableBitArray
public ParsableBitArray(byte[] data)
Creates a new instance that wraps an existing array.
| Parameters | |
|---|---|
byte[] data |
The data to wrap. |
ParsableBitArray
public ParsableBitArray(byte[] data, int limit)
Creates a new instance that wraps an existing array.
| Parameters | |
|---|---|
byte[] data |
The data to wrap. |
int limit |
The limit in bytes. |
Public methods
byteAlign
public void byteAlign()
Aligns the position to the next byte boundary. Does nothing if the position is already aligned.
getBytePosition
public int getBytePosition()
Returns the current byte offset. Must only be called when the position is byte aligned.
| Throws | |
|---|---|
java.lang.IllegalStateException |
If the position isn't byte aligned. |
putInt
public void putInt(int value, int numBits)
Overwrites 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.
| Parameters | |
|---|---|
int value |
The integer whose |
int numBits |
The number of bits to write. |
readBits
public int readBits(int numBits)
Reads up to 32 bits.
| Parameters | |
|---|---|
int numBits |
The number of bits to read. |
| Returns | |
|---|---|
int |
An integer whose bottom |
readBits
public void readBits(byte[] buffer, int offset, int numBits)
Reads numBits bits into buffer.
| Parameters | |
|---|---|
byte[] buffer |
The array into which the read data should be written. The trailing |
int offset |
The offset in |
int numBits |
The number of bits to read. |
readBitsToLong
public long readBitsToLong(int numBits)
Reads up to 64 bits.
| Parameters | |
|---|---|
int numBits |
The number of bits to read. |
| Returns | |
|---|---|
long |
A long whose bottom |
readBytes
public void readBytes(byte[] buffer, int offset, int length)
Reads the next length bytes into buffer. Must only be called when the position is byte aligned.
| Parameters | |
|---|---|
byte[] buffer |
The array into which the read data should be written. |
int offset |
The offset in |
int length |
The number of bytes to read. |
| Throws | |
|---|---|
java.lang.IllegalStateException |
If the position isn't byte aligned. |
| See also | |
|---|---|
arraycopy |
readBytesAsString
public String readBytesAsString(int length)
Reads the next length bytes as a UTF-8 string. Must only be called when the position is byte aligned.
| Parameters | |
|---|---|
int length |
The number of bytes to read. |
| Returns | |
|---|---|
String |
The string encoded by the bytes in UTF-8. |
readBytesAsString
public String readBytesAsString(int length, Charset charset)
Reads the next length bytes as a string encoded in Charset. Must only be called when the position is byte aligned.
| Parameters | |
|---|---|
int length |
The number of bytes to read. |
Charset charset |
The character set of the encoded characters. |
| Returns | |
|---|---|
String |
The string encoded by the bytes in the specified character set. |
reset
public void reset(byte[] data)
Updates the instance to wrap data, and resets the position to zero.
| Parameters | |
|---|---|
byte[] data |
The array to wrap. |
reset
public void reset(ParsableByteArray parsableByteArray)
Sets this instance's data, position and limit to match the provided parsableByteArray. Any modifications to the underlying data array will be visible in both instances
| Parameters | |
|---|---|
ParsableByteArray parsableByteArray |
The |
reset
public void reset(byte[] data, int limit)
Updates the instance to wrap data, and resets the position to zero.
| Parameters | |
|---|---|
byte[] data |
The array to wrap. |
int limit |
The limit in bytes. |
setPosition
public void setPosition(int position)
Sets the current bit offset.
| Parameters | |
|---|---|
int position |
The position to set. |
skipBits
public void skipBits(int numBits)
Skips bits and moves current reading position forward.
| Parameters | |
|---|---|
int numBits |
The number of bits to skip. |
skipBytes
public void skipBytes(int length)
Skips the next length bytes. Must only be called when the position is byte aligned.
| Parameters | |
|---|---|
int length |
The number of bytes to read. |
| Throws | |
|---|---|
java.lang.IllegalStateException |
If the position isn't byte aligned. |