@npeersab/msgreader 2.0.1 → 3.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/utils.js CHANGED
@@ -1,6 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.uInt2int = exports.arraysEqual = void 0;
4
3
  function arraysEqual(a, b) {
5
4
  if (a === b)
6
5
  return true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@npeersab/msgreader",
3
- "version": "2.0.1",
3
+ "version": "3.8.3",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -20,9 +20,9 @@
20
20
  },
21
21
  "homepage": "https://github.com/npeersab/msgreader#readme",
22
22
  "devDependencies": {
23
- "typescript": "^3.7.4"
23
+ "typescript": "3.8.3"
24
24
  },
25
25
  "dependencies": {
26
- "fast-text-encoding": "^1.0.3"
26
+ "fast-text-encoding": "1.0.3"
27
27
  }
28
28
  }
@@ -1,293 +0,0 @@
1
- export declare type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array;
2
- /**
3
- * Type endsWith '*' mean array.
4
- * Type endsWith '+' mean array | utf8 string with length encoded as Uint16 & write/read before the actual array | utf8 string.
5
- */
6
- export declare type Type = 'Int8' | 'Int16' | 'Int32' | 'Uint8' | 'Uint16' | 'Uint32' | 'Float32' | 'Float64' | 'Int8*' | 'Int16*' | 'Int32*' | 'Uint8*' | 'Uint16*' | 'Uint32*' | 'Float32*' | 'Float64*' | 'Utf8WithLen';
7
- /**
8
- * DataStream reads scalars, arrays and structs of data from an ArrayBuffer.
9
- * It's like a file-like DataView on steroids.
10
- *
11
- * @param {ArrayBuffer} arrayBuffer ArrayBuffer to read from.
12
- * @param {?Number} byteOffset Offset from arrayBuffer beginning for the DataStream.
13
- * @param {?Boolean} endianness DataStream.BIG_ENDIAN or DataStream.LITTLE_ENDIAN (the default).
14
- */
15
- export default class DataStream {
16
- endianness: boolean;
17
- private _byteOffset;
18
- position: number;
19
- private _buffer;
20
- private _dataView;
21
- constructor(arrayBuffer?: ArrayBuffer | {
22
- buffer: ArrayBuffer;
23
- byteOffset: number;
24
- byteLength: number;
25
- }, byteOffset?: number, endianness?: boolean);
26
- /**
27
- * Big-endian const to use as default endianness.
28
- * @type {boolean}
29
- */
30
- static readonly BIG_ENDIAN: boolean;
31
- /**
32
- * Little-endian const to use as default endianness.
33
- * @type {boolean}
34
- */
35
- static readonly LITTLE_ENDIAN: boolean;
36
- /**
37
- * Whether to extend DataStream buffer when trying to write beyond its size.
38
- * If set, the buffer is reallocated to twice its current size until the
39
- * requested write fits the buffer.
40
- * @type {boolean}
41
- */
42
- private _dynamicSize;
43
- get dynamicSize(): boolean;
44
- set dynamicSize(v: boolean);
45
- /**
46
- * Virtual byte length of the DataStream backing buffer.
47
- * Updated to be max of original buffer size and last written size.
48
- * If dynamicSize is false is set to buffer size.
49
- * @type {number}
50
- */
51
- private _byteLength;
52
- /**
53
- * Returns the byte length of the DataStream object.
54
- * @type {number}
55
- */
56
- get byteLength(): number;
57
- /**
58
- * Set/get the backing ArrayBuffer of the DataStream object.
59
- * The setter updates the DataView to point to the new buffer.
60
- * @type {Object}
61
- */
62
- get buffer(): ArrayBuffer;
63
- set buffer(v: ArrayBuffer);
64
- /**
65
- * Set/get the byteOffset of the DataStream object.
66
- * The setter updates the DataView to point to the new byteOffset.
67
- * @type {number}
68
- */
69
- get byteOffset(): number;
70
- set byteOffset(v: number);
71
- /**
72
- * Set/get the backing DataView of the DataStream object.
73
- * The setter updates the buffer and byteOffset to point to the DataView values.
74
- * @type get: DataView, set: {buffer: ArrayBuffer, byteOffset: number, byteLength: number}
75
- */
76
- get dataView(): DataView;
77
- set dataView(v: DataView);
78
- bigEndian(): DataStream;
79
- /**
80
- * Internal function to resize the DataStream buffer when required.
81
- * @param {number} extra Number of bytes to add to the buffer allocation.
82
- * @return {null}
83
- */
84
- private _realloc;
85
- /**
86
- * Internal function to trim the DataStream buffer when required.
87
- * Used for stripping out the extra bytes from the backing buffer when
88
- * the virtual byteLength is smaller than the buffer byteLength (happens after
89
- * growing the buffer with writes and not filling the extra space completely).
90
- * @return {null}
91
- */
92
- private _trimAlloc;
93
- /**
94
- * Sets the DataStream read/write position to given position.
95
- * Clamps between 0 and DataStream length.
96
- * @param {number} pos Position to seek to.
97
- * @return {null}
98
- */
99
- seek(pos: number): void;
100
- /**
101
- * Returns true if the DataStream seek pointer is at the end of buffer and
102
- * there's no more data to read.
103
- * @return {boolean} True if the seek pointer is at the end of the buffer.
104
- */
105
- isEof(): boolean;
106
- /**
107
- * Maps a Uint8Array into the DataStream buffer.
108
- *
109
- * Nice for quickly reading in data.
110
- *
111
- * @param {number} length Number of elements to map.
112
- * @return {Object} Uint8Array to the DataStream backing buffer.
113
- */
114
- mapUint8Array(length: number): Uint8Array;
115
- /**
116
- Reads a 16-bit int from the DataStream with the offset
117
-
118
- @param {number} offset The offset.
119
- @return {number} The read number.
120
- */
121
- readShort(offset: number): number;
122
- /**
123
- Reads an 8-bit int from the DataStream with the offset.
124
-
125
- @param {number} offset The offset.
126
- @return {number} The read number.
127
- */
128
- readByte(offset: number): number;
129
- /**
130
- Read UCS-2 string of desired length and offset from the DataStream.
131
-
132
- @param {number} offset The offset.
133
- @param {number} length The length of the string to read.
134
- @return {string} The read string.
135
- */
136
- readStringAt(offset: number, length: number): string;
137
- /**
138
- * Reads an Int32Array of desired length and endianness from the DataStream.
139
- *
140
- * @param {number} length Number of elements to map.
141
- * @param {?boolean} e Endianness of the data to read.
142
- * @return {Object} The read Int32Array.
143
- */
144
- readInt32Array(length: number, e?: boolean): Int32Array;
145
- /**
146
- * Reads an Int16Array of desired length and endianness from the DataStream.
147
- *
148
- * @param {number} length Number of elements to map.
149
- * @param {?boolean} e Endianness of the data to read.
150
- * @return {Object} The read Int16Array.
151
- */
152
- readInt16Array(length: number, e?: boolean): Int16Array;
153
- /**
154
- * Reads an Int8Array of desired length from the DataStream.
155
- *
156
- * @param {number} length Number of elements to map.
157
- * @return {Object} The read Int8Array.
158
- */
159
- readInt8Array(length: number): Int8Array;
160
- /**
161
- * Reads a Uint32Array of desired length and endianness from the DataStream.
162
- *
163
- * @param {number} length Number of elements to map.
164
- * @param {?boolean} e Endianness of the data to read.
165
- * @return {Object} The read Uint32Array.
166
- */
167
- readUint32Array(length: number, e?: boolean): Uint32Array;
168
- /**
169
- * Reads a Uint16Array of desired length and endianness from the DataStream.
170
- *
171
- * @param {number} length Number of elements to map.
172
- * @param {?boolean} e Endianness of the data to read.
173
- * @return {Object} The read Uint16Array.
174
- */
175
- readUint16Array(length: number, e?: boolean): Uint16Array;
176
- /**
177
- * Reads a Uint8Array of desired length from the DataStream.
178
- *
179
- * @param {number} length Number of elements to map.
180
- * @return {Object} The read Uint8Array.
181
- */
182
- readUint8Array(length: number): Uint8Array;
183
- /**
184
- * Reads a Float64Array of desired length and endianness from the DataStream.
185
- *
186
- * @param {number} length Number of elements to map.
187
- * @param {?boolean} e Endianness of the data to read.
188
- * @return {Object} The read Float64Array.
189
- */
190
- readFloat64Array(length: number, e?: boolean): Float64Array;
191
- /**
192
- * Reads a Float32Array of desired length and endianness from the DataStream.
193
- *
194
- * @param {number} length Number of elements to map.
195
- * @param {?boolean} e Endianness of the data to read.
196
- * @return {Object} The read Float32Array.
197
- */
198
- readFloat32Array(length: number, e?: boolean): Float32Array;
199
- /**
200
- * Reads a 32-bit int from the DataStream with the desired endianness.
201
- *
202
- * @param {?boolean} e Endianness of the number.
203
- * @return {number} The read number.
204
- */
205
- readInt32(e?: boolean): number;
206
- /**
207
- * Reads a 16-bit int from the DataStream with the desired endianness.
208
- *
209
- * @param {?boolean} e Endianness of the number.
210
- * @return {number} The read number.
211
- */
212
- readInt16(e?: boolean): number;
213
- /**
214
- * Reads an 8-bit int from the DataStream.
215
- *
216
- * @return {number} The read number.
217
- */
218
- readInt8(): number;
219
- /**
220
- Reads a 32-bit int from the DataStream with the offset.
221
-
222
- @param {number} offset The offset.
223
- @return {number} The read number.
224
- */
225
- readInt(offset: number): number;
226
- /**
227
- * Native endianness. Either DataStream.BIG_ENDIAN or DataStream.LITTLE_ENDIAN
228
- * depending on the platform endianness.
229
- *
230
- * @type {boolean}
231
- */
232
- static readonly endianness: boolean;
233
- /**
234
- * Copies byteLength bytes from the src buffer at srcOffset to the
235
- * dst buffer at dstOffset.
236
- *
237
- * @param {Object} dst Destination ArrayBuffer to write to.
238
- * @param {number} dstOffset Offset to the destination ArrayBuffer.
239
- * @param {Object} src Source ArrayBuffer to read from.
240
- * @param {number} srcOffset Offset to the source ArrayBuffer.
241
- * @param {number} byteLength Number of bytes to copy.
242
- */
243
- static memcpy(dst: ArrayBufferLike, dstOffset: number, src: ArrayBuffer, srcOffset: number, byteLength: number): void;
244
- /**
245
- * Converts array to native endianness in-place.
246
- *
247
- * @param {Object} array Typed array to convert.
248
- * @param {boolean} arrayIsLittleEndian True if the data in the array is
249
- * little-endian. Set false for big-endian.
250
- * @return {Object} The converted typed array.
251
- */
252
- static arrayToNative(array: TypedArray, arrayIsLittleEndian: boolean): object;
253
- /**
254
- * Flips typed array endianness in-place.
255
- *
256
- * @param {Object} array Typed array to flip.
257
- * @return {Object} The converted typed array.
258
- */
259
- static flipArrayEndianness(array: TypedArray): object;
260
- /**
261
- * Creates an array from an array of character codes.
262
- * Uses String.fromCharCode in chunks for memory efficiency and then concatenates
263
- * the resulting string chunks.
264
- *
265
- * @param {TypedArray} array Array of character codes.
266
- * @return {string} String created from the character codes.
267
- */
268
- static createStringFromArray(array: TypedArray): string;
269
- /**
270
- * Seek position where DataStream#readStruct ran into a problem.
271
- * Useful for debugging struct parsing.
272
- *
273
- * @type {number}
274
- */
275
- failurePosition: number;
276
- /**
277
- * Read UCS-2 string of desired length and endianness from the DataStream.
278
- *
279
- * @param {number} length The length of the string to read.
280
- * @param {boolean} endianness The endianness of the string data in the DataStream.
281
- * @return {string} The read string.
282
- */
283
- readUCS2String(length: number, endianness?: boolean): string;
284
- /**
285
- * Read a string of desired length and encoding from the DataStream.
286
- *
287
- * @param {number} length The length of the string to read in bytes.
288
- * @param {?string} encoding The encoding of the string data in the DataStream.
289
- * Defaults to ASCII.
290
- * @return {string} The read string.
291
- */
292
- readString(length: number, encoding?: string): string;
293
- }
@@ -1,490 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const fast_text_encoding_1 = require("fast-text-encoding");
4
- /**
5
- * DataStream reads scalars, arrays and structs of data from an ArrayBuffer.
6
- * It's like a file-like DataView on steroids.
7
- *
8
- * @param {ArrayBuffer} arrayBuffer ArrayBuffer to read from.
9
- * @param {?Number} byteOffset Offset from arrayBuffer beginning for the DataStream.
10
- * @param {?Boolean} endianness DataStream.BIG_ENDIAN or DataStream.LITTLE_ENDIAN (the default).
11
- */
12
- class DataStream {
13
- constructor(arrayBuffer, byteOffset, endianness = DataStream.LITTLE_ENDIAN) {
14
- this.endianness = endianness;
15
- this.position = 0;
16
- /**
17
- * Whether to extend DataStream buffer when trying to write beyond its size.
18
- * If set, the buffer is reallocated to twice its current size until the
19
- * requested write fits the buffer.
20
- * @type {boolean}
21
- */
22
- this._dynamicSize = true;
23
- /**
24
- * Virtual byte length of the DataStream backing buffer.
25
- * Updated to be max of original buffer size and last written size.
26
- * If dynamicSize is false is set to buffer size.
27
- * @type {number}
28
- */
29
- this._byteLength = 0;
30
- /**
31
- * Seek position where DataStream#readStruct ran into a problem.
32
- * Useful for debugging struct parsing.
33
- *
34
- * @type {number}
35
- */
36
- this.failurePosition = 0;
37
- this._byteOffset = byteOffset || 0;
38
- if (arrayBuffer instanceof ArrayBuffer) {
39
- this.buffer = arrayBuffer;
40
- }
41
- else if (typeof arrayBuffer === 'object') {
42
- this.dataView = arrayBuffer;
43
- if (byteOffset) {
44
- this._byteOffset += byteOffset;
45
- }
46
- }
47
- else {
48
- this.buffer = new ArrayBuffer(arrayBuffer || 1);
49
- }
50
- }
51
- get dynamicSize() {
52
- return this._dynamicSize;
53
- }
54
- set dynamicSize(v) {
55
- if (!v) {
56
- this._trimAlloc();
57
- }
58
- this._dynamicSize = v;
59
- }
60
- /**
61
- * Returns the byte length of the DataStream object.
62
- * @type {number}
63
- */
64
- get byteLength() {
65
- return this._byteLength - this._byteOffset;
66
- }
67
- /**
68
- * Set/get the backing ArrayBuffer of the DataStream object.
69
- * The setter updates the DataView to point to the new buffer.
70
- * @type {Object}
71
- */
72
- get buffer() {
73
- this._trimAlloc();
74
- return this._buffer;
75
- }
76
- set buffer(v) {
77
- this._buffer = v;
78
- this._dataView = new DataView(this._buffer, this._byteOffset);
79
- this._byteLength = this._buffer.byteLength;
80
- }
81
- /**
82
- * Set/get the byteOffset of the DataStream object.
83
- * The setter updates the DataView to point to the new byteOffset.
84
- * @type {number}
85
- */
86
- get byteOffset() {
87
- return this._byteOffset;
88
- }
89
- set byteOffset(v) {
90
- this._byteOffset = v;
91
- this._dataView = new DataView(this._buffer, this._byteOffset);
92
- this._byteLength = this._buffer.byteLength;
93
- }
94
- /**
95
- * Set/get the backing DataView of the DataStream object.
96
- * The setter updates the buffer and byteOffset to point to the DataView values.
97
- * @type get: DataView, set: {buffer: ArrayBuffer, byteOffset: number, byteLength: number}
98
- */
99
- get dataView() {
100
- return this._dataView;
101
- }
102
- set dataView(v) {
103
- this._byteOffset = v.byteOffset;
104
- this._buffer = v.buffer;
105
- this._dataView = new DataView(this._buffer, this._byteOffset);
106
- this._byteLength = this._byteOffset + v.byteLength;
107
- }
108
- bigEndian() {
109
- this.endianness = DataStream.BIG_ENDIAN;
110
- return this;
111
- }
112
- /**
113
- * Internal function to resize the DataStream buffer when required.
114
- * @param {number} extra Number of bytes to add to the buffer allocation.
115
- * @return {null}
116
- */
117
- _realloc(extra) {
118
- if (!this._dynamicSize) {
119
- return;
120
- }
121
- const req = this._byteOffset + this.position + extra;
122
- let blen = this._buffer.byteLength;
123
- if (req <= blen) {
124
- if (req > this._byteLength) {
125
- this._byteLength = req;
126
- }
127
- return;
128
- }
129
- if (blen < 1) {
130
- blen = 1;
131
- }
132
- while (req > blen) {
133
- blen *= 2;
134
- }
135
- const buf = new ArrayBuffer(blen);
136
- const src = new Uint8Array(this._buffer);
137
- const dst = new Uint8Array(buf, 0, src.length);
138
- dst.set(src);
139
- this.buffer = buf;
140
- this._byteLength = req;
141
- }
142
- /**
143
- * Internal function to trim the DataStream buffer when required.
144
- * Used for stripping out the extra bytes from the backing buffer when
145
- * the virtual byteLength is smaller than the buffer byteLength (happens after
146
- * growing the buffer with writes and not filling the extra space completely).
147
- * @return {null}
148
- */
149
- _trimAlloc() {
150
- if (this._byteLength === this._buffer.byteLength) {
151
- return;
152
- }
153
- const buf = new ArrayBuffer(this._byteLength);
154
- const dst = new Uint8Array(buf);
155
- const src = new Uint8Array(this._buffer, 0, dst.length);
156
- dst.set(src);
157
- this.buffer = buf;
158
- }
159
- /**
160
- * Sets the DataStream read/write position to given position.
161
- * Clamps between 0 and DataStream length.
162
- * @param {number} pos Position to seek to.
163
- * @return {null}
164
- */
165
- seek(pos) {
166
- const npos = Math.max(0, Math.min(this.byteLength, pos));
167
- this.position = isNaN(npos) || !isFinite(npos) ? 0 : npos;
168
- }
169
- /**
170
- * Returns true if the DataStream seek pointer is at the end of buffer and
171
- * there's no more data to read.
172
- * @return {boolean} True if the seek pointer is at the end of the buffer.
173
- */
174
- isEof() {
175
- return this.position >= this.byteLength;
176
- }
177
- /**
178
- * Maps a Uint8Array into the DataStream buffer.
179
- *
180
- * Nice for quickly reading in data.
181
- *
182
- * @param {number} length Number of elements to map.
183
- * @return {Object} Uint8Array to the DataStream backing buffer.
184
- */
185
- mapUint8Array(length) {
186
- this._realloc(length);
187
- const arr = new Uint8Array(this._buffer, this.byteOffset + this.position, length);
188
- this.position += length;
189
- return arr;
190
- }
191
- /**
192
- Reads a 16-bit int from the DataStream with the offset
193
-
194
- @param {number} offset The offset.
195
- @return {number} The read number.
196
- */
197
- readShort(offset) {
198
- this.seek(offset);
199
- return this.readInt16();
200
- }
201
- /**
202
- Reads an 8-bit int from the DataStream with the offset.
203
-
204
- @param {number} offset The offset.
205
- @return {number} The read number.
206
- */
207
- readByte(offset) {
208
- this.seek(offset);
209
- return this.readInt8();
210
- }
211
- /**
212
- Read UCS-2 string of desired length and offset from the DataStream.
213
-
214
- @param {number} offset The offset.
215
- @param {number} length The length of the string to read.
216
- @return {string} The read string.
217
- */
218
- readStringAt(offset, length) {
219
- this.seek(offset);
220
- return this.readUCS2String(length);
221
- }
222
- /**
223
- * Reads an Int32Array of desired length and endianness from the DataStream.
224
- *
225
- * @param {number} length Number of elements to map.
226
- * @param {?boolean} e Endianness of the data to read.
227
- * @return {Object} The read Int32Array.
228
- */
229
- readInt32Array(length, e) {
230
- length = length == null ? this.byteLength - this.position / 4 : length;
231
- const arr = new Int32Array(length);
232
- DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset + this.position, length * arr.BYTES_PER_ELEMENT);
233
- DataStream.arrayToNative(arr, e == null ? this.endianness : e);
234
- this.position += arr.byteLength;
235
- return arr;
236
- }
237
- /**
238
- * Reads an Int16Array of desired length and endianness from the DataStream.
239
- *
240
- * @param {number} length Number of elements to map.
241
- * @param {?boolean} e Endianness of the data to read.
242
- * @return {Object} The read Int16Array.
243
- */
244
- readInt16Array(length, e) {
245
- length = length == null ? this.byteLength - this.position / 2 : length;
246
- const arr = new Int16Array(length);
247
- DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset + this.position, length * arr.BYTES_PER_ELEMENT);
248
- DataStream.arrayToNative(arr, e == null ? this.endianness : e);
249
- this.position += arr.byteLength;
250
- return arr;
251
- }
252
- /**
253
- * Reads an Int8Array of desired length from the DataStream.
254
- *
255
- * @param {number} length Number of elements to map.
256
- * @return {Object} The read Int8Array.
257
- */
258
- readInt8Array(length) {
259
- length = length == null ? this.byteLength - this.position : length;
260
- const arr = new Int8Array(length);
261
- DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset + this.position, length * arr.BYTES_PER_ELEMENT);
262
- this.position += arr.byteLength;
263
- return arr;
264
- }
265
- /**
266
- * Reads a Uint32Array of desired length and endianness from the DataStream.
267
- *
268
- * @param {number} length Number of elements to map.
269
- * @param {?boolean} e Endianness of the data to read.
270
- * @return {Object} The read Uint32Array.
271
- */
272
- readUint32Array(length, e) {
273
- length = length == null ? this.byteLength - this.position / 4 : length;
274
- const arr = new Uint32Array(length);
275
- DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset + this.position, length * arr.BYTES_PER_ELEMENT);
276
- DataStream.arrayToNative(arr, e == null ? this.endianness : e);
277
- this.position += arr.byteLength;
278
- return arr;
279
- }
280
- /**
281
- * Reads a Uint16Array of desired length and endianness from the DataStream.
282
- *
283
- * @param {number} length Number of elements to map.
284
- * @param {?boolean} e Endianness of the data to read.
285
- * @return {Object} The read Uint16Array.
286
- */
287
- readUint16Array(length, e) {
288
- length = length == null ? this.byteLength - this.position / 2 : length;
289
- const arr = new Uint16Array(length);
290
- DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset + this.position, length * arr.BYTES_PER_ELEMENT);
291
- DataStream.arrayToNative(arr, e == null ? this.endianness : e);
292
- this.position += arr.byteLength;
293
- return arr;
294
- }
295
- /**
296
- * Reads a Uint8Array of desired length from the DataStream.
297
- *
298
- * @param {number} length Number of elements to map.
299
- * @return {Object} The read Uint8Array.
300
- */
301
- readUint8Array(length) {
302
- length = length == null ? this.byteLength - this.position : length;
303
- const arr = new Uint8Array(length);
304
- DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset + this.position, length * arr.BYTES_PER_ELEMENT);
305
- this.position += arr.byteLength;
306
- return arr;
307
- }
308
- /**
309
- * Reads a Float64Array of desired length and endianness from the DataStream.
310
- *
311
- * @param {number} length Number of elements to map.
312
- * @param {?boolean} e Endianness of the data to read.
313
- * @return {Object} The read Float64Array.
314
- */
315
- readFloat64Array(length, e) {
316
- length = length == null ? this.byteLength - this.position / 8 : length;
317
- const arr = new Float64Array(length);
318
- DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset + this.position, length * arr.BYTES_PER_ELEMENT);
319
- DataStream.arrayToNative(arr, e == null ? this.endianness : e);
320
- this.position += arr.byteLength;
321
- return arr;
322
- }
323
- /**
324
- * Reads a Float32Array of desired length and endianness from the DataStream.
325
- *
326
- * @param {number} length Number of elements to map.
327
- * @param {?boolean} e Endianness of the data to read.
328
- * @return {Object} The read Float32Array.
329
- */
330
- readFloat32Array(length, e) {
331
- length = length == null ? this.byteLength - this.position / 4 : length;
332
- const arr = new Float32Array(length);
333
- DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset + this.position, length * arr.BYTES_PER_ELEMENT);
334
- DataStream.arrayToNative(arr, e == null ? this.endianness : e);
335
- this.position += arr.byteLength;
336
- return arr;
337
- }
338
- /**
339
- * Reads a 32-bit int from the DataStream with the desired endianness.
340
- *
341
- * @param {?boolean} e Endianness of the number.
342
- * @return {number} The read number.
343
- */
344
- readInt32(e) {
345
- const v = this._dataView.getInt32(this.position, e == null ? this.endianness : e);
346
- this.position += 4;
347
- return v;
348
- }
349
- /**
350
- * Reads a 16-bit int from the DataStream with the desired endianness.
351
- *
352
- * @param {?boolean} e Endianness of the number.
353
- * @return {number} The read number.
354
- */
355
- readInt16(e) {
356
- const v = this._dataView.getInt16(this.position, e == null ? this.endianness : e);
357
- this.position += 2;
358
- return v;
359
- }
360
- /**
361
- * Reads an 8-bit int from the DataStream.
362
- *
363
- * @return {number} The read number.
364
- */
365
- readInt8() {
366
- const v = this._dataView.getInt8(this.position);
367
- this.position += 1;
368
- return v;
369
- }
370
- /**
371
- Reads a 32-bit int from the DataStream with the offset.
372
-
373
- @param {number} offset The offset.
374
- @return {number} The read number.
375
- */
376
- readInt(offset) {
377
- this.seek(offset);
378
- return this.readInt32();
379
- }
380
- /**
381
- * Copies byteLength bytes from the src buffer at srcOffset to the
382
- * dst buffer at dstOffset.
383
- *
384
- * @param {Object} dst Destination ArrayBuffer to write to.
385
- * @param {number} dstOffset Offset to the destination ArrayBuffer.
386
- * @param {Object} src Source ArrayBuffer to read from.
387
- * @param {number} srcOffset Offset to the source ArrayBuffer.
388
- * @param {number} byteLength Number of bytes to copy.
389
- */
390
- static memcpy(dst, dstOffset, src, srcOffset, byteLength) {
391
- const dstU8 = new Uint8Array(dst, dstOffset, byteLength);
392
- const srcU8 = new Uint8Array(src, srcOffset, byteLength);
393
- dstU8.set(srcU8);
394
- }
395
- /**
396
- * Converts array to native endianness in-place.
397
- *
398
- * @param {Object} array Typed array to convert.
399
- * @param {boolean} arrayIsLittleEndian True if the data in the array is
400
- * little-endian. Set false for big-endian.
401
- * @return {Object} The converted typed array.
402
- */
403
- static arrayToNative(array, arrayIsLittleEndian) {
404
- if (arrayIsLittleEndian === this.endianness) {
405
- return array;
406
- }
407
- else {
408
- return this.flipArrayEndianness(array); // ???
409
- }
410
- }
411
- /**
412
- * Flips typed array endianness in-place.
413
- *
414
- * @param {Object} array Typed array to flip.
415
- * @return {Object} The converted typed array.
416
- */
417
- static flipArrayEndianness(array) {
418
- const u8 = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
419
- for (let i = 0; i < array.byteLength; i += array.BYTES_PER_ELEMENT) {
420
- for (
421
- // tslint:disable-next-line one-variable-per-declaration
422
- let j = i + array.BYTES_PER_ELEMENT - 1, k = i; j > k; j--, k++) {
423
- const tmp = u8[k];
424
- u8[k] = u8[j];
425
- u8[j] = tmp;
426
- }
427
- }
428
- return array;
429
- }
430
- /**
431
- * Creates an array from an array of character codes.
432
- * Uses String.fromCharCode in chunks for memory efficiency and then concatenates
433
- * the resulting string chunks.
434
- *
435
- * @param {TypedArray} array Array of character codes.
436
- * @return {string} String created from the character codes.
437
- */
438
- static createStringFromArray(array) {
439
- const chunkSize = 0x8000;
440
- const chunks = [];
441
- for (let i = 0; i < array.length; i += chunkSize) {
442
- chunks.push(String.fromCharCode.apply(null, array.subarray(i, i + chunkSize)));
443
- }
444
- return chunks.join('');
445
- }
446
- /**
447
- * Read UCS-2 string of desired length and endianness from the DataStream.
448
- *
449
- * @param {number} length The length of the string to read.
450
- * @param {boolean} endianness The endianness of the string data in the DataStream.
451
- * @return {string} The read string.
452
- */
453
- readUCS2String(length, endianness) {
454
- return DataStream.createStringFromArray(this.readUint16Array(length, endianness));
455
- }
456
- /**
457
- * Read a string of desired length and encoding from the DataStream.
458
- *
459
- * @param {number} length The length of the string to read in bytes.
460
- * @param {?string} encoding The encoding of the string data in the DataStream.
461
- * Defaults to ASCII.
462
- * @return {string} The read string.
463
- */
464
- readString(length, encoding) {
465
- if (encoding == null || encoding === 'ASCII') {
466
- return DataStream.createStringFromArray(this.mapUint8Array(length == null ? this.byteLength - this.position : length));
467
- }
468
- else {
469
- return new fast_text_encoding_1.TextDecoder(encoding).decode(this.mapUint8Array(length));
470
- }
471
- }
472
- }
473
- exports.default = DataStream;
474
- /**
475
- * Big-endian const to use as default endianness.
476
- * @type {boolean}
477
- */
478
- DataStream.BIG_ENDIAN = false;
479
- /**
480
- * Little-endian const to use as default endianness.
481
- * @type {boolean}
482
- */
483
- DataStream.LITTLE_ENDIAN = true;
484
- /**
485
- * Native endianness. Either DataStream.BIG_ENDIAN or DataStream.LITTLE_ENDIAN
486
- * depending on the platform endianness.
487
- *
488
- * @type {boolean}
489
- */
490
- DataStream.endianness = new Int8Array(new Int16Array([1]).buffer)[0] > 0;