Skip to content

Documentation / @sw-file-lib/core / BinaryReader

Class: BinaryReader ​

ts
class BinaryReader {
  constructor(input: BinaryReaderInput);
  readU8(): number;
  readU16(littleEndian: boolean): number;
  readU32(littleEndian: boolean): number;
  readF32(littleEndian: boolean): number;
  readBytes(length: number): Uint8Array;
  readBytesUntilZero(): Uint8Array;
  readAscii(length?: number): string;
  readUtf8(length?: number): string;
}

Defined in: binary/BinaryReader.ts:33

Sequential reader for binary data.

Supports reading primitive values, byte arrays, and ASCII/UTF-8 strings from an in-memory buffer. The read position advances automatically after each operation.

Constructors ​

Constructor ​

ts
new BinaryReader(input: BinaryReaderInput): BinaryReader;

Defined in: binary/BinaryReader.ts:42

Creates a reader from binary data.

Parameters ​

ParameterTypeDescription
inputBinaryReaderInputSource buffer to read from.

Returns ​

BinaryReader

Methods ​

readU8() ​

ts
readU8(): number;

Defined in: binary/BinaryReader.ts:55

Reads an unsigned 8-bit integer.

Returns ​

number

The next byte as a number in the range 0–255.

Throws ​

If there is not enough data remaining to read the value.


readU16() ​

ts
readU16(littleEndian?: boolean): number;

Defined in: binary/BinaryReader.ts:69

Reads an unsigned 16-bit integer.

Parameters ​

ParameterTypeDefault valueDescription
littleEndianbooleantrueWhether the value is encoded in little-endian format. Defaults to true.

Returns ​

number

The decoded integer.

Throws ​

If there is not enough data remaining to read the value.


readU32() ​

ts
readU32(littleEndian?: boolean): number;

Defined in: binary/BinaryReader.ts:83

Reads an unsigned 32-bit integer.

Parameters ​

ParameterTypeDefault valueDescription
littleEndianbooleantrueWhether the value is encoded in little-endian format. Defaults to true.

Returns ​

number

The decoded integer.

Throws ​

If there is not enough data remaining to read the value.


readF32() ​

ts
readF32(littleEndian?: boolean): number;

Defined in: binary/BinaryReader.ts:97

Reads a 32-bit IEEE 754 floating-point value.

Parameters ​

ParameterTypeDefault valueDescription
littleEndianbooleantrueWhether the value is encoded in little-endian format. Defaults to true.

Returns ​

number

The decoded floating-point number.

Throws ​

If there is not enough data remaining to read the value.


readBytes() ​

ts
readBytes(length: number): Uint8Array;

Defined in: binary/BinaryReader.ts:110

Reads a fixed number of bytes.

Parameters ​

ParameterTypeDescription
lengthnumberNumber of bytes to read.

Returns ​

Uint8Array

A new Uint8Array containing the copied bytes.

Throws ​

If there is not enough data remaining to read the value.


readBytesUntilZero() ​

ts
readBytesUntilZero(): Uint8Array;

Defined in: binary/BinaryReader.ts:127

Reads bytes until a null terminator (0x00) is encountered.

The returned array does not include the terminating null byte. The reader position is advanced past the terminator.

Returns ​

Uint8Array

The bytes before the null terminator.

Throws ​

If there is no null terminator until the end of the buffer.


readAscii() ​

ts
readAscii(length?: number): string;

Defined in: binary/BinaryReader.ts:147

Reads a string by converting each byte with String.fromCharCode().

If length is omitted, bytes are read until a null terminator (0x00) is encountered. Otherwise, exactly length bytes are read.

Parameters ​

ParameterTypeDescription
length?numberFixed byte length to read.

Returns ​

string

The decoded ASCII string.

Throws ​

If the requested bytes exceed the remaining input, or if a null terminator is not found when length is omitted.


readUtf8() ​

ts
readUtf8(length?: number): string;

Defined in: binary/BinaryReader.ts:173

Reads a UTF-8 string.

If length is omitted, bytes are read until a null terminator (0x00) is encountered. Otherwise, exactly length bytes are read.

Parameters ​

ParameterTypeDescription
length?numberFixed byte length to read.

Returns ​

string

The decoded UTF-8 string.

Throws ​

If the requested bytes exceed the remaining input, or if a null terminator is not found when length is omitted.