@woosh/meep-engine 2.39.31 → 2.39.34

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.
@@ -21,1076 +21,1074 @@ export const EndianType = {
21
21
  const MIN_GROWTH_STEP = 1024;
22
22
 
23
23
  /**
24
- *
25
- * @constructor
24
+ * 2^31-1, values above this will be cropped incorrectly when bit-shifting
25
+ * @type {number}
26
26
  */
27
- function BinaryBuffer() {
28
- /**
29
- *
30
- * @type {EndianType|boolean}
31
- */
32
- this.endianness = EndianType.BigEndian;
33
-
34
- /**
35
- *
36
- * @type {number}
37
- */
38
- this.position = 0;
39
-
40
- /**
41
- *
42
- * @type {number}
43
- */
44
- this.length = 0;
27
+ const MAX_SAFE_UINT_VAR = 2147483647;
45
28
 
29
+ export class BinaryBuffer {
46
30
  /**
47
31
  *
48
- * @type {number}
32
+ * @constructor
49
33
  */
50
- this.capacity = 0;
34
+ constructor() {
35
+ /**
36
+ *
37
+ * @type {EndianType|boolean}
38
+ */
39
+ this.endianness = EndianType.BigEndian;
40
+
41
+ /**
42
+ *
43
+ * @type {number}
44
+ */
45
+ this.position = 0;
46
+
47
+ /**
48
+ *
49
+ * @type {number}
50
+ */
51
+ this.length = 0;
52
+
53
+ /**
54
+ *
55
+ * @type {number}
56
+ */
57
+ this.capacity = 0;
58
+
59
+ /**
60
+ *
61
+ * @type {ArrayBuffer}
62
+ * @private
63
+ */
64
+ this.data = new ArrayBuffer(0);
65
+
66
+ /**
67
+ *
68
+ * @type {DataView}
69
+ * @private
70
+ */
71
+ this.dataView = new DataView(this.data);
72
+
73
+ /**
74
+ *
75
+ * @type {Uint8Array}
76
+ * @private
77
+ */
78
+ this.__data_uint8 = new Uint8Array(0);
79
+
80
+ /**
81
+ *
82
+ * @type {number}
83
+ * @private
84
+ */
85
+ this.__growFactor = 1.1;
86
+ }
51
87
 
52
88
  /**
53
- *
54
- * @type {ArrayBuffer}
55
- * @private
89
+ * @param {ArrayBuffer} arrayBuffer
56
90
  */
57
- this.data = new ArrayBuffer(0);
91
+ fromArrayBuffer(arrayBuffer) {
92
+ this.data = arrayBuffer;
93
+ this.dataView = new DataView(arrayBuffer);
94
+ this.__data_uint8 = new Uint8Array(arrayBuffer);
95
+
96
+ this.capacity = arrayBuffer.byteLength;
97
+ //assume that all the data is useful
98
+ this.length = this.capacity;
99
+ this.position = 0;
100
+ }
58
101
 
59
102
  /**
60
103
  *
61
- * @type {DataView}
62
- * @private
104
+ * @returns {BinaryBuffer}
63
105
  */
64
- this.dataView = new DataView(this.data);
106
+ trim() {
107
+ this.setCapacity(this.position);
65
108
 
66
- /**
67
- *
68
- * @type {Uint8Array}
69
- * @private
70
- */
71
- this.__data_uint8 = new Uint8Array(0);
109
+ return this;
110
+ }
72
111
 
73
112
  /**
74
113
  *
75
- * @type {number}
76
- * @private
114
+ * @param {number} capacity
77
115
  */
78
- this.__growFactor = 1.1;
79
- }
116
+ setCapacity(capacity) {
117
+ const oldData = this.__data_uint8;
118
+ const newData = new Uint8Array(capacity);
80
119
 
81
- /**
82
- * @param {ArrayBuffer} arrayBuffer
83
- */
84
- BinaryBuffer.prototype.fromArrayBuffer = function (arrayBuffer) {
85
- this.data = arrayBuffer;
86
- this.dataView = new DataView(arrayBuffer);
87
- this.__data_uint8 = new Uint8Array(arrayBuffer);
88
-
89
- this.capacity = arrayBuffer.byteLength;
90
- //assume that all the data is useful
91
- this.length = this.capacity;
92
- this.position = 0;
93
- };
120
+ //copy old data
121
+ const copyLength = Math.min(newData.length, oldData.length);
122
+ newData.set(oldData.subarray(0, copyLength), 0);
94
123
 
95
- /**
96
- *
97
- * @param {ArrayBuffer} v
98
- * @return {BinaryBuffer}
99
- */
100
- BinaryBuffer.fromArrayBuffer = function (v) {
101
- const r = new BinaryBuffer();
102
-
103
- r.fromArrayBuffer(v);
104
-
105
- return r;
106
- };
124
+ this.data = newData.buffer;
125
+ this.__data_uint8 = newData;
126
+ this.dataView = new DataView(this.data);
107
127
 
108
- /**
109
- *
110
- * @returns {BinaryBuffer}
111
- */
112
- BinaryBuffer.prototype.trim = function () {
113
- this.setCapacity(this.position);
114
-
115
- return this;
116
- };
117
-
118
- /**
119
- *
120
- * @param {number} capacity
121
- */
122
- BinaryBuffer.prototype.setCapacity = function (capacity) {
123
- const oldData = this.__data_uint8;
124
- const newData = new Uint8Array(capacity);
125
-
126
- //copy old data
127
- const copyLength = Math.min(newData.length, oldData.length);
128
- newData.set(oldData.subarray(0, copyLength), 0);
129
-
130
- this.data = newData.buffer;
131
- this.__data_uint8 = newData;
132
- this.dataView = new DataView(this.data);
133
-
134
- this.capacity = capacity;
135
- };
136
-
137
- BinaryBuffer.prototype.ensureCapacity = function (minCapacity) {
138
- if (this.capacity < minCapacity) {
139
- const newCapacity = Math.ceil(Math.max(
140
- minCapacity,
141
- this.capacity * this.__growFactor,
142
- this.capacity + MIN_GROWTH_STEP
143
- ));
144
- this.setCapacity(newCapacity);
128
+ this.capacity = capacity;
145
129
  }
146
- };
147
130
 
148
- BinaryBuffer.prototype.readFloat32 = function () {
149
- const result = this.dataView.getFloat32(this.position, this.endianness);
131
+ ensureCapacity(minCapacity) {
132
+ if (this.capacity < minCapacity) {
133
+ const newCapacity = Math.ceil(Math.max(
134
+ minCapacity,
135
+ this.capacity * this.__growFactor,
136
+ this.capacity + MIN_GROWTH_STEP
137
+ ));
138
+ this.setCapacity(newCapacity);
139
+ }
140
+ }
150
141
 
151
- this.position += 4;
142
+ readFloat32() {
143
+ const result = this.dataView.getFloat32(this.position, this.endianness);
152
144
 
153
- return result;
154
- };
145
+ this.position += 4;
155
146
 
156
- BinaryBuffer.prototype.readFloat64 = function () {
157
- const result = this.dataView.getFloat64(this.position, this.endianness);
147
+ return result;
148
+ }
158
149
 
159
- this.position += 8;
150
+ readFloat64() {
151
+ const result = this.dataView.getFloat64(this.position, this.endianness);
160
152
 
161
- return result;
162
- };
153
+ this.position += 8;
163
154
 
164
- BinaryBuffer.prototype.readInt8 = function () {
165
- const result = this.dataView.getInt8(this.position);
155
+ return result;
156
+ }
166
157
 
167
- this.position += 1;
158
+ readInt8() {
159
+ const result = this.dataView.getInt8(this.position);
168
160
 
169
- return result;
170
- };
161
+ this.position += 1;
171
162
 
172
- BinaryBuffer.prototype.readInt16 = function () {
173
- const result = this.dataView.getInt16(this.position, this.endianness);
163
+ return result;
164
+ }
174
165
 
175
- this.position += 2;
166
+ readInt16() {
167
+ const result = this.dataView.getInt16(this.position, this.endianness);
176
168
 
177
- return result;
178
- };
169
+ this.position += 2;
179
170
 
180
- /**
181
- *
182
- * @returns {number}
183
- */
184
- BinaryBuffer.prototype.readInt32 = function () {
185
- const result = this.dataView.getInt32(this.position, this.endianness);
171
+ return result;
172
+ }
186
173
 
187
- this.position += 4;
174
+ /**
175
+ *
176
+ * @returns {number}
177
+ */
178
+ readInt32() {
179
+ const result = this.dataView.getInt32(this.position, this.endianness);
188
180
 
189
- return result;
190
- };
181
+ this.position += 4;
191
182
 
192
- /**
193
- *
194
- * @returns {number}
195
- */
196
- BinaryBuffer.prototype.readUint8 = function () {
197
- const result = this.dataView.getUint8(this.position);
183
+ return result;
184
+ }
198
185
 
199
- this.position += 1;
186
+ /**
187
+ *
188
+ * @returns {number}
189
+ */
190
+ readUint8() {
191
+ const result = this.dataView.getUint8(this.position);
200
192
 
201
- return result;
202
- };
193
+ this.position += 1;
203
194
 
204
- /**
205
- *
206
- * @returns {number}
207
- */
208
- BinaryBuffer.prototype.readUint16 = function () {
209
- const result = this.dataView.getUint16(this.position, this.endianness);
195
+ return result;
196
+ }
210
197
 
211
- this.position += 2;
198
+ /**
199
+ *
200
+ * @returns {number}
201
+ */
202
+ readUint16() {
203
+ const result = this.dataView.getUint16(this.position, this.endianness);
212
204
 
213
- return result;
214
- };
205
+ this.position += 2;
215
206
 
216
- /**
217
- *
218
- * @returns {number}
219
- */
220
- BinaryBuffer.prototype.readUint16LE = function () {
221
- const result = this.dataView.getUint16(this.position, EndianType.LittleEndian);
207
+ return result;
208
+ }
222
209
 
223
- this.position += 2;
210
+ /**
211
+ *
212
+ * @returns {number}
213
+ */
214
+ readUint16LE() {
215
+ const result = this.dataView.getUint16(this.position, EndianType.LittleEndian);
224
216
 
225
- return result;
226
- };
217
+ this.position += 2;
227
218
 
228
- /**
229
- *
230
- * @returns {number}
231
- */
232
- BinaryBuffer.prototype.readUint16BE = function () {
233
- const result = this.dataView.getUint16(this.position, EndianType.BigEndian);
219
+ return result;
220
+ }
234
221
 
235
- this.position += 2;
222
+ /**
223
+ *
224
+ * @returns {number}
225
+ */
226
+ readUint16BE() {
227
+ const result = this.dataView.getUint16(this.position, EndianType.BigEndian);
236
228
 
237
- return result;
238
- };
229
+ this.position += 2;
239
230
 
240
- /**
241
- *
242
- * @returns {number}
243
- */
244
- BinaryBuffer.prototype.readUint24 = function () {
245
- if (this.endianness === EndianType.BigEndian) {
246
- return this.readUint24BE();
247
- } else {
248
- return this.readUint24LE();
231
+ return result;
249
232
  }
250
- };
251
233
 
252
- /**
253
- *
254
- * @returns {number}
255
- */
256
- BinaryBuffer.prototype.readUint24LE = function () {
257
- const b0 = this.dataView.getUint8(this.position);
258
- const b1 = this.dataView.getUint8(this.position + 1);
259
- const b2 = this.dataView.getUint8(this.position + 2);
234
+ /**
235
+ *
236
+ * @returns {number}
237
+ */
238
+ readUint24() {
239
+ if (this.endianness === EndianType.BigEndian) {
240
+ return this.readUint24BE();
241
+ } else {
242
+ return this.readUint24LE();
243
+ }
244
+ }
260
245
 
261
- this.position += 3;
246
+ /**
247
+ *
248
+ * @returns {number}
249
+ */
250
+ readUint24LE() {
251
+ const b0 = this.dataView.getUint8(this.position);
252
+ const b1 = this.dataView.getUint8(this.position + 1);
253
+ const b2 = this.dataView.getUint8(this.position + 2);
262
254
 
263
- return b0 | (b1 << 8) | (b2 << 16);
264
- };
255
+ this.position += 3;
265
256
 
266
- /**
267
- *
268
- * @returns {number}
269
- */
270
- BinaryBuffer.prototype.readUint24BE = function () {
271
- const b0 = this.dataView.getUint8(this.position);
272
- const b1 = this.dataView.getUint8(this.position + 1);
273
- const b2 = this.dataView.getUint8(this.position + 2);
257
+ return b0 | (b1 << 8) | (b2 << 16);
258
+ }
274
259
 
275
- this.position += 3;
260
+ /**
261
+ *
262
+ * @returns {number}
263
+ */
264
+ readUint24BE() {
265
+ const b0 = this.dataView.getUint8(this.position);
266
+ const b1 = this.dataView.getUint8(this.position + 1);
267
+ const b2 = this.dataView.getUint8(this.position + 2);
276
268
 
277
- return b2 | (b1 << 8) | (b0 << 16);
278
- };
269
+ this.position += 3;
279
270
 
280
- /**
281
- *
282
- * @returns {number}
283
- */
284
- BinaryBuffer.prototype.readUint32 = function () {
285
- const result = this.dataView.getUint32(this.position, this.endianness);
271
+ return b2 | (b1 << 8) | (b0 << 16);
272
+ }
286
273
 
287
- this.position += 4;
274
+ /**
275
+ *
276
+ * @returns {number}
277
+ */
278
+ readUint32() {
279
+ const result = this.dataView.getUint32(this.position, this.endianness);
288
280
 
289
- return result;
290
- };
281
+ this.position += 4;
291
282
 
292
- /**
293
- *
294
- * @returns {number}
295
- */
296
- BinaryBuffer.prototype.readUint32LE = function () {
297
- const result = this.dataView.getUint32(this.position, EndianType.LittleEndian);
283
+ return result;
284
+ }
298
285
 
299
- this.position += 4;
286
+ /**
287
+ *
288
+ * @returns {number}
289
+ */
290
+ readUint32LE() {
291
+ const result = this.dataView.getUint32(this.position, EndianType.LittleEndian);
300
292
 
301
- return result;
302
- };
293
+ this.position += 4;
303
294
 
304
- /**
305
- *
306
- * @returns {number}
307
- */
308
- BinaryBuffer.prototype.readUint32BE = function () {
309
- const result = this.dataView.getUint32(this.position, EndianType.BigEndian);
295
+ return result;
296
+ }
310
297
 
311
- this.position += 4;
298
+ /**
299
+ *
300
+ * @returns {number}
301
+ */
302
+ readUint32BE() {
303
+ const result = this.dataView.getUint32(this.position, EndianType.BigEndian);
312
304
 
313
- return result;
314
- };
305
+ this.position += 4;
315
306
 
316
- /**
317
- *
318
- * @param {number} destination_offset starting index in the destination array
319
- * @param {number} length number of elements to read
320
- * @param {Uint8Array} destination
321
- */
322
- BinaryBuffer.prototype.readUint8Array = function (destination, destination_offset, length) {
323
- for (let i = 0; i < length; i++) {
324
- destination[i + destination_offset] = this.readUint8();
307
+ return result;
325
308
  }
326
- };
327
309
 
328
- /**
329
- *
330
- * @param {number} destination_offset starting index in the destination array
331
- * @param {number} length number of elements to read
332
- * @param {Uint16Array} destination
333
- */
334
- BinaryBuffer.prototype.readUint16Array = function (destination, destination_offset, length) {
335
- for (let i = 0; i < length; i++) {
336
- destination[i + destination_offset] = this.readUint16();
310
+ /**
311
+ *
312
+ * @param {number} destination_offset starting index in the destination array
313
+ * @param {number} length number of elements to read
314
+ * @param {Uint8Array} destination
315
+ */
316
+ readUint8Array(destination, destination_offset, length) {
317
+ for (let i = 0; i < length; i++) {
318
+ destination[i + destination_offset] = this.readUint8();
319
+ }
337
320
  }
338
- };
339
321
 
340
- /**
341
- *
342
- * @param {number} destination_offset starting index in the destination array
343
- * @param {number} length number of elements to read
344
- * @param {Uint32Array} destination
345
- */
346
- BinaryBuffer.prototype.readUint32Array = function (destination, destination_offset, length) {
347
- for (let i = 0; i < length; i++) {
348
- destination[i + destination_offset] = this.readUint32();
322
+ /**
323
+ *
324
+ * @param {number} destination_offset starting index in the destination array
325
+ * @param {number} length number of elements to read
326
+ * @param {Uint16Array} destination
327
+ */
328
+ readUint16Array(destination, destination_offset, length) {
329
+ for (let i = 0; i < length; i++) {
330
+ destination[i + destination_offset] = this.readUint16();
331
+ }
349
332
  }
350
- };
351
333
 
352
- /**
353
- *
354
- * @param {number} destination_offset starting index in the destination array
355
- * @param {number} length number of elements to read
356
- * @param {Int8Array} destination
357
- */
358
- BinaryBuffer.prototype.readInt8Array = function (destination, destination_offset, length) {
359
- for (let i = 0; i < length; i++) {
360
- destination[i + destination_offset] = this.readInt8();
334
+ /**
335
+ *
336
+ * @param {number} destination_offset starting index in the destination array
337
+ * @param {number} length number of elements to read
338
+ * @param {Uint32Array} destination
339
+ */
340
+ readUint32Array(destination, destination_offset, length) {
341
+ for (let i = 0; i < length; i++) {
342
+ destination[i + destination_offset] = this.readUint32();
343
+ }
361
344
  }
362
- };
363
345
 
364
- /**
365
- *
366
- * @param {number} destination_offset starting index in the destination array
367
- * @param {number} length number of elements to read
368
- * @param {Int16Array} destination
369
- */
370
- BinaryBuffer.prototype.readInt16Array = function (destination, destination_offset, length) {
371
- for (let i = 0; i < length; i++) {
372
- destination[i + destination_offset] = this.readInt16();
346
+ /**
347
+ *
348
+ * @param {number} destination_offset starting index in the destination array
349
+ * @param {number} length number of elements to read
350
+ * @param {Int8Array} destination
351
+ */
352
+ readInt8Array(destination, destination_offset, length) {
353
+ for (let i = 0; i < length; i++) {
354
+ destination[i + destination_offset] = this.readInt8();
355
+ }
373
356
  }
374
- };
375
357
 
376
- /**
377
- *
378
- * @param {number} destination_offset starting index in the destination array
379
- * @param {number} length number of elements to read
380
- * @param {Int32Array} destination
381
- */
382
- BinaryBuffer.prototype.readInt32Array = function (destination, destination_offset, length) {
383
- for (let i = 0; i < length; i++) {
384
- destination[i + destination_offset] = this.readInt32();
358
+ /**
359
+ *
360
+ * @param {number} destination_offset starting index in the destination array
361
+ * @param {number} length number of elements to read
362
+ * @param {Int16Array} destination
363
+ */
364
+ readInt16Array(destination, destination_offset, length) {
365
+ for (let i = 0; i < length; i++) {
366
+ destination[i + destination_offset] = this.readInt16();
367
+ }
385
368
  }
386
- };
387
369
 
370
+ /**
371
+ *
372
+ * @param {number} destination_offset starting index in the destination array
373
+ * @param {number} length number of elements to read
374
+ * @param {Int32Array} destination
375
+ */
376
+ readInt32Array(destination, destination_offset, length) {
377
+ for (let i = 0; i < length; i++) {
378
+ destination[i + destination_offset] = this.readInt32();
379
+ }
380
+ }
388
381
 
389
- /**
390
- *
391
- * @param {number} destination_offset starting index in the destination array
392
- * @param {number} length number of elements to read
393
- * @param {Float32Array|number[]} destination
394
- */
395
- BinaryBuffer.prototype.readFloat32Array = function (destination, destination_offset, length) {
396
- for (let i = 0; i < length; i++) {
397
- destination[i + destination_offset] = this.readFloat32();
382
+ /**
383
+ *
384
+ * @param {number} destination_offset starting index in the destination array
385
+ * @param {number} length number of elements to read
386
+ * @param {Float32Array|number[]} destination
387
+ */
388
+ readFloat32Array(destination, destination_offset, length) {
389
+ for (let i = 0; i < length; i++) {
390
+ destination[i + destination_offset] = this.readFloat32();
391
+ }
398
392
  }
399
- };
400
393
 
401
- /**
402
- *
403
- * @param {number} destination_offset starting index in the destination array
404
- * @param {number} length number of elements to read
405
- * @param {Float64Array} destination
406
- */
407
- BinaryBuffer.prototype.readFloat64Array = function (destination, destination_offset, length) {
408
- for (let i = 0; i < length; i++) {
409
- destination[i + destination_offset] = this.readFloat64();
394
+ /**
395
+ *
396
+ * @param {number} destination_offset starting index in the destination array
397
+ * @param {number} length number of elements to read
398
+ * @param {Float64Array} destination
399
+ */
400
+ readFloat64Array(destination, destination_offset, length) {
401
+ for (let i = 0; i < length; i++) {
402
+ destination[i + destination_offset] = this.readFloat64();
403
+ }
410
404
  }
411
- };
412
405
 
413
- /**
414
- *
415
- * @param {number} source_offset starting index in the source array
416
- * @param {number} length number of elements to read
417
- * @param {Float32Array|number[]} source
418
- */
419
- BinaryBuffer.prototype.writeFloat32Array = function (source, source_offset, length) {
420
- for (let i = 0; i < length; i++) {
421
- this.writeFloat32(source[i + source_offset]);
406
+ /**
407
+ *
408
+ * @param {number} source_offset starting index in the source array
409
+ * @param {number} length number of elements to read
410
+ * @param {Float32Array|number[]} source
411
+ */
412
+ writeFloat32Array(source, source_offset, length) {
413
+ for (let i = 0; i < length; i++) {
414
+ this.writeFloat32(source[i + source_offset]);
415
+ }
422
416
  }
423
- };
424
417
 
425
- /**
426
- *
427
- * @param {number} value
428
- */
429
- BinaryBuffer.prototype.writeFloat32 = function (value) {
430
- const end = this.position + 4;
431
- this.ensureCapacity(end);
418
+ /**
419
+ *
420
+ * @param {number} value
421
+ */
422
+ writeFloat32(value) {
423
+ const end = this.position + 4;
424
+ this.ensureCapacity(end);
432
425
 
433
- this.dataView.setFloat32(this.position, value, this.endianness);
426
+ this.dataView.setFloat32(this.position, value, this.endianness);
434
427
 
435
- this.position = end;
436
- };
428
+ this.position = end;
429
+ }
437
430
 
438
- /**
439
- *
440
- * @param {number} value
441
- */
442
- BinaryBuffer.prototype.writeFloat64 = function (value) {
443
- const end = this.position + 8;
444
- this.ensureCapacity(end);
431
+ /**
432
+ *
433
+ * @param {number} value
434
+ */
435
+ writeFloat64(value) {
436
+ const end = this.position + 8;
437
+ this.ensureCapacity(end);
445
438
 
446
- this.dataView.setFloat64(this.position, value, this.endianness);
439
+ this.dataView.setFloat64(this.position, value, this.endianness);
447
440
 
448
- this.position = end;
449
- };
441
+ this.position = end;
442
+ }
450
443
 
451
- /**
452
- *
453
- * @param {number} value
454
- */
455
- BinaryBuffer.prototype.writeInt8 = function (value) {
456
- const end = this.position + 1;
457
- this.ensureCapacity(end);
444
+ /**
445
+ *
446
+ * @param {number} value
447
+ */
448
+ writeInt8(value) {
449
+ const end = this.position + 1;
450
+ this.ensureCapacity(end);
458
451
 
459
- this.dataView.setInt8(this.position, value);
452
+ this.dataView.setInt8(this.position, value);
460
453
 
461
- this.position = end;
462
- };
454
+ this.position = end;
455
+ }
463
456
 
464
- /**
465
- *
466
- * @param {number} value
467
- */
468
- BinaryBuffer.prototype.writeInt16 = function (value) {
469
- const end = this.position + 2;
470
- this.ensureCapacity(end);
457
+ /**
458
+ *
459
+ * @param {number} value
460
+ */
461
+ writeInt16(value) {
462
+ const end = this.position + 2;
463
+ this.ensureCapacity(end);
471
464
 
472
- this.dataView.setInt16(this.position, value, this.endianness);
465
+ this.dataView.setInt16(this.position, value, this.endianness);
473
466
 
474
- this.position = end;
475
- };
467
+ this.position = end;
468
+ }
476
469
 
477
- /**
478
- *
479
- * @param {number} value
480
- */
481
- BinaryBuffer.prototype.writeInt32 = function (value) {
482
- const end = this.position + 4;
483
- this.ensureCapacity(end);
470
+ /**
471
+ *
472
+ * @param {number} value
473
+ */
474
+ writeInt32(value) {
475
+ const end = this.position + 4;
476
+ this.ensureCapacity(end);
484
477
 
485
- this.dataView.setInt32(this.position, value, this.endianness);
478
+ this.dataView.setInt32(this.position, value, this.endianness);
486
479
 
487
- this.position = end;
488
- };
480
+ this.position = end;
481
+ }
489
482
 
490
- /**
491
- *
492
- * @param {number} value
493
- */
494
- BinaryBuffer.prototype.writeUint8 = function (value) {
495
- const end = this.position + 1;
496
- this.ensureCapacity(end);
483
+ /**
484
+ *
485
+ * @param {number} value
486
+ */
487
+ writeUint8(value) {
488
+ const end = this.position + 1;
489
+ this.ensureCapacity(end);
497
490
 
498
- this.dataView.setUint8(this.position, value);
491
+ this.dataView.setUint8(this.position, value);
499
492
 
500
- this.position = end;
501
- };
493
+ this.position = end;
494
+ }
502
495
 
503
- /**
504
- *
505
- * @param {Uint8Array|number[]} source
506
- * @param {number} source_offset
507
- * @param {number} length
508
- */
509
- BinaryBuffer.prototype.writeUint8Array = function (source, source_offset, length) {
510
- for (let i = 0; i < length; i++) {
511
- this.writeUint8(source[source_offset + i]);
496
+ /**
497
+ *
498
+ * @param {Uint8Array|number[]} source
499
+ * @param {number} source_offset
500
+ * @param {number} length
501
+ */
502
+ writeUint8Array(source, source_offset, length) {
503
+ for (let i = 0; i < length; i++) {
504
+ this.writeUint8(source[source_offset + i]);
505
+ }
512
506
  }
513
- }
514
507
 
515
- /**
516
- *
517
- * @param {number} value
518
- */
519
- BinaryBuffer.prototype.writeUint16 = function (value) {
520
- const end = this.position + 2;
521
- this.ensureCapacity(end);
508
+ /**
509
+ *
510
+ * @param {number} value
511
+ */
512
+ writeUint16(value) {
513
+ const end = this.position + 2;
514
+ this.ensureCapacity(end);
522
515
 
523
- this.dataView.setUint16(this.position, value, this.endianness);
516
+ this.dataView.setUint16(this.position, value, this.endianness);
524
517
 
525
- this.position = end;
526
- };
518
+ this.position = end;
519
+ }
527
520
 
528
- /**
529
- *
530
- * @param {number} value
531
- */
532
- BinaryBuffer.prototype.writeUint16BE = function (value) {
533
- const end = this.position + 2;
534
- this.ensureCapacity(end);
521
+ /**
522
+ *
523
+ * @param {number} value
524
+ */
525
+ writeUint16BE(value) {
526
+ const end = this.position + 2;
527
+ this.ensureCapacity(end);
535
528
 
536
- this.dataView.setUint16(this.position, value, EndianType.BigEndian);
529
+ this.dataView.setUint16(this.position, value, EndianType.BigEndian);
537
530
 
538
- this.position = end;
539
- };
531
+ this.position = end;
532
+ }
540
533
 
541
- /**
542
- *
543
- * @param {number} value
544
- */
545
- BinaryBuffer.prototype.writeUint16LE = function (value) {
546
- const end = this.position + 2;
547
- this.ensureCapacity(end);
534
+ /**
535
+ *
536
+ * @param {number} value
537
+ */
538
+ writeUint16LE(value) {
539
+ const end = this.position + 2;
540
+ this.ensureCapacity(end);
548
541
 
549
- this.dataView.setUint16(this.position, value, EndianType.LittleEndian);
542
+ this.dataView.setUint16(this.position, value, EndianType.LittleEndian);
550
543
 
551
- this.position = end;
552
- };
544
+ this.position = end;
545
+ }
553
546
 
554
- /**
555
- *
556
- * @param {Uint16Array|number[]} source
557
- * @param {number} source_offset
558
- * @param {number} length
559
- */
560
- BinaryBuffer.prototype.writeUint16Array = function (source, source_offset, length) {
561
- for (let i = 0; i < length; i++) {
562
- this.writeUint16(source[source_offset + i]);
547
+ /**
548
+ *
549
+ * @param {Uint16Array|number[]} source
550
+ * @param {number} source_offset
551
+ * @param {number} length
552
+ */
553
+ writeUint16Array(source, source_offset, length) {
554
+ for (let i = 0; i < length; i++) {
555
+ this.writeUint16(source[source_offset + i]);
556
+ }
563
557
  }
564
- }
565
558
 
566
- /**
567
- *
568
- * @param {number} value
569
- */
570
- BinaryBuffer.prototype.writeUint24 = function (value) {
571
- if (this.endianness === EndianType.BigEndian) {
572
- this.writeUint24BE(value);
573
- } else {
574
- this.writeUint24LE(value);
559
+ /**
560
+ *
561
+ * @param {number} value
562
+ */
563
+ writeUint24(value) {
564
+ if (this.endianness === EndianType.BigEndian) {
565
+ this.writeUint24BE(value);
566
+ } else {
567
+ this.writeUint24LE(value);
568
+ }
575
569
  }
576
- };
577
570
 
578
- /**
579
- *
580
- * @param {number} value
581
- */
582
- BinaryBuffer.prototype.writeUint24BE = function (value) {
583
- const end = this.position + 3;
571
+ /**
572
+ *
573
+ * @param {number} value
574
+ */
575
+ writeUint24BE(value) {
576
+ const end = this.position + 3;
584
577
 
585
- this.ensureCapacity(end);
578
+ this.ensureCapacity(end);
586
579
 
587
- const b0 = value & 0xFF;
588
- const b1 = (value >> 8) & 0xFF;
589
- const b2 = (value >> 16) & 0xFF;
580
+ const b0 = value & 0xFF;
581
+ const b1 = (value >> 8) & 0xFF;
582
+ const b2 = (value >> 16) & 0xFF;
590
583
 
591
- this.dataView.setUint8(this.position, b2);
592
- this.dataView.setUint8(this.position + 1, b1);
593
- this.dataView.setUint8(this.position + 2, b0);
584
+ this.dataView.setUint8(this.position, b2);
585
+ this.dataView.setUint8(this.position + 1, b1);
586
+ this.dataView.setUint8(this.position + 2, b0);
594
587
 
595
- this.position = end;
596
- };
588
+ this.position = end;
589
+ }
597
590
 
598
- /**
599
- *
600
- * @param {number} value
601
- */
602
- BinaryBuffer.prototype.writeUint24LE = function (value) {
603
- const end = this.position + 3;
591
+ /**
592
+ *
593
+ * @param {number} value
594
+ */
595
+ writeUint24LE(value) {
596
+ const end = this.position + 3;
604
597
 
605
- this.ensureCapacity(end);
598
+ this.ensureCapacity(end);
606
599
 
607
- const b0 = value & 0xFF;
608
- const b1 = (value >> 8) & 0xFF;
609
- const b2 = (value >> 16) & 0xFF;
600
+ const b0 = value & 0xFF;
601
+ const b1 = (value >> 8) & 0xFF;
602
+ const b2 = (value >> 16) & 0xFF;
610
603
 
611
- this.dataView.setUint8(this.position, b0);
612
- this.dataView.setUint8(this.position + 1, b1);
613
- this.dataView.setUint8(this.position + 2, b2);
604
+ this.dataView.setUint8(this.position, b0);
605
+ this.dataView.setUint8(this.position + 1, b1);
606
+ this.dataView.setUint8(this.position + 2, b2);
614
607
 
615
- this.position = end;
616
- };
608
+ this.position = end;
609
+ }
617
610
 
618
- /**
619
- * 2^31-1, values above this will be cropped incorrectly when bit-shifting
620
- * @type {number}
621
- */
622
- const MAX_SAFE_UINT_VAR = 2147483647;
611
+ /**
612
+ * Write Uint of variable length
613
+ * NOTE: uses 7-bit encoding with 1 bit used for carry-over flag
614
+ * @param {number} value
615
+ */
616
+ writeUintVar(value) {
617
+ assert.isNonNegativeInteger(value, 'value');
618
+ assert.ok(value <= MAX_SAFE_UINT_VAR, `value=[${value}] exceeds maximum safe limit[=${MAX_SAFE_UINT_VAR}]`);
623
619
 
624
- /**
625
- * Write Uint of variable length
626
- * NOTE: uses 7-bit encoding with 1 bit used for carry-over flag
627
- * @param {number} value
628
- */
629
- BinaryBuffer.prototype.writeUintVar = function (value) {
630
- assert.isNonNegativeInteger(value, 'value');
631
- assert.ok(value <= MAX_SAFE_UINT_VAR, `value=[${value}] exceeds maximum safe limit[=${MAX_SAFE_UINT_VAR}]`);
620
+ let first = true;
632
621
 
633
- let first = true;
622
+ while (first || value !== 0) {
623
+ first = false;
634
624
 
635
- while (first || value !== 0) {
636
- first = false;
625
+ let lower7bits = (value & 0x7f);
637
626
 
638
- let lower7bits = (value & 0x7f);
627
+ value >>= 7;
639
628
 
640
- value >>= 7;
629
+ if (value > 0) {
630
+ //write carry-over flag
631
+ lower7bits |= 128;
632
+ }
641
633
 
642
- if (value > 0) {
643
- //write carry-over flag
644
- lower7bits |= 128;
634
+ this.writeUint8(lower7bits);
645
635
  }
646
-
647
- this.writeUint8(lower7bits);
648
636
  }
649
- };
650
637
 
651
- /**
652
- * Read Uint of variable length, a compliment to {@link #writeUintVar}
653
- * @returns {number}
654
- */
655
- BinaryBuffer.prototype.readUintVar = function () {
656
- let more = true;
657
- let value = 0;
658
- let shift = 0;
638
+ /**
639
+ * Read Uint of variable length, a compliment to {@link #writeUintVar}
640
+ * @returns {number}
641
+ */
642
+ readUintVar() {
643
+ let more = true;
644
+ let value = 0;
645
+ let shift = 0;
659
646
 
660
- while (more) {
661
- let lower7bits = this.readUint8();
647
+ while (more) {
648
+ let lower7bits = this.readUint8();
662
649
 
663
- //read carry-over flag
664
- more = (lower7bits & 128) !== 0;
650
+ //read carry-over flag
651
+ more = (lower7bits & 128) !== 0;
665
652
 
666
- //read value part of the byte
667
- value |= (lower7bits & 0x7f) << shift;
653
+ //read value part of the byte
654
+ value |= (lower7bits & 0x7f) << shift;
668
655
 
669
- //increment shift
670
- shift += 7;
671
- }
656
+ //increment shift
657
+ shift += 7;
658
+ }
672
659
 
673
- return value;
674
- };
660
+ return value;
661
+ }
675
662
 
663
+ /**
664
+ *
665
+ * @param {number} value
666
+ */
667
+ writeUint32(value) {
668
+ const end = this.position + 4;
669
+ this.ensureCapacity(end);
676
670
 
677
- /**
678
- *
679
- * @param {number} value
680
- */
681
- BinaryBuffer.prototype.writeUint32 = function (value) {
682
- const end = this.position + 4;
683
- this.ensureCapacity(end);
671
+ this.dataView.setUint32(this.position, value, this.endianness);
684
672
 
685
- this.dataView.setUint32(this.position, value, this.endianness);
673
+ this.position = end;
674
+ }
686
675
 
687
- this.position = end;
688
- };
676
+ /**
677
+ *
678
+ * @param {number} value
679
+ */
680
+ writeUint32BE(value) {
681
+ const end = this.position + 4;
682
+ this.ensureCapacity(end);
689
683
 
690
- /**
691
- *
692
- * @param {number} value
693
- */
694
- BinaryBuffer.prototype.writeUint32BE = function (value) {
695
- const end = this.position + 4;
696
- this.ensureCapacity(end);
684
+ this.dataView.setUint32(this.position, value, EndianType.BigEndian);
697
685
 
698
- this.dataView.setUint32(this.position, value, EndianType.BigEndian);
686
+ this.position = end;
687
+ }
699
688
 
700
- this.position = end;
701
- };
689
+ /**
690
+ *
691
+ * @param {number} value
692
+ */
693
+ writeUint32LE(value) {
694
+ const end = this.position + 4;
695
+ this.ensureCapacity(end);
702
696
 
703
- /**
704
- *
705
- * @param {number} value
706
- */
707
- BinaryBuffer.prototype.writeUint32LE = function (value) {
708
- const end = this.position + 4;
709
- this.ensureCapacity(end);
697
+ this.dataView.setUint32(this.position, value, EndianType.LittleEndian);
710
698
 
711
- this.dataView.setUint32(this.position, value, EndianType.LittleEndian);
699
+ this.position = end;
700
+ }
712
701
 
713
- this.position = end;
714
- };
702
+ /**
703
+ *
704
+ * @param {Uint8Array|Uint8ClampedArray} array
705
+ * @param {number} source_offset
706
+ * @param {number} length
707
+ */
708
+ writeBytes(array, source_offset, length) {
709
+ const source_end = source_offset + length;
710
+ assert.greaterThanOrEqual(array.length, source_end, 'source array underflow');
715
711
 
716
- /**
717
- *
718
- * @param {Uint8Array|Uint8ClampedArray} array
719
- * @param {number} source_offset
720
- * @param {number} length
721
- */
722
- BinaryBuffer.prototype.writeBytes = function (array, source_offset, length) {
723
- const source_end = source_offset + length;
724
- assert.greaterThanOrEqual(array.length, source_end, 'source array underflow');
712
+ const targetAddress = this.position;
725
713
 
726
- const targetAddress = this.position;
714
+ const end = targetAddress + length;
727
715
 
728
- const end = targetAddress + length;
716
+ this.ensureCapacity(end);
729
717
 
730
- this.ensureCapacity(end);
718
+ if (source_offset === 0 && array.length === length) {
719
+ // copying entire source array
731
720
 
732
- if (source_offset === 0 && array.length === length) {
733
- // copying entire source array
721
+ this.__data_uint8.set(array, targetAddress);
734
722
 
735
- this.__data_uint8.set(array, targetAddress);
723
+ } else if (typeof array.subarray === "function") {
724
+ // typed array, use "subarray" method
736
725
 
737
- } else if (typeof array.subarray === "function") {
738
- // typed array, use "subarray" method
726
+ this.__data_uint8.set(array.subarray(source_offset, source_end), targetAddress);
739
727
 
740
- this.__data_uint8.set(array.subarray(source_offset, source_end), targetAddress);
728
+ } else {
729
+ // not a typed array, copy byte by byte manually
741
730
 
742
- } else {
743
- // not a typed array, copy byte by byte manually
731
+ for (let i = 0; i < length; i++) {
732
+ this.__data_uint8[targetAddress + i] = array[source_offset + i];
733
+ }
744
734
 
745
- for (let i = 0; i < length; i++) {
746
- this.__data_uint8[targetAddress + i] = array[source_offset + i];
747
735
  }
748
736
 
737
+ this.position = end;
749
738
  }
750
739
 
751
- this.position = end;
752
- };
740
+ /**
741
+ *
742
+ * @param {Uint8Array} destination
743
+ * @param {number} destination_offset
744
+ * @param {number} length
745
+ */
746
+ readBytes(destination, destination_offset, length) {
747
+ const source_position = this.position;
753
748
 
754
- /**
755
- *
756
- * @param {Uint8Array} destination
757
- * @param {number} destination_offset
758
- * @param {number} length
759
- */
760
- BinaryBuffer.prototype.readBytes = function (destination, destination_offset, length) {
761
- const source_position = this.position;
749
+ const end = source_position + length;
762
750
 
763
- const end = source_position + length;
751
+ const uint8 = this.__data_uint8;
764
752
 
765
- const uint8 = this.__data_uint8;
753
+ if (length < 128) {
754
+ // small copy
755
+ array_copy(uint8, source_position, destination, destination_offset, length);
756
+ } else {
757
+ destination.set(uint8.subarray(source_position, end), destination_offset);
758
+ }
766
759
 
767
- if (length < 128) {
768
- // small copy
769
- array_copy(uint8, source_position, destination, destination_offset, length);
770
- } else {
771
- destination.set(uint8.subarray(source_position, end), destination_offset);
760
+ this.position = end;
772
761
  }
773
762
 
774
- this.position = end;
775
- };
776
-
777
-
778
- /**
779
- * Adapted from https://github.com/samthor/fast-text-encoding/blob/master/text.js
780
- * @licence Original license is Apache 2.0
781
- * @param {String} string
782
- */
783
- BinaryBuffer.prototype.writeUTF8String = function (string) {
784
- if (string === null) {
785
- //mark NULL
786
- this.writeUint32(4294967295);
787
- //bail, no string data to write
788
- return;
789
- } else if (string === undefined) {
790
- //mark undefined
791
- this.writeUint32(4294967294);
792
-
793
- return;
794
- }
763
+ /**
764
+ * Adapted from https://github.com/samthor/fast-text-encoding/blob/master/text.js
765
+ * @licence Original license is Apache 2.0
766
+ * @param {String} string
767
+ */
768
+ writeUTF8String(string) {
769
+ if (string === null) {
770
+ //mark NULL
771
+ this.writeUint32(4294967295);
772
+ //bail, no string data to write
773
+ return;
774
+ } else if (string === undefined) {
775
+ //mark undefined
776
+ this.writeUint32(4294967294);
777
+
778
+ return;
779
+ }
795
780
 
796
781
 
797
- let pos = 0;
798
- const len = string.length;
782
+ let pos = 0;
783
+ const len = string.length;
799
784
 
800
- if (len >= 4294967294) {
801
- throw new Error('String is too long');
802
- }
785
+ if (len >= 4294967294) {
786
+ throw new Error('String is too long');
787
+ }
803
788
 
804
- //mark non-NULL
805
- this.writeUint32(len);
789
+ //mark non-NULL
790
+ this.writeUint32(len);
806
791
 
807
- const startPosition = this.position;
792
+ const startPosition = this.position;
808
793
 
809
- let at = startPosition; // output position
794
+ let at = startPosition; // output position
810
795
 
811
- let tlen = Math.max(32, len + (len >> 1) + 7); // 1.5x size
796
+ let tlen = Math.max(32, len + (len >> 1) + 7); // 1.5x size
812
797
 
813
- this.ensureCapacity(tlen + at);
798
+ this.ensureCapacity(tlen + at);
814
799
 
815
- let target = this.__data_uint8; // ... but at 8 byte offset
800
+ let target = this.__data_uint8; // ... but at 8 byte offset
816
801
 
817
802
 
818
- while (pos < len) {
819
- let value = string.charCodeAt(pos++);
820
- if (value >= 0xd800 && value <= 0xdbff) {
821
- // high surrogate
822
- if (pos < len) {
823
- const extra = string.charCodeAt(pos);
824
- if ((extra & 0xfc00) === 0xdc00) {
825
- ++pos;
826
- value = ((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000;
827
- }
828
- }
803
+ while (pos < len) {
804
+ let value = string.charCodeAt(pos++);
829
805
  if (value >= 0xd800 && value <= 0xdbff) {
830
- continue; // drop lone surrogate
806
+ // high surrogate
807
+ if (pos < len) {
808
+ const extra = string.charCodeAt(pos);
809
+ if ((extra & 0xfc00) === 0xdc00) {
810
+ ++pos;
811
+ value = ((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000;
812
+ }
813
+ }
814
+ if (value >= 0xd800 && value <= 0xdbff) {
815
+ continue; // drop lone surrogate
816
+ }
831
817
  }
832
- }
833
818
 
834
- // expand the buffer if we couldn't write 4 bytes
835
- if (at + 4 > this.capacity) {
836
- tlen += 8; // minimum extra
837
- tlen *= (1.0 + (pos / len) * 2); // take 2x the remaining
838
- tlen = (tlen >> 3) << 3; // 8 byte offset
819
+ // expand the buffer if we couldn't write 4 bytes
820
+ if (at + 4 > this.capacity) {
821
+ tlen += 8; // minimum extra
822
+ tlen *= (1.0 + (pos / len) * 2); // take 2x the remaining
823
+ tlen = (tlen >> 3) << 3; // 8 byte offset
839
824
 
840
- this.ensureCapacity(tlen + startPosition);
825
+ this.ensureCapacity(tlen + startPosition);
841
826
 
842
- target = this.__data_uint8;
843
- }
827
+ target = this.__data_uint8;
828
+ }
844
829
 
845
- if ((value & 0xffffff80) === 0) { // 1-byte
846
- target[at++] = value; // ASCII
847
- continue;
848
- } else if ((value & 0xfffff800) === 0) { // 2-byte
849
- target[at++] = ((value >> 6) & 0x1f) | 0xc0;
850
- } else if ((value & 0xffff0000) === 0) { // 3-byte
851
- target[at++] = ((value >> 12) & 0x0f) | 0xe0;
852
- target[at++] = ((value >> 6) & 0x3f) | 0x80;
853
- } else if ((value & 0xffe00000) === 0) { // 4-byte
854
- target[at++] = ((value >> 18) & 0x07) | 0xf0;
855
- target[at++] = ((value >> 12) & 0x3f) | 0x80;
856
- target[at++] = ((value >> 6) & 0x3f) | 0x80;
857
- } else {
858
- // FIXME: do we care
859
- continue;
830
+ if ((value & 0xffffff80) === 0) { // 1-byte
831
+ target[at++] = value; // ASCII
832
+ continue;
833
+ } else if ((value & 0xfffff800) === 0) { // 2-byte
834
+ target[at++] = ((value >> 6) & 0x1f) | 0xc0;
835
+ } else if ((value & 0xffff0000) === 0) { // 3-byte
836
+ target[at++] = ((value >> 12) & 0x0f) | 0xe0;
837
+ target[at++] = ((value >> 6) & 0x3f) | 0x80;
838
+ } else if ((value & 0xffe00000) === 0) { // 4-byte
839
+ target[at++] = ((value >> 18) & 0x07) | 0xf0;
840
+ target[at++] = ((value >> 12) & 0x3f) | 0x80;
841
+ target[at++] = ((value >> 6) & 0x3f) | 0x80;
842
+ } else {
843
+ // FIXME: do we care
844
+ continue;
845
+ }
846
+
847
+ target[at++] = (value & 0x3f) | 0x80;
860
848
  }
861
849
 
862
- target[at++] = (value & 0x3f) | 0x80;
850
+ this.position = at;
863
851
  }
864
852
 
865
- this.position = at;
866
- };
867
-
868
- /**
869
- * Adapted from https://github.com/samthor/fast-text-encoding/blob/master/text.js
870
- * @licence Original license is Apache 2.0
871
- * @returns {String}
872
- */
873
- BinaryBuffer.prototype.readUTF8String = function () {
874
- //check for null
875
- const stringLength = this.readUint32();
876
-
877
- if (stringLength === 4294967295) {
878
- //null string
879
- return null;
880
- } else if (stringLength === 4294967294) {
881
- //undefined string
882
- return undefined;
883
- }
853
+ /**
854
+ * Adapted from https://github.com/samthor/fast-text-encoding/blob/master/text.js
855
+ * @licence Original license is Apache 2.0
856
+ * @returns {String}
857
+ */
858
+ readUTF8String() {
859
+ //check for null
860
+ const stringLength = this.readUint32();
861
+
862
+ if (stringLength === 4294967295) {
863
+ //null string
864
+ return null;
865
+ } else if (stringLength === 4294967294) {
866
+ //undefined string
867
+ return undefined;
868
+ }
884
869
 
885
- const bytes = this.__data_uint8;
870
+ const bytes = this.__data_uint8;
886
871
 
887
872
 
888
- let result = "";
873
+ let result = "";
889
874
 
890
- let i = this.position;
875
+ let i = this.position;
891
876
 
892
- let charCount = 0;
877
+ let charCount = 0;
893
878
 
894
- while (i < this.capacity && charCount < stringLength) {
895
- const byte1 = bytes[i++];
896
- let codePoint;
879
+ while (i < this.capacity && charCount < stringLength) {
880
+ const byte1 = bytes[i++];
881
+ let codePoint;
897
882
 
898
- if (byte1 === 0) {
899
- break; // NULL
900
- }
883
+ if (byte1 === 0) {
884
+ break; // NULL
885
+ }
901
886
 
902
- if ((byte1 & 0x80) === 0) { // 1-byte
903
- codePoint = byte1;
904
- } else if ((byte1 & 0xe0) === 0xc0) { // 2-byte
905
- const byte2 = bytes[i++] & 0x3f;
906
- codePoint = (((byte1 & 0x1f) << 6) | byte2);
907
- } else if ((byte1 & 0xf0) === 0xe0) {
908
- const byte2 = bytes[i++] & 0x3f;
909
- const byte3 = bytes[i++] & 0x3f;
910
- codePoint = (((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
911
- } else if ((byte1 & 0xf8) === 0xf0) {
912
- const byte2 = bytes[i++] & 0x3f;
913
- const byte3 = bytes[i++] & 0x3f;
914
- const byte4 = bytes[i++] & 0x3f;
915
-
916
- // this can be > 0xffff, so possibly generate surrogates
917
- codePoint = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
918
- if (codePoint > 0xffff) {
919
- // codepoint &= ~0x10000;
920
- codePoint -= 0x10000;
921
-
922
- result += String.fromCharCode((codePoint >>> 10) & 0x3ff | 0xd800);
923
- charCount++;
924
-
925
- codePoint = 0xdc00 | codePoint & 0x3ff;
887
+ if ((byte1 & 0x80) === 0) { // 1-byte
888
+ codePoint = byte1;
889
+ } else if ((byte1 & 0xe0) === 0xc0) { // 2-byte
890
+ const byte2 = bytes[i++] & 0x3f;
891
+ codePoint = (((byte1 & 0x1f) << 6) | byte2);
892
+ } else if ((byte1 & 0xf0) === 0xe0) {
893
+ const byte2 = bytes[i++] & 0x3f;
894
+ const byte3 = bytes[i++] & 0x3f;
895
+ codePoint = (((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
896
+ } else if ((byte1 & 0xf8) === 0xf0) {
897
+ const byte2 = bytes[i++] & 0x3f;
898
+ const byte3 = bytes[i++] & 0x3f;
899
+ const byte4 = bytes[i++] & 0x3f;
900
+
901
+ // this can be > 0xffff, so possibly generate surrogates
902
+ codePoint = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
903
+ if (codePoint > 0xffff) {
904
+ // codepoint &= ~0x10000;
905
+ codePoint -= 0x10000;
906
+
907
+ result += String.fromCharCode((codePoint >>> 10) & 0x3ff | 0xd800);
908
+ charCount++;
909
+
910
+ codePoint = 0xdc00 | codePoint & 0x3ff;
911
+ }
912
+ } else {
913
+ // FIXME: we're ignoring this
926
914
  }
927
- } else {
928
- // FIXME: we're ignoring this
915
+
916
+ charCount++;
917
+ result += String.fromCharCode(codePoint);
918
+
929
919
  }
930
920
 
931
- charCount++;
932
- result += String.fromCharCode(codePoint);
921
+ this.position = i;
933
922
 
923
+ return result;
934
924
  }
935
925
 
936
- this.position = i;
937
-
938
- return result;
939
- };
926
+ /**
927
+ *
928
+ * @param {string} string
929
+ */
930
+ writeASCIIString(string) {
931
+ const char_count = string.length;
940
932
 
941
- /**
942
- *
943
- * @param {string} string
944
- */
945
- BinaryBuffer.prototype.writeASCIIString = function (string) {
946
- const char_count = string.length;
933
+ const start = this.position;
934
+ const end = start + char_count;
947
935
 
948
- const start = this.position;
949
- const end = start + char_count;
936
+ this.ensureCapacity(end);
950
937
 
951
- this.ensureCapacity(end);
938
+ for (let i = 0; i < char_count; i++) {
939
+ const char_code = string.charCodeAt(i);
952
940
 
953
- for (let i = 0; i < char_count; i++) {
954
- const char_code = string.charCodeAt(i);
941
+ if (char_code > 0xFF) {
942
+ throw new Error(`Character ${String.fromCharCode(char_code)} can\'t be represented by a US-ASCII byte.`);
943
+ }
955
944
 
956
- if (char_code > 0xFF) {
957
- throw new Error(`Character ${String.fromCharCode(char_code)} can\'t be represented by a US-ASCII byte.`);
945
+ this.__data_uint8[start + i] = char_code;
958
946
  }
959
947
 
960
- this.__data_uint8[start + i] = char_code;
948
+ this.position = end;
961
949
  }
962
950
 
963
- this.position = end;
964
- };
951
+ /**
952
+ *
953
+ * @param {number} length
954
+ * @returns {string}
955
+ */
956
+ readASCIICharacters(length) {
957
+ let result = "";
965
958
 
966
- /**
967
- *
968
- * @param {number} length
969
- * @returns {string}
970
- */
971
- BinaryBuffer.prototype.readASCIICharacters = function (length) {
972
- let result = "";
959
+ for (let i = 0; i < length; i++) {
960
+ const code = this.readUint8();
973
961
 
974
- for (let i = 0; i < length; i++) {
975
- const code = this.readUint8();
962
+ result += String.fromCharCode(code);
963
+ }
976
964
 
977
- result += String.fromCharCode(code);
965
+ return result;
978
966
  }
979
967
 
980
- return result;
981
- };
968
+ /**
969
+ *
970
+ * @param {ArrayBuffer} v
971
+ * @return {BinaryBuffer}
972
+ */
973
+ static fromArrayBuffer(v) {
974
+ const r = new BinaryBuffer();
982
975
 
983
- /**
984
- *
985
- * @param {BinaryBuffer} source
986
- * @param {BinaryBuffer} target
987
- * @returns {string} Copied value
988
- */
989
- BinaryBuffer.copyUTF8String = function (source, target) {
990
- const v = source.readUTF8String();
976
+ r.fromArrayBuffer(v);
977
+
978
+ return r;
979
+ }
991
980
 
992
- target.writeUTF8String(v);
981
+ /**
982
+ *
983
+ * @param {BinaryBuffer} source
984
+ * @param {BinaryBuffer} target
985
+ * @returns {string} Copied value
986
+ */
987
+ static copyUTF8String(source, target) {
988
+ const v = source.readUTF8String();
993
989
 
994
- return v;
995
- };
996
- /**
997
- *
998
- * @param {BinaryBuffer} source
999
- * @param {BinaryBuffer} target
1000
- * @returns {number} Copied value
1001
- */
1002
- BinaryBuffer.copyUintVar = function (source, target) {
1003
- const v = source.readUintVar();
990
+ target.writeUTF8String(v);
1004
991
 
1005
- target.writeUintVar(v);
992
+ return v;
993
+ }
1006
994
 
1007
- return v;
1008
- };
995
+ /**
996
+ *
997
+ * @param {BinaryBuffer} source
998
+ * @param {BinaryBuffer} target
999
+ * @returns {number} Copied value
1000
+ */
1001
+ static copyUintVar(source, target) {
1002
+ const v = source.readUintVar();
1009
1003
 
1010
- /**
1011
- *
1012
- * @param {BinaryBuffer} source
1013
- * @param {BinaryBuffer} target
1014
- * @returns {number} Copied value
1015
- */
1016
- BinaryBuffer.copyUint8 = function (source, target) {
1017
- const v = source.readUint8();
1004
+ target.writeUintVar(v);
1018
1005
 
1019
- target.writeUint8(v);
1006
+ return v;
1007
+ }
1020
1008
 
1021
- return v;
1022
- };
1009
+ /**
1010
+ *
1011
+ * @param {BinaryBuffer} source
1012
+ * @param {BinaryBuffer} target
1013
+ * @returns {number} Copied value
1014
+ */
1015
+ static copyUint8(source, target) {
1016
+ const v = source.readUint8();
1023
1017
 
1024
- /**
1025
- *
1026
- * @param {BinaryBuffer} source
1027
- * @param {BinaryBuffer} target
1028
- * @returns {number} Copied value
1029
- */
1030
- BinaryBuffer.copyUint16 = function (source, target) {
1031
- const v = source.readUint16();
1018
+ target.writeUint8(v);
1032
1019
 
1033
- target.writeUint16(v);
1020
+ return v;
1021
+ }
1034
1022
 
1035
- return v;
1036
- };
1037
- /**
1038
- *
1039
- * @param {BinaryBuffer} source
1040
- * @param {BinaryBuffer} target
1041
- * @returns {number} Copied value
1042
- */
1043
- BinaryBuffer.copyUint32 = function (source, target) {
1044
- const v = source.readUint32();
1023
+ /**
1024
+ *
1025
+ * @param {BinaryBuffer} source
1026
+ * @param {BinaryBuffer} target
1027
+ * @returns {number} Copied value
1028
+ */
1029
+ static copyUint16(source, target) {
1030
+ const v = source.readUint16();
1045
1031
 
1046
- target.writeUint32(v);
1032
+ target.writeUint16(v);
1047
1033
 
1048
- return v;
1049
- };
1034
+ return v;
1035
+ }
1050
1036
 
1051
- /**
1052
- *
1053
- * @param {BinaryBuffer} source
1054
- * @param {BinaryBuffer} target
1055
- * @returns {number} Copied value
1056
- */
1057
- BinaryBuffer.copyFloat32 = function (source, target) {
1058
- const v = source.readFloat32();
1037
+ /**
1038
+ *
1039
+ * @param {BinaryBuffer} source
1040
+ * @param {BinaryBuffer} target
1041
+ * @returns {number} Copied value
1042
+ */
1043
+ static copyUint32(source, target) {
1044
+ const v = source.readUint32();
1059
1045
 
1060
- target.writeFloat32(v);
1046
+ target.writeUint32(v);
1061
1047
 
1062
- return v;
1063
- };
1048
+ return v;
1049
+ }
1064
1050
 
1065
- /**
1066
- *
1067
- * @param {BinaryBuffer} source
1068
- * @param {BinaryBuffer} target
1069
- * @returns {number} Copied value
1070
- */
1071
- BinaryBuffer.copyFloat64 = function (source, target) {
1072
- const v = source.readFloat64();
1051
+ /**
1052
+ *
1053
+ * @param {BinaryBuffer} source
1054
+ * @param {BinaryBuffer} target
1055
+ * @returns {number} Copied value
1056
+ */
1057
+ static copyFloat32(source, target) {
1058
+ const v = source.readFloat32();
1073
1059
 
1074
- target.writeFloat64(v);
1060
+ target.writeFloat32(v);
1075
1061
 
1076
- return v;
1077
- };
1062
+ return v;
1063
+ }
1078
1064
 
1079
- /**
1080
- *
1081
- * @param {BinaryBuffer} source
1082
- * @param {BinaryBuffer} target
1083
- * @param {number} length
1084
- */
1085
- BinaryBuffer.copyBytes = function (source, target, length) {
1086
- const temp = new Uint8Array(length);
1065
+ /**
1066
+ *
1067
+ * @param {BinaryBuffer} source
1068
+ * @param {BinaryBuffer} target
1069
+ * @returns {number} Copied value
1070
+ */
1071
+ static copyFloat64(source, target) {
1072
+ const v = source.readFloat64();
1087
1073
 
1088
- source.readBytes(temp, 0, length);
1074
+ target.writeFloat64(v);
1089
1075
 
1090
- target.writeBytes(temp, 0, length);
1076
+ return v;
1077
+ }
1091
1078
 
1092
- return temp;
1093
- };
1079
+ /**
1080
+ *
1081
+ * @param {BinaryBuffer} source
1082
+ * @param {BinaryBuffer} target
1083
+ * @param {number} length
1084
+ */
1085
+ static copyBytes(source, target, length) {
1086
+ const temp = new Uint8Array(length);
1087
+
1088
+ source.readBytes(temp, 0, length);
1094
1089
 
1090
+ target.writeBytes(temp, 0, length);
1095
1091
 
1096
- export { BinaryBuffer };
1092
+ return temp;
1093
+ }
1094
+ }