ParsableByteArray
@UnstableApi
@CheckReturnValue
class ParsableByteArray
Wraps a byte array, providing a set of methods for parsing data from it. Numerical values are parsed with the assumption that their constituent bytes are in big endian order.
Summary
Constants |
|
|---|---|
const Int |
INVALID_CODE_POINT = 1114112A value that is outside the valid range of unicode code points. |
Public constructors |
|---|
|
Creates a new instance that initially has no backing data. |
ParsableByteArray(data: ByteArray!)Creates a new instance wrapping |
ParsableByteArray(limit: Int)Creates a new instance with |
ParsableByteArray(data: ByteArray!, limit: Int)Creates a new instance that wraps an existing array. |
Public functions |
|
|---|---|
Int |
Returns the number of bytes yet to be read. |
Int |
capacity()Returns the capacity of the array, which may be larger than the limit. |
Unit |
ensureCapacity(requiredCapacity: Int)Ensures the backing array is at least |
Int |
limit()Returns the limit. |
Char |
peekChar()Peeks at the next two bytes and interprets them as a big-endian char. |
Char |
This function is deprecated. Either use |
Int |
peekCodePoint(charset: Charset!)Peeks at the code point starting at |
Int |
peekInt()Peeks the next four bytes as a signed value. |
Int |
Peeks at the next byte as an unsigned value. |
Int |
Peeks the next three bytes as an unsigned value. |
Unit |
readBytes(bitArray: ParsableBitArray!, length: Int)Reads the next |
Unit |
readBytes(buffer: ByteBuffer!, length: Int)Reads the next |
Unit |
Reads the next |
String? |
readDelimiterTerminatedString(delimiter: Char)Reads up to the next delimiter byte (or the limit) as UTF-8 characters. |
Double |
Reads the next eight bytes as a 64-bit floating point value. |
Float |
Reads the next four bytes as a 32-bit floating point value. |
Int |
readInt()Reads the next four bytes as a signed value |
Int |
Reads the next three bytes as a signed value. |
String? |
readLine()Reads a line of text in UTF-8. |
String? |
Reads a line of text in |
Int |
Reads the next four bytes as a signed value in little endian order. |
Int |
Reads the next three bytes as a signed value in little endian order. |
Long |
Reads the next eight bytes as a signed value in little endian order. |
Short |
Reads the next two bytes as a signed value. |
Long |
Reads the next four bytes as an unsigned value in little endian order. |
Int |
Reads the next three bytes as an unsigned value in little endian order. |
Int |
Reads the next four bytes as a little endian unsigned integer into an integer, if the top bit is a zero. |
Int |
Reads the next two bytes as an unsigned value. |
Long |
readLong()Reads the next eight bytes as a signed value. |
String? |
Reads up to the next NUL byte (or the limit) as UTF-8 characters. |
String! |
readNullTerminatedString(length: Int)Reads the next |
Short |
Reads the next two bytes as a signed value. |
String! |
readString(length: Int)Reads the next |
String! |
readString(length: Int, charset: Charset!)Reads the next |
Int |
Reads a Synchsafe integer. |
Int |
Reads the next byte as an unsigned value. |
Int |
Reads the next four bytes, returning the integer portion of the fixed point 16.16 integer. |
Long |
Reads the next four bytes as an unsigned value. |
Int |
Reads the next three bytes as an unsigned value. |
Int |
Reads the next four bytes as an unsigned integer into an integer, if the top bit is a zero. |
Int |
Reads an unsigned variable-length LEB128 value into an int. |
Long |
Reads an unsigned variable-length LEB128 value into a long. |
Long |
Reads the next eight bytes as an unsigned long into a long, if the top bit is a zero. |
Int |
Reads the next two bytes as an unsigned value. |
Long |
Reads a long value encoded by UTF-8 encoding |
Charset? |
Reads a UTF byte order mark (BOM) and returns the UTF |
Unit |
Updates the instance to wrap |
Unit |
Resets the position to zero and the limit to the specified value. |
Unit |
Updates the instance to wrap |
Unit |
Sets the limit. |
java-static Unit |
@VisibleForTestingSets whether all read/peek methods should enforce that |
Unit |
Moves the reading offset by |
Unit |
Skips a variable-length LEB128 value. |
Constants
INVALID_CODE_POINT
const val INVALID_CODE_POINT = 1114112: Int
A value that is outside the valid range of unicode code points.
Public constructors
ParsableByteArray
ParsableByteArray(data: ByteArray!)
Creates a new instance wrapping data, and sets the limit to data.length.
| Parameters | |
|---|---|
data: ByteArray! |
The array to wrap. |
ParsableByteArray
ParsableByteArray(limit: Int)
Creates a new instance with limit bytes and sets the limit.
| Parameters | |
|---|---|
limit: Int |
The limit to set. |
ParsableByteArray
ParsableByteArray(data: ByteArray!, limit: Int)
Creates a new instance that wraps an existing array.
Public functions
ensureCapacity
fun ensureCapacity(requiredCapacity: Int): Unit
Ensures the backing array is at least requiredCapacity long.
position, limit, and all data in the underlying array (including that beyond limit) are preserved.
This might replace or wipe the underlying array, potentially invalidating any local references.
peekCodePoint
fun peekCodePoint(charset: Charset!): Int
Peeks at the code point starting at getPosition as interpreted by charset.
The exact behaviour depends on charset:
- US_ASCII: Returns the byte at
getPositionif it's valid ASCII (less than0x80), otherwise returnsINVALID_CODE_POINT. - UTF-8: If
getPositionis the start of a UTF-8 code unit the whole unit is decoded and returned. OtherwiseINVALID_CODE_POINTis returned. - UTF-16 (all endian-nesses):
- If
getPositionis at the start of ahigh surrogatecode unit and the following two bytes are aisLowSurrogatelow surrogate} code unit, thecombined code pointis returned. - Otherwise the single code unit starting at
getPositionis returned directly. - UTF-16 has no support for byte-level synchronization, so if
getPositionis not aligned with the start of a UTF-16 code unit then the result is undefined.
- If
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if charset is not supported. Only US_ASCII, UTF-8, UTF-16, UTF-16BE, and UTF-16LE are supported. |
java.lang.IndexOutOfBoundsException |
if |
readBytes
fun readBytes(bitArray: ParsableBitArray!, length: Int): Unit
Reads the next length bytes into bitArray, and resets the position of
bitArray to zero.
| Parameters | |
|---|---|
bitArray: ParsableBitArray! |
The |
length: Int |
The number of bytes to write. |
readBytes
fun readBytes(buffer: ByteBuffer!, length: Int): Unit
Reads the next length bytes into buffer.
| Parameters | |
|---|---|
buffer: ByteBuffer! |
The |
length: Int |
The number of bytes to read. |
| See also | |
|---|---|
put |
readBytes
fun readBytes(buffer: ByteArray!, offset: Int, length: Int): Unit
Reads the next length bytes into buffer at offset.
| Parameters | |
|---|---|
buffer: ByteArray! |
The array into which the read data should be written. |
offset: Int |
The offset in |
length: Int |
The number of bytes to read. |
| See also | |
|---|---|
arraycopy |
readDelimiterTerminatedString
fun readDelimiterTerminatedString(delimiter: Char): String?
Reads up to the next delimiter byte (or the limit) as UTF-8 characters.
| Returns | |
|---|---|
String? |
The string not including any terminating delimiter byte, or null if the end of the data has already been reached. |
readLine
fun readLine(charset: Charset!): String?
Reads a line of text in charset.
A line is considered to be terminated by any one of a carriage return ('\r'), a line feed ('\n'), or a carriage return followed immediately by a line feed ('\r\n'). This method discards leading UTF byte order marks (BOM), if present.
The position is advanced to start of the next line (i.e. any line terminators are skipped).
| Returns | |
|---|---|
String? |
The line not including any line-termination characters, or null if the end of the data has already been reached. |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if charset is not supported. Only US_ASCII, UTF-8, UTF-16, UTF-16BE, and UTF-16LE are supported. |
readLittleEndianInt
fun readLittleEndianInt(): Int
Reads the next four bytes as a signed value in little endian order.
readLittleEndianInt24
fun readLittleEndianInt24(): Int
Reads the next three bytes as a signed value in little endian order.
readLittleEndianLong
fun readLittleEndianLong(): Long
Reads the next eight bytes as a signed value in little endian order.
readLittleEndianShort
fun readLittleEndianShort(): Short
Reads the next two bytes as a signed value.
readLittleEndianUnsignedInt
fun readLittleEndianUnsignedInt(): Long
Reads the next four bytes as an unsigned value in little endian order.
readLittleEndianUnsignedInt24
fun readLittleEndianUnsignedInt24(): Int
Reads the next three bytes as an unsigned value in little endian order.
readLittleEndianUnsignedIntToInt
fun readLittleEndianUnsignedIntToInt(): Int
Reads the next four bytes as a little endian unsigned integer into an integer, if the top bit is a zero.
| Throws | |
|---|---|
java.lang.IllegalStateException |
Thrown if the top bit of the input data is set. |
readLittleEndianUnsignedShort
fun readLittleEndianUnsignedShort(): Int
Reads the next two bytes as an unsigned value.
readNullTerminatedString
fun readNullTerminatedString(): String?
Reads up to the next NUL byte (or the limit) as UTF-8 characters.
| Returns | |
|---|---|
String? |
The string not including any terminating NUL byte, or null if the end of the data has already been reached. |
readNullTerminatedString
fun readNullTerminatedString(length: Int): String!
Reads the next length bytes as UTF-8 characters. A terminating NUL byte is discarded, if present.
| Parameters | |
|---|---|
length: Int |
The number of bytes to read. |
| Returns | |
|---|---|
String! |
The string, not including any terminating NUL byte. |
readString
fun readString(length: Int): String!
Reads the next length bytes as UTF-8 characters.
| Parameters | |
|---|---|
length: Int |
The number of bytes to read. |
| Returns | |
|---|---|
String! |
The string encoded by the bytes. |
readString
fun readString(length: Int, charset: Charset!): String!
Reads the next length bytes as characters in the specified Charset.
| 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. |
readSynchSafeInt
fun readSynchSafeInt(): Int
Reads a Synchsafe integer.
Synchsafe integers keep the highest bit of every byte zeroed. A 32 bit synchsafe integer can store 28 bits of information.
| Returns | |
|---|---|
Int |
The parsed value. |
readUnsignedFixedPoint1616
fun readUnsignedFixedPoint1616(): Int
Reads the next four bytes, returning the integer portion of the fixed point 16.16 integer.
readUnsignedIntToInt
fun readUnsignedIntToInt(): Int
Reads the next four bytes as an unsigned integer into an integer, if the top bit is a zero.
| Throws | |
|---|---|
java.lang.IllegalStateException |
Thrown if the top bit of the input data is set. |
readUnsignedLeb128ToInt
fun readUnsignedLeb128ToInt(): Int
Reads an unsigned variable-length LEB128 value into an int.
| Returns | |
|---|---|
Int |
integer value |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if the read value is greater than |
readUnsignedLeb128ToLong
fun readUnsignedLeb128ToLong(): Long
Reads an unsigned variable-length LEB128 value into a long.
| Returns | |
|---|---|
Long |
long value |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if the byte to be read is over the limit of the parsable byte array |
readUnsignedLongToLong
fun readUnsignedLongToLong(): Long
Reads the next eight bytes as an unsigned long into a long, if the top bit is a zero.
| Throws | |
|---|---|
java.lang.IllegalStateException |
Thrown if the top bit of the input data is set. |
readUtf8EncodedLong
fun readUtf8EncodedLong(): Long
Reads a long value encoded by UTF-8 encoding
| Returns | |
|---|---|
Long |
Decoded long value |
| Throws | |
|---|---|
java.lang.NumberFormatException |
if there is a problem with decoding |
readUtfCharsetFromBom
fun readUtfCharsetFromBom(): Charset?
Reads a UTF byte order mark (BOM) and returns the UTF Charset it represents. Returns null without advancing position if no BOM is found.
reset
fun reset(data: ByteArray!): Unit
Updates the instance to wrap data, and resets the position to zero and the limit to data.length.
| Parameters | |
|---|---|
data: ByteArray! |
The array to wrap. |
reset
fun reset(limit: Int): Unit
Resets the position to zero and the limit to the specified value. This might replace or wipe the underlying array, potentially invalidating any local references.
| Parameters | |
|---|---|
limit: Int |
The limit to set. |
reset
fun reset(data: ByteArray!, limit: Int): Unit
Updates the instance to wrap data, and resets the position to zero.
setShouldEnforceLimitOnLegacyMethods
@VisibleForTesting
java-static fun setShouldEnforceLimitOnLegacyMethods(enforceLimit: Boolean): Unit
Sets whether all read/peek methods should enforce that getPosition never exceeds limit.
Setting this to true in tests can help catch cases of accidentally reading beyond limit but still within the bounds of the underlying getData.
Some (newer) methods will always enforce the invariant, even when this is set to
false.
Defaults to false (this may change in a later release).
skipBytes
fun skipBytes(bytes: Int): Unit
Moves the reading offset by bytes.
| Parameters | |
|---|---|
bytes: Int |
The number of bytes to skip. |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
Thrown if the new position is neither in nor at the end of the array. |