Skip to content

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

Class: BinaryWriter ​

ts
class BinaryWriter {
  constructor(size: number);
  getPos(): number;
  setPos(value: number): void;
  withSize(sizeBytes: 1 | 2 | 4, writeContent: (writer: BinaryWriter) => void): void;
  writeU8(v: number): void;
  writeU16(v: number, littleEndian: boolean): void;
  writeU32(v: number, littleEndian: boolean): void;
  writeF32(v: number, littleEndian: boolean): void;
  writeAscii(text: string, endNull?: boolean): void;
  writeUtf8(text: string, endNull?: boolean): void;
  writeBytes(data: Uint8Array): void;
  toUint8Array(): Uint8Array<ArrayBuffer>;
}

Defined in: binary/BinaryWriter.ts:7

Sequential writer for binary data.

The internal buffer grows automatically as needed. Values are written at the current position, which advances after each operation.

Constructors ​

Constructor ​

ts
new BinaryWriter(size?: number): BinaryWriter;

Defined in: binary/BinaryWriter.ts:19

Creates a binary writer.

Parameters ​

ParameterTypeDefault valueDescription
sizenumber1024Initial buffer size in bytes. The buffer expands automatically if additional capacity is required. Defaults to 1024.

Returns ​

BinaryWriter

Methods ​

getPos() ​

ts
getPos(): number;

Defined in: binary/BinaryWriter.ts:30

Returns the current write position in bytes.

Returns ​

number

The current byte offset.


setPos() ​

ts
setPos(value: number): void;

Defined in: binary/BinaryWriter.ts:42

Sets the current write position.

If the position is moved beyond the end of the written data, the gap is filled with 0x00 when subsequent writes occur.

Parameters ​

ParameterTypeDescription
valuenumberNew byte offset.

Returns ​

void


withSize() ​

ts
withSize(sizeBytes: 1 | 2 | 4, writeContent: (writer: BinaryWriter) => void): void;

Defined in: binary/BinaryWriter.ts:62

Writes a length-prefixed block.

The byte length of the data written inside writeContent is calculated automatically and written immediately before the block using an unsigned integer of the specified size.

For example, sizeBytes = 2 produces:

text
[length: u16][content...]

Parameters ​

ParameterTypeDescription
sizeBytes1 | 2 | 4Size of the length field in bytes (1, 2, or 4).
writeContent(writer: BinaryWriter) => voidCallback that writes the block contents.

Returns ​

void


writeU8() ​

ts
writeU8(v: number): void;

Defined in: binary/BinaryWriter.ts:108

Writes an unsigned 8-bit integer.

Parameters ​

ParameterTypeDescription
vnumberValue to write.

Returns ​

void


writeU16() ​

ts
writeU16(v: number, littleEndian?: boolean): void;

Defined in: binary/BinaryWriter.ts:121

Writes an unsigned 16-bit integer.

Parameters ​

ParameterTypeDefault valueDescription
vnumberundefinedValue to write.
littleEndianbooleantrueWhether to encode in little-endian format. Defaults to true.

Returns ​

void


writeU32() ​

ts
writeU32(v: number, littleEndian?: boolean): void;

Defined in: binary/BinaryWriter.ts:134

Writes an unsigned 32-bit integer.

Parameters ​

ParameterTypeDefault valueDescription
vnumberundefinedValue to write.
littleEndianbooleantrueWhether to encode in little-endian format. Defaults to true.

Returns ​

void


writeF32() ​

ts
writeF32(v: number, littleEndian?: boolean): void;

Defined in: binary/BinaryWriter.ts:147

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

Parameters ​

ParameterTypeDefault valueDescription
vnumberundefinedValue to write.
littleEndianbooleantrueWhether to encode in little-endian format. Defaults to true.

Returns ​

void


writeAscii() ​

ts
writeAscii(text: string, endNull?: boolean): void;

Defined in: binary/BinaryWriter.ts:160

Writes an ASCII string.

Parameters ​

ParameterTypeDescription
textstringText to encode.
endNull?booleanWhether to append a null terminator (0x00). Defaults to false.

Returns ​

void


writeUtf8() ​

ts
writeUtf8(text: string, endNull?: boolean): void;

Defined in: binary/BinaryWriter.ts:178

Writes a UTF-8 string.

Parameters ​

ParameterTypeDescription
textstringText to encode.
endNull?booleanWhether to append a null terminator (0x00). Defaults to false.

Returns ​

void


writeBytes() ​

ts
writeBytes(data: Uint8Array): void;

Defined in: binary/BinaryWriter.ts:192

Writes raw bytes.

Parameters ​

ParameterTypeDescription
dataUint8ArrayBytes to append.

Returns ​

void


toUint8Array() ​

ts
toUint8Array(): Uint8Array<ArrayBuffer>;

Defined in: binary/BinaryWriter.ts:206

Returns the written data as a compact byte array.

The returned array contains only the written region and does not include any unused capacity from the internal buffer.

Returns ​

Uint8Array<ArrayBuffer>

The serialized binary data.