Options
All
  • Public
  • Public/Protected
  • All
Menu

This DataStream is for internal use.

Hierarchy

  • DataStream

Index

Constructors

  • new DataStream(arrayBuffer: Uint8Array | ArrayBuffer | Int8Array | DataView, byteOffset: number, endianness: boolean): DataStream
  • DataStream reads scalars, arrays and structs of data from an ArrayBuffer. It's like a file-like DataView on steroids.

    Parameters

    • arrayBuffer: Uint8Array | ArrayBuffer | Int8Array | DataView

      ArrayBuffer to read from.

    • byteOffset: number

      Offset from arrayBuffer beginning for the DataStream.

    • endianness: boolean

    Returns DataStream

Properties

failurePosition: number = 0

Seek position where readStruct ran into a problem. Useful for debugging struct parsing.

BIG_ENDIAN: boolean = false

Big-endian const to use as default endianness.

LITTLE_ENDIAN: boolean = true

Little-endian const to use as default endianness.

endianness: boolean = ...

Native endianness. Either DataStream.BIG_ENDIAN or DataStream.LITTLE_ENDIAN depending on the platform endianness.

Accessors

  • get buffer(): ArrayBuffer
  • set buffer(v: ArrayBuffer): void
  • Set/get the backing ArrayBuffer of the DataStream object. The setter updates the DataView to point to the new buffer.

    Returns ArrayBuffer

  • Set/get the backing ArrayBuffer of the DataStream object. The setter updates the DataView to point to the new buffer.

    Parameters

    • v: ArrayBuffer

    Returns void

  • get byteLength(): number
  • Returns the byte length of the DataStream object.

    Returns number

  • get byteOffset(): number
  • set byteOffset(v: number): void
  • Set/get the byteOffset of the DataStream object. The setter updates the DataView to point to the new byteOffset.

    Returns number

  • Set/get the byteOffset of the DataStream object. The setter updates the DataView to point to the new byteOffset.

    Parameters

    • v: number

    Returns void

  • get dataView(): DataView
  • set dataView(v: DataView): void
  • Set/get the backing DataView of the DataStream object. The setter updates the buffer and byteOffset to point to the DataView values.

    Returns DataView

  • Set/get the backing DataView of the DataStream object. The setter updates the buffer and byteOffset to point to the DataView values.

    Parameters

    • v: DataView

    Returns void

  • get dynamicSize(): boolean
  • set dynamicSize(v: boolean): void
  • Whether to extend DataStream buffer when trying to write beyond its size. If set, the buffer is reallocated to twice its current size until the requested write fits the buffer.

    Returns boolean

  • Whether to extend DataStream buffer when trying to write beyond its size. If set, the buffer is reallocated to twice its current size until the requested write fits the buffer.

    Parameters

    • v: boolean

    Returns void

Methods

  • isEof(): boolean
  • Returns true if the DataStream seek pointer is at the end of buffer and there's no more data to read.

    Returns boolean

    True if the seek pointer is at the end of the buffer.

  • mapFloat32Array(length: number, e?: boolean): Float32Array
  • Maps a Float32Array into the DataStream buffer, swizzling it to native endianness in-place. The current offset from the start of the buffer needs to be a multiple of element size, just like with typed array views.

    Nice for quickly reading in data. Warning: potentially modifies the buffer contents.

    Parameters

    • length: number

      Number of elements to map.

    • Optional e: boolean

      Endianness of the data to read.

    Returns Float32Array

    Float32Array to the DataStream backing buffer.

  • mapFloat64Array(length: number, e?: boolean): Float64Array
  • Maps a Float64Array into the DataStream buffer, swizzling it to native endianness in-place. The current offset from the start of the buffer needs to be a multiple of element size, just like with typed array views.

    Nice for quickly reading in data. Warning: potentially modifies the buffer contents.

    Parameters

    • length: number

      Number of elements to map.

    • Optional e: boolean

      Endianness of the data to read.

    Returns Float64Array

    Float64Array to the DataStream backing buffer.

  • mapInt16Array(length: number, e?: boolean): Int16Array
  • Maps an Int16Array into the DataStream buffer, swizzling it to native endianness in-place. The current offset from the start of the buffer needs to be a multiple of element size, just like with typed array views.

    Nice for quickly reading in data. Warning: potentially modifies the buffer contents.

    Parameters

    • length: number

      Number of elements to map.

    • Optional e: boolean

      Endianness of the data to read.

    Returns Int16Array

    Int16Array to the DataStream backing buffer.

  • mapInt32Array(length: number, e?: boolean): Int32Array
  • Maps an Int32Array into the DataStream buffer, swizzling it to native endianness in-place. The current offset from the start of the buffer needs to be a multiple of element size, just like with typed array views.

    Nice for quickly reading in data. Warning: potentially modifies the buffer contents.

    Parameters

    • length: number

      Number of elements to map.

    • Optional e: boolean

      Endianness of the data to read.

    Returns Int32Array

    Int32Array to the DataStream backing buffer.

  • mapInt8Array(length: number): Int8Array
  • Maps an Int8Array into the DataStream buffer.

    Nice for quickly reading in data.

    Parameters

    • length: number

      Number of elements to map.

    Returns Int8Array

    Int8Array to the DataStream backing buffer.

  • mapUint16Array(length: number, e?: boolean): Uint16Array
  • Maps a Uint16Array into the DataStream buffer, swizzling it to native endianness in-place. The current offset from the start of the buffer needs to be a multiple of element size, just like with typed array views.

    Nice for quickly reading in data. Warning: potentially modifies the buffer contents.

    Parameters

    • length: number

      Number of elements to map.

    • Optional e: boolean

      Endianness of the data to read.

    Returns Uint16Array

    Uint16Array to the DataStream backing buffer.

  • mapUint32Array(length: number, e?: boolean): Uint32Array
  • Maps a Uint32Array into the DataStream buffer, swizzling it to native endianness in-place. The current offset from the start of the buffer needs to be a multiple of element size, just like with typed array views.

    Nice for quickly reading in data. Warning: potentially modifies the buffer contents.

    Parameters

    • length: number

      Number of elements to map.

    • Optional e: boolean

      Endianness of the data to read.

    Returns Uint32Array

    Uint32Array to the DataStream backing buffer.

  • mapUint8Array(length: number): Uint8Array
  • Maps a Uint8Array into the DataStream buffer.

    Nice for quickly reading in data.

    Parameters

    • length: number

      Number of elements to map.

    Returns Uint8Array

    Uint8Array to the DataStream backing buffer.

  • readByte(offset: number): number
  • Reads an 8-bit int from the DataStream with the offset.

    Parameters

    • offset: number

      The offset.

    Returns number

    The read number.

  • readCString(length?: number): string
  • Read null-terminated string of desired length from the DataStream. Truncates the returned string so that the null byte is not a part of it.

    Parameters

    • Optional length: number

      The length of the string to read.

    Returns string

    The read string.

  • readFloat32(e?: boolean): number
  • Reads a 32-bit float from the DataStream with the desired endianness.

    Parameters

    • Optional e: boolean

      Endianness of the number.

    Returns number

    The read number.

  • readFloat32Array(length: number, e?: boolean): Float32Array
  • Reads a Float32Array of desired length and endianness from the DataStream.

    Parameters

    • length: number

      Number of elements to map.

    • Optional e: boolean

      Endianness of the data to read.

    Returns Float32Array

    The read Float32Array.

  • readFloat64(e?: boolean): number
  • Reads a 64-bit float from the DataStream with the desired endianness.

    Parameters

    • Optional e: boolean

      Endianness of the number.

    Returns number

    The read number.

  • readFloat64Array(length: number, e?: boolean): Float64Array
  • Reads a Float64Array of desired length and endianness from the DataStream.

    Parameters

    • length: number

      Number of elements to map.

    • Optional e: boolean

      Endianness of the data to read.

    Returns Float64Array

    The read Float64Array.

  • readInt(offset: number): number
  • Reads a 32-bit int from the DataStream with the offset.

    Parameters

    • offset: number

      The offset.

    Returns number

    The read number.

  • readInt16(e?: boolean): number
  • Reads a 16-bit int from the DataStream with the desired endianness.

    Parameters

    • Optional e: boolean

      Endianness of the number.

    Returns number

    The read number.

  • readInt16Array(length: number, e?: boolean): Int16Array
  • Reads an Int16Array of desired length and endianness from the DataStream.

    Parameters

    • length: number

      Number of elements to map.

    • Optional e: boolean

      Endianness of the data to read.

    Returns Int16Array

    The read Int16Array.

  • readInt32(e?: boolean): number
  • Reads a 32-bit int from the DataStream with the desired endianness.

    Parameters

    • Optional e: boolean

      Endianness of the number.

    Returns number

    The read number.

  • readInt32Array(length: number, e?: boolean): Int32Array
  • Reads an Int32Array of desired length and endianness from the DataStream.

    Parameters

    • length: number

      Number of elements to map.

    • Optional e: boolean

      Endianness of the data to read.

    Returns Int32Array

    The read Int32Array.

  • readInt8(): number
  • Reads an 8-bit int from the DataStream.

    Returns number

    The read number.

  • readInt8Array(length: number): Int8Array
  • Reads an Int8Array of desired length from the DataStream.

    Parameters

    • length: number

      Number of elements to map.

    Returns Int8Array

    The read Int8Array.

  • readShort(offset: number): number
  • Reads a 16-bit int from the DataStream with the offset

    Parameters

    • offset: number

      The offset.

    Returns number

    The read number.

  • readString(length?: number, encoding?: string): any
  • Read a string of desired length and encoding from the DataStream.

    Parameters

    • Optional length: number

      The length of the string to read in bytes.

    • Optional encoding: string

      The encoding of the string data in the DataStream. Defaults to ASCII.

    Returns any

    The read string.

  • readStringAt(offset: number, length: number): string
  • Read UCS-2 string of desired length and offset from the DataStream.

    Parameters

    • offset: number

      The offset.

    • length: number

      The length of the string to read.

    Returns string

    The read string.

  • readStruct(structDefinition: any): {}
  • Reads a struct of data from the DataStream. The struct is defined as a flat array of [name, type]-pairs. See the example below:

    ds.readStruct([ 'headerTag', 'uint32', // Uint32 in DataStream endianness. 'headerTag2', 'uint32be', // Big-endian Uint32. 'headerTag3', 'uint32le', // Little-endian Uint32. 'array', ['[]', 'uint32', 16], // Uint32Array of length 16. 'array2Length', 'uint32', 'array2', ['[]', 'uint32', 'array2Length'] // Uint32Array of length array2Length ]);

    The possible values for the type are as follows:

    // Number types

    // Unsuffixed number types use DataStream endianness. // To explicitly specify endianness, suffix the type with // 'le' for little-endian or 'be' for big-endian, // e.g. 'int32be' for big-endian int32.

    'uint8' -- 8-bit unsigned int 'uint16' -- 16-bit unsigned int 'uint32' -- 32-bit unsigned int 'int8' -- 8-bit int 'int16' -- 16-bit int 'int32' -- 32-bit int 'float32' -- 32-bit float 'float64' -- 64-bit float

    // String types 'cstring' -- ASCII string terminated by a zero byte. 'string:N' -- ASCII string of length N, where N is a literal integer. 'string:variableName' -- ASCII string of length $variableName, where 'variableName' is a previously parsed number in the current struct. 'string,CHARSET:N' -- String of byteLength N encoded with given CHARSET. 'u16string:N' -- UCS-2 string of length N in DataStream endianness. 'u16stringle:N' -- UCS-2 string of length N in little-endian. 'u16stringbe:N' -- UCS-2 string of length N in big-endian.

    // Complex types [name, type, name_2, type_2, ..., name_N, type_N] -- Struct function(dataStream, struct) {} -- Callback function to read and return data. {get: function(dataStream, struct) {}, set: function(dataStream, struct) {}} -- Getter/setter functions to read and return data, handy for using the same struct definition for reading and writing structs. ['[]', type, length] -- Array of given type and length. The length can be either a number, a string that references a previously-read field, or a callback function(struct, dataStream, type){}. If length is '*', reads in as many elements as it can.

    Parameters

    • structDefinition: any

      Struct definition object.

    Returns {}

    The read struct. Null if failed to read struct.

    • readType(t: any, struct: any): any
    • Reads an object of type t from the DataStream, passing struct as the thus-far read struct to possible callbacks that refer to it. Used by readStruct for reading in the values, so the type is one of the readStruct types.

      Parameters

      • t: any

        Type of the object to read.

      • struct: any

        Struct to refer to when resolving length references and for calling callbacks.

      Returns any

      Returns the object on successful read, null on unsuccessful.

    • readUCS2String(length: number, endianness?: boolean): string
    • Read UCS-2 string of desired length and endianness from the DataStream.

      Parameters

      • length: number

        The length of the string to read.

      • Optional endianness: boolean

        The endianness of the string data in the DataStream.

      Returns string

      The read string.

    • readUint16(e?: boolean): number
    • Reads a 16-bit unsigned int from the DataStream with the desired endianness.

      Parameters

      • Optional e: boolean

        Endianness of the number.

      Returns number

      The read number.

    • readUint16Array(length: number, e?: boolean): Uint16Array
    • Reads a Uint16Array of desired length and endianness from the DataStream.

      Parameters

      • length: number

        Number of elements to map.

      • Optional e: boolean

        Endianness of the data to read.

      Returns Uint16Array

      The read Uint16Array.

    • readUint32(e?: boolean): number
    • Reads a 32-bit unsigned int from the DataStream with the desired endianness.

      Parameters

      • Optional e: boolean

        Endianness of the number.

      Returns number

      The read number.

    • readUint32Array(length: number, e?: boolean): Uint32Array
    • Reads a Uint32Array of desired length and endianness from the DataStream.

      Parameters

      • length: number

        Number of elements to map.

      • Optional e: boolean

        Endianness of the data to read.

      Returns Uint32Array

      The read Uint32Array.

    • readUint8(): number
    • Reads an 8-bit unsigned int from the DataStream.

      Returns number

      The read number.

    • readUint8Array(length: number): Uint8Array
    • Reads a Uint8Array of desired length from the DataStream.

      Parameters

      • length: number

        Number of elements to map.

      Returns Uint8Array

      The read Uint8Array.

    • save(filename: any): void
    • Saves the DataStream contents to the given filename. Uses Chrome's anchor download property to initiate download.

      Parameters

      • filename: any

        Filename to save as.

      Returns void

    • seek(pos: number): void
    • Sets the DataStream read/write position to given position. Clamps between 0 and DataStream length.

      Parameters

      • pos: number

        Position to seek to.

      Returns void

    • writeCString(s: string, length?: number): void
    • Writes a null-terminated string to DataStream and zero-pads it to length bytes. If length is not given, writes the string followed by a zero. If string is longer than length, the written part of the string does not have a trailing zero.

      Parameters

      • s: string

        The string to write.

      • Optional length: number

        The number of characters to write.

      Returns void

    • writeFloat32(v: number, e?: boolean): void
    • Writes a 32-bit float to the DataStream with the desired endianness.

      Parameters

      • v: number

        Number to write.

      • Optional e: boolean

        Endianness of the number.

      Returns void

    • writeFloat32Array(arr: ArrayLike<number> | Float32Array, e?: boolean): void
    • Writes a Float32Array of specified endianness to the DataStream.

      Parameters

      • arr: ArrayLike<number> | Float32Array

        The array to write.

      • Optional e: boolean

        Endianness of the data to write.

      Returns void

    • writeFloat64(v: number, e?: boolean): void
    • Writes a 64-bit float to the DataStream with the desired endianness.

      Parameters

      • v: number

        Number to write.

      • Optional e: boolean

        Endianness of the number.

      Returns void

    • writeFloat64Array(arr: ArrayLike<number> | Float64Array, e?: boolean): void
    • Writes a Float64Array of specified endianness to the DataStream.

      Parameters

      • arr: ArrayLike<number> | Float64Array

        The array to write.

      • Optional e: boolean

        Endianness of the data to write.

      Returns void

    • writeInt16(v: number, e?: boolean): void
    • Writes a 16-bit int to the DataStream with the desired endianness.

      Parameters

      • v: number

        Number to write.

      • Optional e: boolean

        Endianness of the number.

      Returns void

    • writeInt16Array(arr: ArrayLike<number> | Int16Array, e?: boolean): void
    • Writes an Int16Array of specified endianness to the DataStream.

      Parameters

      • arr: ArrayLike<number> | Int16Array

        The array to write.

      • Optional e: boolean

        Endianness of the data to write.

      Returns void

    • writeInt32(v: number, e?: boolean): void
    • Writes a 32-bit int to the DataStream with the desired endianness.

      Parameters

      • v: number

        Number to write.

      • Optional e: boolean

        Endianness of the number.

      Returns void

    • writeInt32Array(arr: ArrayLike<number> | Int32Array, e?: boolean): void
    • Writes an Int32Array of specified endianness to the DataStream.

      Parameters

      • arr: ArrayLike<number> | Int32Array

        The array to write.

      • Optional e: boolean

        Endianness of the data to write.

      Returns void

    • writeInt8(v: number): void
    • Writes an 8-bit int to the DataStream.

      Parameters

      • v: number

        Number to write.

      Returns void

    • writeInt8Array(arr: ArrayLike<number> | Int8Array): void
    • Writes an Int8Array to the DataStream.

      Parameters

      • arr: ArrayLike<number> | Int8Array

        The array to write.

      Returns void

    • writeString(s: string, encoding?: string, length?: number): void
    • Writes a string of desired length and encoding to the DataStream.

      Parameters

      • s: string

        The string to write.

      • Optional encoding: string

        The encoding for the written string data. Defaults to ASCII.

      • Optional length: number

        The number of characters to write.

      Returns void

    • writeStruct(structDefinition: any, struct: any): void
    • Writes a struct to the DataStream. Takes a structDefinition that gives the types and a struct object that gives the values. Refer to readStruct for the structure of structDefinition.

      Parameters

      • structDefinition: any

        Type definition of the struct.

      • struct: any

        The struct data object.

      Returns void

    • writeType(t: any, v: any, struct: any): any
    • Writes object v of type t to the DataStream.

      Parameters

      • t: any

        Type of data to write.

      • v: any

        Value of data to write.

      • struct: any

        Struct to pass to write callback functions.

      Returns any

    • writeUCS2String(str: string, endianness?: boolean, lengthOverride?: number): void
    • Write a UCS-2 string of desired endianness to the DataStream. The lengthOverride argument lets you define the number of characters to write. If the string is shorter than lengthOverride, the extra space is padded with zeroes.

      Parameters

      • str: string

        The string to write.

      • Optional endianness: boolean

        The endianness to use for the written string data.

      • Optional lengthOverride: number

        The number of characters to write.

      Returns void

    • writeUint16(v: number, e?: boolean): void
    • Writes a 16-bit unsigned int to the DataStream with the desired endianness.

      Parameters

      • v: number

        Number to write.

      • Optional e: boolean

        Endianness of the number.

      Returns void

    • writeUint16Array(arr: ArrayLike<number> | Uint16Array, e?: boolean): void
    • Writes a Uint16Array of specified endianness to the DataStream.

      Parameters

      • arr: ArrayLike<number> | Uint16Array

        The array to write.

      • Optional e: boolean

        Endianness of the data to write.

      Returns void

    • writeUint32(v: number, e?: boolean): void
    • Writes a 32-bit unsigned int to the DataStream with the desired endianness.

      Parameters

      • v: number

        Number to write.

      • Optional e: boolean

        Endianness of the number.

      Returns void

    • writeUint32Array(arr: ArrayLike<number> | Uint32Array, e?: boolean): void
    • Writes a Uint32Array of specified endianness to the DataStream.

      Parameters

      • arr: ArrayLike<number> | Uint32Array

        The array to write.

      • Optional e: boolean

        Endianness of the data to write.

      Returns void

    • writeUint8(v: number): void
    • Writes an 8-bit unsigned int to the DataStream.

      Parameters

      • v: number

        Number to write.

      Returns void

    • writeUint8Array(arr: Uint8Array | ArrayLike<number>): void
    • Writes a Uint8Array to the DataStream.

      Parameters

      • arr: Uint8Array | ArrayLike<number>

        The array to write.

      Returns void

    Generated using TypeDoc