This DataStream is for internal use.

Constructors

  • DataStream reads scalars, arrays and structs of data from an ArrayBuffer. It's like a file-like DataView on steroids.

    Parameters

    • arrayBuffer: ArrayBuffer | DataView | Int8Array | Uint8Array

      ArrayBuffer to read from.

    • byteOffset: number

      Offset from arrayBuffer beginning for the DataStream.

    • endianness: boolean

    Returns default

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.

endianness: boolean = ...

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

LITTLE_ENDIAN: boolean = true

Little-endian const to use as default endianness.

Accessors

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

    Returns ArrayBuffer

  • set buffer(v: ArrayBuffer): void
  • Parameters

    • v: ArrayBuffer

    Returns void

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

    Returns number

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

    Returns number

  • set byteOffset(v: number): void
  • Parameters

    • v: number

    Returns void

  • get dataView(): DataView
  • 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 dataView(v: DataView): void
  • Parameters

    • v: DataView

    Returns void

  • get dynamicSize(): 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.

    Returns boolean

  • set dynamicSize(v: boolean): void
  • Parameters

    • v: boolean

    Returns void

Methods

  • 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.

  • 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.

    • Optionale: boolean

      Endianness of the data to read.

    Returns Float32Array

    Float32Array to the DataStream backing buffer.

  • 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.

    • Optionale: boolean

      Endianness of the data to read.

    Returns Float64Array

    Float64Array to the DataStream backing buffer.

  • 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.

    • Optionale: boolean

      Endianness of the data to read.

    Returns Int16Array

    Int16Array to the DataStream backing buffer.

  • 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.

    • Optionale: boolean

      Endianness of the data to read.

    Returns Int32Array

    Int32Array to the DataStream backing buffer.

  • 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.

  • 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.

    • Optionale: boolean

      Endianness of the data to read.

    Returns Uint16Array

    Uint16Array to the DataStream backing buffer.

  • 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.

    • Optionale: boolean

      Endianness of the data to read.

    Returns Uint32Array

    Uint32Array to the DataStream backing buffer.

  • 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.

  • Reads an 8-bit int from the DataStream with the offset.

    Parameters

    • offset: number

      The offset.

    Returns number

    The read number.

  • 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

    • Optionallength: number

      The length of the string to read.

    Returns string

    The read string.

  • Reads a 32-bit float from the DataStream with the desired endianness.

    Parameters

    • Optionale: boolean

      Endianness of the number.

    Returns number

    The read number.

  • Reads a Float32Array of desired length and endianness from the DataStream.

    Parameters

    • length: number

      Number of elements to map.

    • Optionale: boolean

      Endianness of the data to read.

    Returns Float32Array

    The read Float32Array.

  • Reads a 64-bit float from the DataStream with the desired endianness.

    Parameters

    • Optionale: boolean

      Endianness of the number.

    Returns number

    The read number.

  • Reads a Float64Array of desired length and endianness from the DataStream.

    Parameters

    • length: number

      Number of elements to map.

    • Optionale: boolean

      Endianness of the data to read.

    Returns Float64Array

    The read Float64Array.

  • Reads a 32-bit int from the DataStream with the offset.

    Parameters

    • offset: number

      The offset.

    Returns number

    The read number.

  • Reads a 16-bit int from the DataStream with the desired endianness.

    Parameters

    • Optionale: boolean

      Endianness of the number.

    Returns number

    The read number.

  • Reads an Int16Array of desired length and endianness from the DataStream.

    Parameters

    • length: number

      Number of elements to map.

    • Optionale: boolean

      Endianness of the data to read.

    Returns Int16Array

    The read Int16Array.

  • Reads a 32-bit int from the DataStream with the desired endianness.

    Parameters

    • Optionale: boolean

      Endianness of the number.

    Returns number

    The read number.

  • Reads an Int32Array of desired length and endianness from the DataStream.

    Parameters

    • length: number

      Number of elements to map.

    • Optionale: boolean

      Endianness of the data to read.

    Returns Int32Array

    The read Int32Array.

  • Reads an 8-bit int from the DataStream.

    Returns number

    The read number.

  • Reads an Int8Array of desired length from the DataStream.

    Parameters

    • length: number

      Number of elements to map.

    Returns Int8Array

    The read Int8Array.

  • Reads a 16-bit int from the DataStream with the offset

    Parameters

    • offset: number

      The offset.

    Returns number

    The read number.

  • Read a string of desired length and encoding from the DataStream.

    Parameters

    • Optionallength: number

      The length of the string to read in bytes.

    • Optionalencoding: string

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

    Returns string

    The read 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.

  • 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.

  • 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.

  • Read UCS-2 string of desired length and endianness from the DataStream.

    Parameters

    • length: number

      The length of the string to read.

    • Optionalendianness: boolean

      The endianness of the string data in the DataStream.

    Returns string

    The read string.

  • Reads a 16-bit unsigned int from the DataStream with the desired endianness.

    Parameters

    • Optionale: boolean

      Endianness of the number.

    Returns number

    The read number.

  • Reads a Uint16Array of desired length and endianness from the DataStream.

    Parameters

    • length: number

      Number of elements to map.

    • Optionale: boolean

      Endianness of the data to read.

    Returns Uint16Array

    The read Uint16Array.

  • Reads a 32-bit unsigned int from the DataStream with the desired endianness.

    Parameters

    • Optionale: boolean

      Endianness of the number.

    Returns number

    The read number.

  • Reads a Uint32Array of desired length and endianness from the DataStream.

    Parameters

    • length: number

      Number of elements to map.

    • Optionale: boolean

      Endianness of the data to read.

    Returns Uint32Array

    The read Uint32Array.

  • Reads an 8-bit unsigned int from the DataStream.

    Returns number

    The read number.

  • Reads a Uint8Array of desired length from the DataStream.

    Parameters

    • length: number

      Number of elements to map.

    Returns Uint8Array

    The read Uint8Array.

  • 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

  • Sets the DataStream read/write position to given position. Clamps between 0 and DataStream length.

    Parameters

    • pos: number

      Position to seek to.

    Returns 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.

    • Optionallength: number

      The number of characters to write.

    Returns void

  • Writes a 32-bit float to the DataStream with the desired endianness.

    Parameters

    • v: number

      Number to write.

    • Optionale: boolean

      Endianness of the number.

    Returns void

  • Writes a Float32Array of specified endianness to the DataStream.

    Parameters

    • arr: ArrayLike<number> | Float32Array

      The array to write.

    • Optionale: boolean

      Endianness of the data to write.

    Returns void

  • Writes a 64-bit float to the DataStream with the desired endianness.

    Parameters

    • v: number

      Number to write.

    • Optionale: boolean

      Endianness of the number.

    Returns void

  • Writes a Float64Array of specified endianness to the DataStream.

    Parameters

    • arr: ArrayLike<number> | Float64Array

      The array to write.

    • Optionale: boolean

      Endianness of the data to write.

    Returns void

  • Writes a 16-bit int to the DataStream with the desired endianness.

    Parameters

    • v: number

      Number to write.

    • Optionale: boolean

      Endianness of the number.

    Returns void

  • Writes an Int16Array of specified endianness to the DataStream.

    Parameters

    • arr: ArrayLike<number> | Int16Array

      The array to write.

    • Optionale: boolean

      Endianness of the data to write.

    Returns void

  • Writes a 32-bit int to the DataStream with the desired endianness.

    Parameters

    • v: number

      Number to write.

    • Optionale: boolean

      Endianness of the number.

    Returns void

  • Writes an Int32Array of specified endianness to the DataStream.

    Parameters

    • arr: ArrayLike<number> | Int32Array

      The array to write.

    • Optionale: boolean

      Endianness of the data to write.

    Returns void

  • Writes an 8-bit int to the DataStream.

    Parameters

    • v: number

      Number to write.

    Returns void

  • Writes an Int8Array to the DataStream.

    Parameters

    • arr: ArrayLike<number> | Int8Array

      The array to write.

    Returns void

  • Writes a string of desired length and encoding to the DataStream.

    Parameters

    • s: string

      The string to write.

    • Optionalencoding: string

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

    • Optionallength: number

      The number of characters to write.

    Returns 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

  • 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

  • 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.

    • Optionalendianness: boolean

      The endianness to use for the written string data.

    • OptionallengthOverride: number

      The number of characters to write.

    Returns void

  • Writes a 16-bit unsigned int to the DataStream with the desired endianness.

    Parameters

    • v: number

      Number to write.

    • Optionale: boolean

      Endianness of the number.

    Returns void

  • Writes a Uint16Array of specified endianness to the DataStream.

    Parameters

    • arr: ArrayLike<number> | Uint16Array

      The array to write.

    • Optionale: boolean

      Endianness of the data to write.

    Returns void

  • Writes a 32-bit unsigned int to the DataStream with the desired endianness.

    Parameters

    • v: number

      Number to write.

    • Optionale: boolean

      Endianness of the number.

    Returns void

  • Writes a Uint32Array of specified endianness to the DataStream.

    Parameters

    • arr: ArrayLike<number> | Uint32Array

      The array to write.

    • Optionale: boolean

      Endianness of the data to write.

    Returns void

  • Writes an 8-bit unsigned int to the DataStream.

    Parameters

    • v: number

      Number to write.

    Returns void

  • Writes a Uint8Array to the DataStream.

    Parameters

    • arr: ArrayLike<number> | Uint8Array

      The array to write.

    Returns void