DataStream reads scalars, arrays and structs of data from an ArrayBuffer. It's like a file-like DataView on steroids.
ArrayBuffer to read from.
Offset from arrayBuffer beginning for the DataStream.
DataStream.BIG_ENDIAN or DataStream.LITTLE_ENDIAN (the default).
Seek position where readStruct ran into a problem. Useful for debugging struct parsing.
Static
BIG_Big-endian const to use as default endianness.
Static
endiannessNative endianness. Either DataStream.BIG_ENDIAN or DataStream.LITTLE_ENDIAN depending on the platform endianness.
Static
LITTLE_Little-endian const to use as default endianness.
Set/get the backing ArrayBuffer of the DataStream object. The setter updates the DataView to point to the new buffer.
Returns the byte length of the DataStream object.
Set/get the byteOffset of the DataStream object. The setter updates the DataView to point to the new byteOffset.
Set/get the backing DataView of the DataStream object. The setter updates the buffer and byteOffset to point to the DataView values.
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.
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.
Number of elements to map.
Optional
e: booleanEndianness of the data to read.
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.
Number of elements to map.
Optional
e: booleanEndianness of the data to read.
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.
Number of elements to map.
Optional
e: booleanEndianness of the data to read.
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.
Number of elements to map.
Optional
e: booleanEndianness of the data to read.
Int32Array 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.
Number of elements to map.
Optional
e: booleanEndianness of the data to read.
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.
Number of elements to map.
Optional
e: booleanEndianness of the data to read.
Uint32Array to the DataStream backing buffer.
Read a string of desired length and encoding from the DataStream.
Optional
length: numberThe length of the string to read in bytes.
Optional
encoding: stringThe encoding of the string data in the DataStream. Defaults to ASCII.
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.
Struct definition object.
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.
Type of the object to read.
Struct to refer to when resolving length references and for calling callbacks.
Returns the object on successful read, null on unsuccessful.
Read UCS-2 string of desired length and endianness from the DataStream.
The length of the string to read.
Optional
endianness: booleanThe endianness of the string data in the DataStream.
The read string.
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.
The string to write.
Optional
length: numberThe number of characters to write.
Writes a string of desired length and encoding to the DataStream.
The string to write.
Optional
encoding: stringThe encoding for the written string data. Defaults to ASCII.
Optional
length: numberThe number of characters to write.
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.
Type definition of the struct.
The struct data object.
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.
The string to write.
Optional
endianness: booleanThe endianness to use for the written string data.
Optional
lengthOverride: numberThe number of characters to write.
This DataStream is for internal use.