NGL@1.0.0-beta.7 Home Manual Reference Source Gallery
import IOBuffer from 'ngl/src/utils/io-buffer.js'
public class | source

IOBuffer

Class for writing and reading binary data

Constructor Summary

Public Constructor
public

Member Summary

Public Members
public

Reference to the internal ArrayBuffer object

public

Byte length of the internal ArrayBuffer

public

Byte offset of the internal ArrayBuffer

public

Byte length of the internal ArrayBuffer

public
public

The current offset of the buffer's pointer

Private Members
private
private
private
private

Method Summary

Public Methods
public

available(byteLength: number): boolean

Checks if the memory allocated to the buffer is sufficient to store more bytes after the offset

public

Make sure the buffer has sufficient memory to write a given byteLength at the current pointer offset If the buffer's memory is insufficient, this method will create a new buffer (a copy) with a length that is twice (byteLength + current offset)

public

Same as IOBuffer#toArray but returns a Buffer if possible.

public

Check if big-endian mode is used for reading and writing multi-byte values

public

Check if little-endian mode is used for reading and writing multi-byte values

public

Store the current pointer offset.

public

Pop the last pointer offset from the mark stack, and set the current pointer offset to the popped value

public

Push the current pointer offset to the mark stack

public

Read a byte and return false if the byte's value is 0, or true otherwise Moves pointer forward

public
public

Read n bytes and move pointer forward.

public

Read 1-byte ascii character and move pointer forward

public

Read n 1-byte ascii characters and move pointer forward

public

Read a 32-bit floating number and move pointer forward

public

Read a 64-bit floating number and move pointer forward

public

Read a 16-bit signed integer and move pointer forward

public

Read a 32-bit signed integer and move pointer forward

public

Read a signed 8-bit integer and move pointer forward

public

Read a 16-bit unsigned integer and move pointer forward

public

Read a 32-bit unsigned integer and move pointer forward

public

Read an unsigned 8-bit integer and move pointer forward

public

Move the pointer back to the last pointer offset set by mark

public

Move the pointer offset back to 0

public

seek(offset: number): IOBuffer

Move the pointer to the given offset

public

Switches to big-endian mode for reading and writing multi-byte values

public

Set little-endian mode for reading and writing multi-byte values

public

Move the pointer n bytes forward

public

Export a Uint8Array view of the internal buffer.

public

writeBoolean(value: any): IOBuffer

Write 0xff if the passed value is truthy, 0x00 otherwise

public

An alias for IOBuffer#writeUint8

public

Write bytes

public

Write the charCode of the passed string's first character to the current pointer offset

public

Write the charCodes of the passed string's characters to the current pointer offset

public

Write a 32-bit floating number at the current pointer offset

public

Write a 64-bit floating number at the current pointer offset

public

Write value as an 16-bit signed integer

public

Write a 32-bit signed integer at the current pointer offset

public

Write value as an 8-bit signed integer

public

Write value as a 16-bit unsigned integer

public

Write a 32-bit unsigned integer at the current pointer offset

public

Write value as a 8-bit unsigned integer

Private Methods
private

Update the last written byte offset

Public Constructors

public constructor(data: undefined | number | ArrayBuffer | TypedArray | IOBuffer | Buffer, params: object) source

Params:

NameTypeAttributeDescription
data undefined | number | ArrayBuffer | TypedArray | IOBuffer | Buffer

The data to construct the IOBuffer with.

If it's a number, it will initialize the buffer with the number as the buffer's length. If it's undefined, it will initialize the buffer with a default length of 8 Kb. If its an ArrayBuffer, a TypedArray, an IOBuffer instance, or a Node.js Buffer, it will create a view over the underlying ArrayBuffer.

params object
  • optional
params.offset number
  • optional
  • default: 0

Ignore the first n bytes of the ArrayBuffer

Public Members

public buffer: ArrayBuffer source

Reference to the internal ArrayBuffer object

public byteLength: Number source

Byte length of the internal ArrayBuffer

public byteOffset: Number source

Byte offset of the internal ArrayBuffer

public length: Number source

Byte length of the internal ArrayBuffer

public littleEndian source

public offset: Number source

The current offset of the buffer's pointer

Private Members

private _data source

private _lastWrittenByte source

private _mark source

private _marks source

Public Methods

public available(byteLength: number): boolean source

Checks if the memory allocated to the buffer is sufficient to store more bytes after the offset

Params:

NameTypeAttributeDescription
byteLength number
  • optional
  • default: 1

The needed memory in bytes

Return:

boolean

Returns true if there is sufficient space and false otherwise

public ensureAvailable(byteLength: number): IOBuffer source

Make sure the buffer has sufficient memory to write a given byteLength at the current pointer offset If the buffer's memory is insufficient, this method will create a new buffer (a copy) with a length that is twice (byteLength + current offset)

Params:

NameTypeAttributeDescription
byteLength number
  • optional
  • default: 1

Return:

IOBuffer

public getBuffer(): Buffer | Uint8Array source

Same as IOBuffer#toArray but returns a Buffer if possible. Otherwise returns a Uint8Array.

Return:

Buffer | Uint8Array

public isBigEndian(): boolean source

Check if big-endian mode is used for reading and writing multi-byte values

Return:

boolean

Returns true if big-endian mode is used, false otherwise

public isLittleEndian(): boolean source

Check if little-endian mode is used for reading and writing multi-byte values

Return:

boolean

Returns true if little-endian mode is used, false otherwise

public mark(): IOBuffer source

Store the current pointer offset.

Return:

IOBuffer

See:

public popMark(): IOBuffer source

Pop the last pointer offset from the mark stack, and set the current pointer offset to the popped value

Return:

IOBuffer

See:

public pushMark(): IOBuffer source

Push the current pointer offset to the mark stack

Return:

IOBuffer

See:

public readBoolean(): boolean source

Read a byte and return false if the byte's value is 0, or true otherwise Moves pointer forward

Return:

boolean

public readByte(): number source

Alias for IOBuffer#readUint8

Return:

number

public readBytes(n: number): Uint8Array source

Read n bytes and move pointer forward.

Params:

NameTypeAttributeDescription
n number

Return:

Uint8Array

public readChar(): string source

Read 1-byte ascii character and move pointer forward

Return:

string

public readChars(n: number): string source

Read n 1-byte ascii characters and move pointer forward

Params:

NameTypeAttributeDescription
n number

Return:

string

public readFloat32(): number source

Read a 32-bit floating number and move pointer forward

Return:

number

public readFloat64(): number source

Read a 64-bit floating number and move pointer forward

Return:

number

public readInt16(): number source

Read a 16-bit signed integer and move pointer forward

Return:

number

public readInt32(): number source

Read a 32-bit signed integer and move pointer forward

Return:

number

public readInt8(): number source

Read a signed 8-bit integer and move pointer forward

Return:

number

public readUint16(): number source

Read a 16-bit unsigned integer and move pointer forward

Return:

number

public readUint32(): number source

Read a 32-bit unsigned integer and move pointer forward

Return:

number

public readUint8(): number source

Read an unsigned 8-bit integer and move pointer forward

Return:

number

public reset(): IOBuffer source

Move the pointer back to the last pointer offset set by mark

Return:

IOBuffer

See:

public rewind(): IOBuffer source

Move the pointer offset back to 0

Return:

IOBuffer

public seek(offset: number): IOBuffer source

Move the pointer to the given offset

Params:

NameTypeAttributeDescription
offset number

Return:

IOBuffer

public setBigEndian(): IOBuffer source

Switches to big-endian mode for reading and writing multi-byte values

Return:

IOBuffer

public setLittleEndian(): IOBuffer source

Set little-endian mode for reading and writing multi-byte values

Return:

IOBuffer

public skip(n: number): IOBuffer source

Move the pointer n bytes forward

Params:

NameTypeAttributeDescription
n number

Return:

IOBuffer

public toArray(): Uint8Array source

Export a Uint8Array view of the internal buffer. The view starts at the byte offset and its length is calculated to stop at the last written byte or the original length.

Return:

Uint8Array

public writeBoolean(value: any): IOBuffer source

Write 0xff if the passed value is truthy, 0x00 otherwise

Params:

NameTypeAttributeDescription
value any

Return:

IOBuffer

public writeByte(value: number): IOBuffer source

An alias for IOBuffer#writeUint8

Params:

NameTypeAttributeDescription
value number

Return:

IOBuffer

public writeBytes(bytes: Array | Uint8Array): IOBuffer source

Write bytes

Params:

NameTypeAttributeDescription
bytes Array | Uint8Array

Return:

IOBuffer

public writeChar(str: string): IOBuffer source

Write the charCode of the passed string's first character to the current pointer offset

Params:

NameTypeAttributeDescription
str string

The character to set

Return:

IOBuffer

public writeChars(str: string): IOBuffer source

Write the charCodes of the passed string's characters to the current pointer offset

Params:

NameTypeAttributeDescription
str string

Return:

IOBuffer

public writeFloat32(value: number): IOBuffer source

Write a 32-bit floating number at the current pointer offset

Params:

NameTypeAttributeDescription
value number

The value to set

Return:

IOBuffer

public writeFloat64(value: number): IOBuffer source

Write a 64-bit floating number at the current pointer offset

Params:

NameTypeAttributeDescription
value number

Return:

IOBuffer

public writeInt16(value: number): IOBuffer source

Write value as an 16-bit signed integer

Params:

NameTypeAttributeDescription
value number

Return:

IOBuffer

public writeInt32(value: number): IOBuffer source

Write a 32-bit signed integer at the current pointer offset

Params:

NameTypeAttributeDescription
value number

Return:

IOBuffer

public writeInt8(value: number): IOBuffer source

Write value as an 8-bit signed integer

Params:

NameTypeAttributeDescription
value number

Return:

IOBuffer

public writeUint16(value: number): IOBuffer source

Write value as a 16-bit unsigned integer

Params:

NameTypeAttributeDescription
value number

Return:

IOBuffer

public writeUint32(value: number): IOBuffer source

Write a 32-bit unsigned integer at the current pointer offset

Params:

NameTypeAttributeDescription
value number

The value to set

Return:

IOBuffer

public writeUint8(value: number): IOBuffer source

Write value as a 8-bit unsigned integer

Params:

NameTypeAttributeDescription
value number

Return:

IOBuffer

Private Methods

private _updateLastWrittenByte() source

Update the last written byte offset