ParsableBitArray


@UnstableApi
@CheckReturnValue
class ParsableBitArray


Wraps a byte array, providing methods that allow it to be read as a bitstream.

Summary

Public constructors

Creates a new instance that initially has no backing data.

Creates a new instance that wraps an existing array.

ParsableBitArray(data: ByteArray!, limit: Int)

Creates a new instance that wraps an existing array.

Public functions

Int

Returns the number of bits yet to be read.

Unit

Aligns the position to the next byte boundary.

Int

Returns the current byte offset.

Int

Returns the current bit offset.

Unit
putInt(value: Int, numBits: Int)

Overwrites numBits from this array using the numBits least significant bits from value.

Boolean

Reads a single bit.

Int
readBits(numBits: Int)

Reads up to 32 bits.

Unit
readBits(buffer: ByteArray!, offset: Int, numBits: Int)

Reads numBits bits into buffer.

Long
readBitsToLong(numBits: Int)

Reads up to 64 bits.

Unit
readBytes(buffer: ByteArray!, offset: Int, length: Int)

Reads the next length bytes into buffer.

String!

Reads the next length bytes as a UTF-8 string.

String!
readBytesAsString(length: Int, charset: Charset!)

Reads the next length bytes as a string encoded in Charset.

Unit
reset(data: ByteArray!)

Updates the instance to wrap data, and resets the position to zero.

Unit
reset(parsableByteArray: ParsableByteArray!)

Sets this instance's data, position and limit to match the provided parsableByteArray.

Unit
reset(data: ByteArray!, limit: Int)

Updates the instance to wrap data, and resets the position to zero.

Unit
setPosition(position: Int)

Sets the current bit offset.

Unit

Skips a single bit.

Unit
skipBits(numBits: Int)

Skips bits and moves current reading position forward.

Unit
skipBytes(length: Int)

Skips the next length bytes.

Public properties

ByteArray<Byte>!

Public constructors

ParsableBitArray

ParsableBitArray()

Creates a new instance that initially has no backing data.

ParsableBitArray

ParsableBitArray(data: ByteArray!)

Creates a new instance that wraps an existing array.

Parameters
data: ByteArray!

The data to wrap.

ParsableBitArray

ParsableBitArray(data: ByteArray!, limit: Int)

Creates a new instance that wraps an existing array.

Parameters
data: ByteArray!

The data to wrap.

limit: Int

The limit in bytes.

Public functions

bitsLeft

fun bitsLeft(): Int

Returns the number of bits yet to be read.

byteAlign

fun byteAlign(): Unit

Aligns the position to the next byte boundary. Does nothing if the position is already aligned.

getBytePosition

fun getBytePosition(): Int

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.

getPosition

fun getPosition(): Int

Returns the current bit offset.

putInt

fun putInt(value: Int, numBits: Int): Unit

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
value: Int

The integer whose numBits least significant bits are written into data.

numBits: Int

The number of bits to write.

readBit

fun readBit(): Boolean

Reads a single bit.

Returns
Boolean

Whether the bit is set.

readBits

fun readBits(numBits: Int): Int

Reads up to 32 bits.

Parameters
numBits: Int

The number of bits to read.

Returns
Int

An integer whose bottom numBits bits hold the read data.

readBits

fun readBits(buffer: ByteArray!, offset: Int, numBits: Int): Unit

Reads numBits bits into buffer.

Parameters
buffer: ByteArray!

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: Int

The offset in buffer at which the read data should be written.

numBits: Int

The number of bits to read.

readBitsToLong

fun readBitsToLong(numBits: Int): Long

Reads up to 64 bits.

Parameters
numBits: Int

The number of bits to read.

Returns
Long

A long whose bottom numBits bits hold the read data.

readBytes

fun readBytes(buffer: ByteArray!, offset: Int, length: Int): Unit

Reads the next length bytes into buffer. Must only be called when the position is byte aligned.

Parameters
buffer: ByteArray!

The array into which the read data should be written.

offset: Int

The offset in buffer at which the read data should be written.

length: Int

The number of bytes to read.

Throws
java.lang.IllegalStateException

If the position isn't byte aligned.

See also
arraycopy

readBytesAsString

fun readBytesAsString(length: Int): String!

Reads the next length bytes as a UTF-8 string. Must only be called when the position is byte aligned.

Parameters
length: Int

The number of bytes to read.

Returns
String!

The string encoded by the bytes in UTF-8.

readBytesAsString

fun readBytesAsString(length: Int, charset: Charset!): String!

Reads the next length bytes as a string encoded in Charset. Must only be called when the position is byte aligned.

Parameters
length: Int

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

fun reset(data: ByteArray!): Unit

Updates the instance to wrap data, and resets the position to zero.

Parameters
data: ByteArray!

The array to wrap.

reset

fun reset(parsableByteArray: ParsableByteArray!): Unit

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 ParsableByteArray.

reset

fun reset(data: ByteArray!, limit: Int): Unit

Updates the instance to wrap data, and resets the position to zero.

Parameters
data: ByteArray!

The array to wrap.

limit: Int

The limit in bytes.

setPosition

fun setPosition(position: Int): Unit

Sets the current bit offset.

Parameters
position: Int

The position to set.

skipBit

fun skipBit(): Unit

Skips a single bit.

skipBits

fun skipBits(numBits: Int): Unit

Skips bits and moves current reading position forward.

Parameters
numBits: Int

The number of bits to skip.

skipBytes

fun skipBytes(length: Int): Unit

Skips the next length bytes. Must only be called when the position is byte aligned.

Parameters
length: Int

The number of bytes to read.

Throws
java.lang.IllegalStateException

If the position isn't byte aligned.

Public properties

data

val dataByteArray<Byte>!