dreamworkAPI
Ctrl K
GitHub
classDocs / BinaryReader

BinaryReader

classsharedmenusource
Object → BinaryReader

A cursor over an immutable binary string, reads values one after another from any bit position.

The cursor (position) is counted in bits from 0, so fields don't have to be aligned to bytes, methods that take as_bytes count in bytes instead.

Bits are read LSB-first, same as pack and Source's bf_read, so data written by BinaryWriter or gmod net.Write* can be read back:

lua
local reader = BinaryReader( "\5\44\1" )

reader:readBit() --> 1
reader:readUInt( 7, true ) --> 2
reader:readUInt( 16, true ) --> 300
reader:isEndOfFile() --> true

Multi-byte values are big endian by default, is_little_endian switches them to little endian, see pack.readUInt for the exact bit layout of both.

All reads throw EndOfFileError when there is not enough data left, the cursor is not moved in that case.

Constructor

BinaryReader(data: any): BinaryReader

Fields

NameTypeDescription
positionintegerThe cursor position in bits.
datastringThe binary data.
data_lengthintegerThe size of the data in bits.

Operators

OperatorOperandResult
leninteger
concat any string

Methods

alignMoves the cursor forward to the next multiple of bit_boundary, clamped to the data.
isAlignedChecks if the cursor is at a multiple of bit_boundary.
isEndOfFileChecks if the cursor is at the end of the data.
openReplaces the data of the reader and moves the cursor to the start.
readReads bytes as a string, the cursor doesn't have to be aligned.
readBitReads one bit.
readBooleanReads one bit as a boolean, same as net.ReadBool.
readBytesReads bytes, the cursor doesn't have to be aligned.
readClassCreates a new instance of the class without calling init, and reads it with its deserialize( reader, ... ) method.
readCountedStringReads the string prefixed with its length, written by BinaryWriter:writeCountedString.
readDateReads date in DOS format (16 bits), see bytepack.readDate.
readDoubleReads signed 8-byte (64 bit) float, the cursor doesn't have to be aligned.
readFixedPointReads signed fixed-point number (Qm.n), see bytepack.readFixedPoint.
readFixedStringReads the string of a fixed size, same as read, but byte_count is required.
readFloatReads signed 4-byte (32 bit) float, the cursor doesn't have to be aligned.
readIntReads signed (two's complement) integer of any size, see pack.readInt.
readLineReads the line terminated by \n (a trailing \r is removed), or all whole bytes left if there is no \n.
readObjectReads the object in place with its deserialize( reader, ... ) method.
readStringReads C-style string, the bytes until the null terminator (\0), or all whole bytes left if there is no terminator, same…
readTerminatedStringReads bytes until the terminator byte, or all whole bytes left if there is no terminator.
readTimeReads time in DOS format (16 bits), see bytepack.readTime.
readUIntReads unsigned integer of any size, see pack.readUInt.
readUnsignedFixedPointReads unsigned fixed-point number (UQm.n), see bytepack.readFixedPoint.
remainingReturns the size of the data after the cursor.
seekMoves the cursor to the absolute position, clamped to the data.
sizeReturns the size of the data.
skipMoves the cursor relative to its position, clamped to the data.
tellReturns the cursor position.

#BinaryReader:align

sharedmenusource
function BinaryReader:align(bit_boundary?: integer): integer

Moves the cursor forward to the next multiple of bit_boundary, clamped to the data.

lua
reader:seek( 3 )
reader:align() --> 8
Parameters
NameTypeDescription
bit_boundaryoptionalintegerThe boundary in bits, 8 (byte) by default.
Returns
TypeNameDescription
integerpositionThe new position in bits.

#BinaryReader:isAligned

sharedmenunodiscardsource
function BinaryReader:isAligned(bit_boundary?: integer): boolean

Checks if the cursor is at a multiple of bit_boundary.

Parameters
NameTypeDescription
bit_boundaryoptionalintegerThe boundary in bits, 8 (byte) by default.
Returns
TypeNameDescription
booleanis_alignedtrue if the cursor is aligned.

#BinaryReader:isEndOfFile

sharedmenunodiscardsource
function BinaryReader:isEndOfFile(): boolean

Checks if the cursor is at the end of the data.

Returns
TypeNameDescription
booleanis_eoftrue if there is nothing left to read.

#BinaryReader:open

sharedmenusource
function BinaryReader:open(data: any)

Replaces the data of the reader and moves the cursor to the start.

Parameters
NameTypeDescription
dataanyThe data, converted to a string with tostring.

#BinaryReader:read

sharedmenunodiscardsource
function BinaryReader:read(byte_count?: integer): string

Reads bytes as a string, the cursor doesn't have to be aligned.

Parameters
NameTypeDescription
byte_countoptionalintegerThe number of bytes, all whole bytes left by default.
Returns
TypeNameDescription
stringdataThe bytes.

#BinaryReader:readBit

sharedmenunodiscardsource
function BinaryReader:readBit(): 0 | 1

Reads one bit.

Returns
TypeNameDescription
0 | 1valueThe bit.

#BinaryReader:readBoolean

sharedmenunodiscardsource
function BinaryReader:readBoolean(): boolean

Reads one bit as a boolean, same as net.ReadBool.

Returns
TypeNameDescription
booleanvaluetrue if the bit is 1.

#BinaryReader:readBytes

sharedmenunodiscardsource
function BinaryReader:readBytes(byte_count?: integer): integer

Reads bytes, the cursor doesn't have to be aligned.

Every byte is a separate return value, so use read for large amounts of data.

Parameters
NameTypeDescription
byte_countoptionalintegerThe number of bytes, all whole bytes left by default.
Returns
TypeNameDescription
integer...The bytes.

#BinaryReader:readClass

sharedmenunodiscardsource
function BinaryReader:readClass(cls: Object | Class | any, ...: any): any

Creates a new instance of the class without calling __init, and reads it with its __deserialize( reader, ... ) method.

lua
local vector = reader:readClass( Vector2 )
Parameters
NameTypeDescription
clsObject | Class | anyThe class to create.
...anyThe arguments passed to __deserialize.
Returns
TypeNameDescription
anyobjThe new object.

#BinaryReader:readCountedString

sharedmenunodiscardsource
function BinaryReader:readCountedString(bit_count: integer, is_little_endian?: boolean): string, integer

Reads the string prefixed with its length, written by BinaryWriter:writeCountedString.

Throws EndOfFileError if the string is cut, the cursor stays after the length in that case.

Parameters
NameTypeDescription
bit_countintegerThe size of the length in bits (1 - 64).
is_little_endianoptionalbooleantrue if the length is little endian, false by default.
Returns
TypeNameDescription
stringdataThe string.
integerdata_lengthThe size of the string in bytes.

#BinaryReader:readDate

sharedmenunodiscardsource
function BinaryReader:readDate(is_little_endian?: boolean): integer, integer, integer

Reads date in DOS format (16 bits), see bytepack.readDate.

DOS date layout
BitsFieldRange
15 - 9year - 19800 - 127 (1980 - 2107)
8 - 5month1 - 12
4 - 0day1 - 31
Parameters
NameTypeDescription
is_little_endianoptionalbooleantrue for little endian, false by default.
Returns
TypeNameDescription
integerdayThe day.
integermonthThe month.
integeryearThe year.

#BinaryReader:readDouble

sharedmenunodiscardsource
function BinaryReader:readDouble(is_little_endian?: boolean): number

Reads signed 8-byte (64 bit) float, the cursor doesn't have to be aligned.

IEEE 754 layout
BitsFieldNotes
63sign1 is negative
62 - 52exponent (11 bits)biased by 1023, 2047 is inf / nan
51 - 0mantissa (52 bits)~15-17 significant decimal digits

Range: 2.2250738585072014e-308 - 1.7976931348623157e308 (normal), Lua numbers are doubles, so nothing is lost.

Parameters
NameTypeDescription
is_little_endianoptionalbooleantrue for little endian (as net.ReadDouble), false by default.
Returns
TypeNameDescription
numbervalueThe double.

#BinaryReader:readFixedPoint

sharedmenunodiscardsource
function BinaryReader:readFixedPoint(m: integer, n: integer, is_little_endian?: boolean): number

Reads signed fixed-point number (Qm.n), see bytepack.readFixedPoint.

Commonly Used Qm.n Formats (signed)
FormatRangePrecision (Step)
Q8.8-128.0 - 127.9960.00390625 (1/256)
Q10.6-512.0 - 511.9843750.015625 (1/64)
Q12.4-2048.0 - 2047.93750.0625 (1/16)
Q16.16-32768.0 - 32767.999980.0000152588 (1/65536)
Q24.8-8388608.0 - 8388607.9960.00390625 (1/256)
Q32.16-2147483648.0 - 2147483647.999980.0000152588 (1/65536)

m + n is the size of the field in bits (1 - 64), so the range is limited to 53 bits.

Parameters
NameTypeDescription
mintegerThe number of integer bits, including the sign bit.
nintegerThe number of fractional bits.
is_little_endianoptionalbooleantrue for little endian, false by default.
Returns
TypeNameDescription
numbervalueThe signed fixed-point number.

#BinaryReader:readFixedString

sharedmenunodiscardsource
function BinaryReader:readFixedString(byte_count: integer): string

Reads the string of a fixed size, same as read, but byte_count is required.

Parameters
NameTypeDescription
byte_countintegerThe size of the string in bytes.
Returns
TypeNameDescription
stringdataThe string.

#BinaryReader:readFloat

sharedmenunodiscardsource
function BinaryReader:readFloat(is_little_endian?: boolean): number

Reads signed 4-byte (32 bit) float, the cursor doesn't have to be aligned.

IEEE 754 layout
BitsFieldNotes
31sign1 is negative
30 - 23exponent (8 bits)biased by 127, 255 is inf / nan
22 - 0mantissa (23 bits)~6-9 significant decimal digits

Range: 1.1754944e-38 - 3.4028235e38 (normal), integers are exact up to 16777216.

Parameters
NameTypeDescription
is_little_endianoptionalbooleantrue for little endian (as net.ReadFloat), false by default.
Returns
TypeNameDescription
numbervalueThe float.

#BinaryReader:readInt

sharedmenunodiscardsource
function BinaryReader:readInt(bit_count: integer, is_little_endian?: boolean): integer

Reads signed (two's complement) integer of any size, see pack.readInt.

Signed Integers limits for bit count
Bit countMin Value (-2^(n-1))Max Value (2^(n-1) - 1)
1-10
2-21
3-43
4-87
5-1615
6-3231
7-6463
8-128127
9-256255
10-512511
11-1,0241,023
12-2,0482,047
13-4,0964,095
14-8,1928,191
15-16,38416,383
16-32,76832,767
17-65,53665,535
18-131,072131,071
19-262,144262,143
20-524,288524,287
21-1,048,5761,048,575
22-2,097,1522,097,151
23-4,194,3044,194,303
24-8,388,6088,388,607
25-16,777,21616,777,215
26-33,554,43233,554,431
27-67,108,86467,108,863
28-134,217,728134,217,727
29-268,435,456268,435,455
30-536,870,912536,870,911
31-1,073,741,8241,073,741,823
32-2,147,483,6482,147,483,647
40-549,755,813,888549,755,813,887
48-140,737,488,355,328140,737,488,355,327
64-9,223,372,036,854,775,8089,223,372,036,854,775,807

Valid values without loss of precision: -9007199254740991 - 9007199254740991 (53 bits), all values above will have problems when working with them.

Parameters
NameTypeDescription
bit_countintegerThe size of the integer in bits (1 - 64), 0 reads nothing and returns 0.
is_little_endianoptionalbooleantrue for little endian, false by default.
Returns
TypeNameDescription
integervalueThe signed integer.

#BinaryReader:readLine

sharedmenunodiscardsource
function BinaryReader:readLine(): string

Reads the line terminated by \n (a trailing \r is removed), or all whole bytes left if there is no \n.

Returns
TypeNameDescription
stringlineThe line without \r\n or \n.

#BinaryReader:readObject

sharedmenusource
function BinaryReader:readObject(obj: T, ...: any): T

Reads the object in place with its __deserialize( reader, ... ) method.

Parameters
NameTypeDescription
objTThe object to read into.
...anyThe arguments passed to __deserialize.
Returns
TypeNameDescription
TobjThe same object.

#BinaryReader:readString

sharedmenunodiscardsource
function BinaryReader:readString(): string

Reads C-style string, the bytes until the null terminator (\0), or all whole bytes left if there is no terminator, same as net.ReadString.

Returns
TypeNameDescription
stringdataThe bytes before the null terminator.

#BinaryReader:readTerminatedString

sharedmenunodiscardsource
function BinaryReader:readTerminatedString(terminator: integer): string, boolean

Reads bytes until the terminator byte, or all whole bytes left if there is no terminator.

The terminator is read too, but isn't a part of the string.

lua
local reader = BinaryReader( "key=value" )
reader:readTerminatedString( 0x3D ) --> "key", true
reader:readTerminatedString( 0x3D ) --> "value", false
Parameters
NameTypeDescription
terminatorintegerThe terminator byte (0 - 255).
Returns
TypeNameDescription
stringdataThe bytes before the terminator.
booleanterminatedtrue if the terminator was found.

#BinaryReader:readTime

sharedmenunodiscardsource
function BinaryReader:readTime(is_little_endian?: boolean): integer, integer, integer

Reads time in DOS format (16 bits), see bytepack.readTime.

DOS time layout
BitsFieldRange
15 - 11hours0 - 23
10 - 5minutes0 - 59
4 - 0seconds / 20 - 29 (0 - 58 seconds)
Parameters
NameTypeDescription
is_little_endianoptionalbooleantrue for little endian, false by default.
Returns
TypeNameDescription
integerhoursThe number of hours.
integerminutesThe number of minutes.
integersecondsThe number of seconds, always even.

#BinaryReader:readUInt

sharedmenunodiscardsource
function BinaryReader:readUInt(bit_count: integer, is_little_endian?: boolean): integer

Reads unsigned integer of any size, see pack.readUInt.

Unsigned Integers limits for bit count
Bit countMax Value (2^n - 1)
11
23
37
415
531
663
7127
8255
9511
101,023
112,047
124,095
138,191
1416,383
1532,767
1665,535
17131,071
18262,143
19524,287
201,048,575
212,097,151
224,194,303
238,388,607
2416,777,215
2533,554,431
2667,108,863
27134,217,727
28268,435,455
29536,870,911
301,073,741,823
312,147,483,647
324,294,967,295
401,099,511,627,775
48281,474,976,710,655
6418,446,744,073,709,551,615

Valid values without loss of precision: 0 - 9007199254740991 (53 bits), all values above will have problems when working with them.

lua
reader:readUInt( 8 ) -- byte
reader:readUInt( 32, true ) -- same as `net.ReadUInt( 32 )`
Parameters
NameTypeDescription
bit_countintegerThe size of the integer in bits (1 - 64), 0 reads nothing and returns 0.
is_little_endianoptionalbooleantrue for little endian, false by default.
Returns
TypeNameDescription
integervalueThe unsigned integer.

#BinaryReader:readUnsignedFixedPoint

sharedmenunodiscardsource
function BinaryReader:readUnsignedFixedPoint(m: integer, n: integer, is_little_endian?: boolean): number

Reads unsigned fixed-point number (UQm.n), see bytepack.readFixedPoint.

Commonly Used UQm.n Formats (unsigned)
FormatRangePrecision (Step)
UQ8.80 - 255.9960.00390625 (1/256)
UQ10.60 - 1023.9843750.015625 (1/64)
UQ12.40 - 4095.93750.0625 (1/16)
UQ16.160 - 65535.999980.0000152588 (1/65536)
UQ24.80 - 16777215.9960.00390625 (1/256)
UQ32.160 - 4294967295.999980.0000152588 (1/65536)

m + n is the size of the field in bits (1 - 64), so the range is limited to 53 bits.

lua
reader:readUnsignedFixedPoint( 8, 8 ) -- UQ8.8, 16 bits
Parameters
NameTypeDescription
mintegerThe number of integer bits.
nintegerThe number of fractional bits.
is_little_endianoptionalbooleantrue for little endian, false by default.
Returns
TypeNameDescription
numbervalueThe unsigned fixed-point number.

#BinaryReader:remaining

sharedmenunodiscardsource
function BinaryReader:remaining(as_bytes?: boolean): integer

Returns the size of the data after the cursor.

Parameters
NameTypeDescription
as_bytesoptionalbooleantrue to return the number of whole bytes left, false (bits) by default.
Returns
TypeNameDescription
integerremainingThe size of the data left to read.

#BinaryReader:seek

sharedmenusource
function BinaryReader:seek(position?: integer, as_bytes?: boolean): integer

Moves the cursor to the absolute position, clamped to the data.

lua
reader:seek() --> 0, the start
reader:seek( 4, true ) --> 32, the 5th byte
reader:seek( -1, true ) --> the last byte
Parameters
NameTypeDescription
positionoptionalintegerThe new position, negative positions count back from the end, 0 by default.
as_bytesoptionalbooleantrue if position is in bytes, false (bits) by default.
Returns
TypeNameDescription
integerpositionThe new position in bits.

#BinaryReader:size

sharedmenunodiscardsource
function BinaryReader:size(as_bytes?: boolean): integer

Returns the size of the data.

Parameters
NameTypeDescription
as_bytesoptionalbooleantrue to return the size in bytes, false (bits) by default.
Returns
TypeNameDescription
integersizeThe size of the data.

#BinaryReader:skip

sharedmenusource
function BinaryReader:skip(length?: integer, as_bytes?: boolean): integer

Moves the cursor relative to its position, clamped to the data.

Parameters
NameTypeDescription
lengthoptionalintegerThe distance to move, negative to move back, 1 by default.
as_bytesoptionalbooleantrue if length is in bytes, false (bits) by default.
Returns
TypeNameDescription
integerpositionThe new position in bits.

#BinaryReader:tell

sharedmenunodiscardsource
function BinaryReader:tell(as_bytes?: boolean): integer

Returns the cursor position.

Parameters
NameTypeDescription
as_bytesoptionalbooleantrue to return the number of whole bytes before the cursor, false (bits) by default.
Returns
TypeNameDescription
integerpositionThe cursor position.
Generated from the dreamwork meta branch.