@aioha/tx-digest 1.0.0 → 1.0.2

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.
@@ -1,107 +1,64 @@
1
1
  /**
2
-
3
2
  * @license bytebuffer.js (c) 2015 Daniel Wirtz <dcode@dcode.io>
4
-
5
- * Backing buffer: ArrayBuffer, Accessor: DataView
6
-
3
+ * Backing buffer: ArrayBuffer
7
4
  * Released under the Apache License, Version 2.0
8
-
9
5
  * see: https://github.com/dcodeIO/bytebuffer.js for details
10
-
11
6
  * modified by @xmcl/bytebuffer
12
-
13
- * And customized for hive-tx
14
-
7
+ * And customized for hive-tx and Aioha
15
8
  */
16
9
 
17
10
  export class ByteBuffer {
18
11
  /**
19
-
20
12
  * ByteBuffer version.
21
-
22
13
  * @type {string}
23
-
24
14
  * @const
25
-
26
15
  * @expose
27
-
28
16
  */
29
-
30
17
  static VERSION = '0.0.1'
31
18
 
32
19
  /**
33
-
34
20
  * Little endian constant that can be used instead of its boolean value. Evaluates to `true`.
35
-
36
21
  * @type {boolean}
37
-
38
22
  * @const
39
-
40
23
  * @expose
41
-
42
24
  */
43
-
44
25
  static LITTLE_ENDIAN = true
45
26
 
46
27
  /**
47
-
48
- * Big endian constant that can be used instead of its boolean value. Evaluates to `false`.
49
-
50
- * @type {boolean}
51
-
52
- * @const
53
-
54
- * @expose
55
-
56
- */
57
-
28
+ * Big endian constant that can be used instead of its boolean value. Evaluates to `false`.
29
+ * @type {boolean}
30
+ * @const
31
+ * @expose
32
+ */
58
33
  static BIG_ENDIAN = false
59
34
 
60
35
  /**
61
-
62
- * Default initial capacity of `16`.
63
-
64
- * @type {number}
65
-
66
- * @expose
67
-
68
- */
69
-
36
+ * Default initial capacity of `16`.
37
+ * @type {number}
38
+ * @expose
39
+ */
70
40
  static DEFAULT_CAPACITY = 16
71
41
 
72
42
  /**
73
-
74
- * Default endianess of `false` for big endian.
75
-
76
- * @type {boolean}
77
-
78
- * @expose
79
-
80
- */
81
-
43
+ * Default endianess of `false` for big endian.
44
+ * @type {boolean}
45
+ * @expose
46
+ */
82
47
  static DEFAULT_ENDIAN = ByteBuffer.BIG_ENDIAN
83
48
 
84
49
  /**
85
-
86
- * Default no assertions flag of `false`.
87
-
88
- * @type {boolean}
89
-
90
- * @expose
91
-
92
- */
93
-
50
+ * Default no assertions flag of `false`.
51
+ * @type {boolean}
52
+ * @expose
53
+ */
94
54
  static DEFAULT_NOASSERT = false
95
55
 
96
56
  /**
97
-
98
- * Backing ArrayBuffer.
99
-
100
- * @type {!ArrayBuffer}
101
-
102
- * @expose
103
-
104
- */
57
+ * Backing ArrayBuffer.
58
+ * @type {!ArrayBuffer}
59
+ * @expose
60
+ */
61
+ buffer
105
62
 
106
63
  /**
107
64
  * Metrics representing number of bytes. Evaluates to `b`.
@@ -111,116 +68,65 @@ export class ByteBuffer {
111
68
  */
112
69
  static METRICS_BYTES = 'b'
113
70
 
114
- buffer
115
-
116
71
  /**
117
-
118
- * DataView utilized to manipulate the backing buffer. Becomes `null` if the backing buffer has a capacity of `0`.
119
-
120
- * @type {?DataView}
121
-
122
- * @expose
123
-
124
- */
125
-
72
+ * DataView utilized to manipulate the backing buffer. Becomes `null` if the backing buffer has a capacity of `0`.
73
+ * @type {?DataView}
74
+ * @expose
75
+ */
126
76
  view
127
77
 
128
78
  /**
129
-
130
- * Absolute read/write offset.
131
-
132
- * @type {number}
133
-
134
- * @expose
135
-
136
- * @see ByteBuffer#flip
137
-
138
- * @see ByteBuffer#clear
139
-
140
- */
141
-
79
+ * Absolute read/write offset.
80
+ * @type {number}
81
+ * @expose
82
+ * @see ByteBuffer#flip
83
+ * @see ByteBuffer#clear
84
+ */
142
85
  offset
143
86
 
144
87
  /**
145
-
146
- * Marked offset.
147
-
148
- * @type {number}
149
-
150
- * @expose
151
-
152
- * @see ByteBuffer#mark
153
-
154
- * @see ByteBuffer#reset
155
-
156
- */
157
-
88
+ * Marked offset.
89
+ * @type {number}
90
+ * @expose
91
+ * @see ByteBuffer#reset
92
+ */
158
93
  markedOffset
159
94
 
160
95
  /**
161
-
162
- * Absolute limit of the contained data. Set to the backing buffer's capacity upon allocation.
163
-
164
- * @type {number}
165
-
166
- * @expose
167
-
168
- * @see ByteBuffer#flip
169
-
170
- * @see ByteBuffer#clear
171
-
172
- */
173
-
96
+ * Absolute limit of the contained data. Set to the backing buffer's capacity upon allocation.
97
+ * @type {number}
98
+ * @expose
99
+ * @see ByteBuffer#flip
100
+ * @see ByteBuffer#clear
101
+ */
174
102
  limit
175
103
 
176
104
  /**
177
-
178
- * Whether to use little endian byte order, defaults to `false` for big endian.
179
-
180
- * @type {boolean}
181
-
182
- * @expose
183
-
184
- */
185
-
105
+ * Whether to use little endian byte order, defaults to `false` for big endian.
106
+ * @type {boolean}
107
+ * @expose
108
+ */
186
109
  littleEndian
187
110
 
188
111
  /**
189
-
190
- * Whether to skip assertions of offsets and values, defaults to `false`.
191
-
192
- * @type {boolean}
193
-
194
- * @expose
195
-
196
- */
197
-
112
+ * Whether to skip assertions of offsets and values, defaults to `false`.
113
+ * @type {boolean}
114
+ * @expose
115
+ */
198
116
  noAssert
199
117
 
200
118
  /**
201
-
202
- * Constructs a new ByteBuffer.
203
-
204
- * @class The swiss army knife for binary data in JavaScript.
205
-
206
- * @exports ByteBuffer
207
-
208
- * @constructor
209
-
210
- * @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
211
-
212
- * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
213
-
214
- * {@link ByteBuffer.DEFAULT_ENDIAN}.
215
-
216
- * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
217
-
218
- * {@link ByteBuffer.DEFAULT_NOASSERT}.
219
-
220
- * @expose
221
-
222
- */
223
-
119
+ * Constructs a new ByteBuffer.
120
+ * @class The swiss army knife for binary data in JavaScript.
121
+ * @exports ByteBuffer
122
+ * @constructor
123
+ * @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
124
+ * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
125
+ * {@link ByteBuffer.DEFAULT_ENDIAN}.
126
+ * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
127
+ * {@link ByteBuffer.DEFAULT_NOASSERT}.
128
+ * @expose
129
+ */
224
130
  constructor(capacity, littleEndian, noAssert) {
225
131
  if (typeof capacity === 'undefined') {
226
132
  capacity = ByteBuffer.DEFAULT_CAPACITY
@@ -236,183 +142,42 @@ export class ByteBuffer {
236
142
 
237
143
  if (!noAssert) {
238
144
  capacity = capacity | 0
239
-
240
145
  if (capacity < 0) {
241
146
  throw RangeError('Illegal capacity')
242
147
  }
243
-
244
148
  littleEndian = !!littleEndian
245
-
246
149
  noAssert = !!noAssert
247
150
  }
248
151
 
249
152
  this.buffer = capacity === 0 ? EMPTY_BUFFER : new ArrayBuffer(capacity)
250
-
251
153
  this.view = capacity === 0 ? new DataView(EMPTY_BUFFER) : new DataView(this.buffer)
252
-
253
154
  this.offset = 0
254
-
255
155
  this.markedOffset = -1
256
-
257
156
  this.limit = capacity
258
-
259
157
  this.littleEndian = littleEndian
260
-
261
158
  this.noAssert = noAssert
262
159
  }
263
160
 
264
161
  /**
265
-
266
- * Gets the accessor type.
267
-
268
- * @returns {Function} `Buffer` under node.js, `Uint8Array` respectively `DataView` in the browser (classes)
269
-
270
- * @expose
271
-
272
- */
273
-
274
- static accessor = function () {
275
- return DataView
276
- }
277
-
278
- /**
279
-
280
- * Allocates a new ByteBuffer backed by a buffer of the specified capacity.
281
-
282
- * @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
283
-
284
- * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
285
-
286
- * {@link ByteBuffer.DEFAULT_ENDIAN}.
287
-
288
- * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
289
-
290
- * {@link ByteBuffer.DEFAULT_NOASSERT}.
291
-
292
- * @returns {!ByteBuffer}
293
-
294
- * @expose
295
-
296
- */
297
-
298
- static allocate = function (capacity, littleEndian, noAssert) {
299
- return new ByteBuffer(capacity, littleEndian, noAssert)
300
- }
301
-
302
- /**
303
-
304
- * Concatenates multiple ByteBuffers into one.
305
-
306
- * @param {!Array.<!ByteBuffer|!ArrayBuffer|!Uint8Array>} buffers Buffers to concatenate
307
-
308
- * @param {(string|boolean)=} encoding String encoding if `buffers` contains a string ("base64", "hex", "binary",
309
-
310
- * defaults to "utf8")
311
-
312
- * @param {boolean=} littleEndian Whether to use little or big endian byte order for the resulting ByteBuffer. Defaults
313
-
314
- * to {@link ByteBuffer.DEFAULT_ENDIAN}.
315
-
316
- * @param {boolean=} noAssert Whether to skip assertions of offsets and values for the resulting ByteBuffer. Defaults to
317
-
318
- * {@link ByteBuffer.DEFAULT_NOASSERT}.
319
-
320
- * @returns {!ByteBuffer} Concatenated ByteBuffer
321
-
322
- * @expose
323
-
324
- */
325
-
326
- static concat = function (buffers, littleEndian, noAssert) {
327
- let capacity = 0
328
-
329
- const k = buffers.length
330
-
331
- let length
332
-
333
- for (let i2 = 0, length2; i2 < k; ++i2) {
334
- const buf = buffers[i2]
335
-
336
- if (!(buf instanceof ByteBuffer)) {
337
- buffers[i2] = ByteBuffer.wrap(buf)
338
- }
339
-
340
- length2 = buffers[i2].limit - buffers[i2].offset
341
-
342
- if (length2 > 0) {
343
- capacity += length2
344
- }
345
- }
346
-
347
- if (capacity === 0) {
348
- return new ByteBuffer(0, littleEndian, noAssert)
349
- }
350
-
351
- const bb = new ByteBuffer(capacity, littleEndian, noAssert)
352
-
353
- let bi
354
-
355
- const view = new Uint8Array(bb.buffer)
356
-
357
- let i = 0
358
-
359
- while (i < k) {
360
- bi = buffers[i++]
361
-
362
- length = bi.limit - bi.offset
363
-
364
- if (length <= 0) {
365
- continue
366
- }
367
-
368
- view.set(new Uint8Array(bi.buffer).subarray(bi.offset, bi.limit), bb.offset)
369
-
370
- bb.offset += length
371
- }
372
-
373
- bb.limit = bb.offset
374
-
375
- bb.offset = 0
376
-
377
- return bb
378
- }
379
-
380
- /**
381
-
382
- * Gets the backing buffer type.
383
-
384
- * @returns {Function} `Buffer` under node.js, `ArrayBuffer` in the browser (classes)
385
-
386
- * @expose
387
-
388
- */
389
-
162
+ * Gets the backing buffer type.
163
+ * @returns {Function} `Buffer` under node.js, `ArrayBuffer` in the browser (classes)
164
+ * @expose
165
+ */
390
166
  static type = function () {
391
167
  return ArrayBuffer
392
168
  }
393
169
 
394
170
  /**
395
-
396
- * Wraps a buffer or a string. Sets the allocated ByteBuffer's {@link ByteBuffer#offset} to `0` and its
397
-
398
- * {@link ByteBuffer#limit} to the length of the wrapped data.
399
-
400
- * @param {!ByteBuffer|!ArrayBuffer|!Uint8Array|string|!Array.<number>} buffer Anything that can be wrapped
401
-
402
- * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
403
-
404
- * {@link ByteBuffer.DEFAULT_ENDIAN}.
405
-
406
- * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
407
-
408
- * {@link ByteBuffer.DEFAULT_NOASSERT}.
409
-
410
- * @returns {!ByteBuffer} A ByteBuffer wrapping `buffer`
411
-
412
- * @expose
413
-
414
- */
415
-
171
+ * Wraps a buffer or a string. Sets the allocated ByteBuffer's {@link ByteBuffer#offset} to `0` and its
172
+ * {@link ByteBuffer#limit} to the length of the wrapped data.
173
+ * @param {!ByteBuffer|!ArrayBuffer|!Uint8Array|string|!Array.<number>} buffer Anything that can be wrapped
174
+ * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
175
+ * {@link ByteBuffer.DEFAULT_ENDIAN}.
176
+ * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
177
+ * {@link ByteBuffer.DEFAULT_NOASSERT}.
178
+ * @returns {!ByteBuffer} A ByteBuffer wrapping `buffer`
179
+ * @expose
180
+ */
416
181
  static wrap = function (buffer, littleEndian, noAssert) {
417
182
  if (buffer === null || typeof buffer !== 'object') {
418
183
  throw TypeError('Illegal buffer')
@@ -433,55 +198,54 @@ export class ByteBuffer {
433
198
 
434
199
  if (buffer.length > 0) {
435
200
  bb.buffer = buffer.buffer
436
-
437
201
  bb.offset = buffer.byteOffset
438
-
439
202
  bb.limit = buffer.byteOffset + buffer.byteLength
440
-
441
203
  bb.view = new DataView(buffer.buffer)
442
204
  }
443
205
  } else if (buffer instanceof ArrayBuffer) {
444
206
  bb = new ByteBuffer(0, littleEndian, noAssert)
445
-
446
207
  if (buffer.byteLength > 0) {
447
208
  bb.buffer = buffer
448
-
449
209
  bb.offset = 0
450
-
451
210
  bb.limit = buffer.byteLength
452
-
453
211
  bb.view = buffer.byteLength > 0 ? new DataView(buffer) : new DataView(EMPTY_BUFFER)
454
212
  }
455
213
  } else if (Object.prototype.toString.call(buffer) === '[object Array]') {
456
214
  bb = new ByteBuffer(buffer.length, littleEndian, noAssert)
457
-
458
215
  bb.limit = buffer.length
459
-
460
216
  for (let i = 0; i < buffer.length; ++i) {
461
217
  bb.view.setUint8(i, buffer[i])
462
218
  }
463
219
  } else {
464
220
  throw TypeError('Illegal buffer')
465
221
  }
466
-
467
222
  return bb
468
223
  }
469
224
 
470
225
  /**
226
+ * Writes a payload of bytes. This is an alias of {@link ByteBuffer#append}.
227
+ * @function
228
+ * @param {!ByteBuffer|!ArrayBuffer|!Uint8Array|string} source Data to write. If `source` is a ByteBuffer, its offsets
229
+ * will be modified according to the performed read operation.
230
+ * @param {(string|number)=} encoding Encoding if `data` is a string ("base64", "hex", "binary", defaults to "utf8")
231
+ * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
232
+ * written if omitted.
233
+ * @returns {!ByteBuffer} this
234
+ * @expose
235
+ */
236
+ writeBytes = this.append
471
237
 
472
- * Reads the specified number of bytes.
473
-
474
- * @param {number} length Number of bytes to read
475
-
476
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `length` if omitted.
477
-
478
- * @returns {!ByteBuffer}
479
-
480
- * @expose
238
+ // types/ints/int8
481
239
 
482
- */
240
+ /**
241
+ * Writes an 8bit signed integer.
242
+ * @param {number} value Value to write
243
+ * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
244
+ * @returns {!ByteBuffer} this
245
+ * @expose
246
+ */
483
247
 
484
- readBytes(length, offset) {
248
+ writeInt8(value, offset) {
485
249
  const relative = typeof offset === 'undefined'
486
250
 
487
251
  if (relative) {
@@ -489,67 +253,60 @@ export class ByteBuffer {
489
253
  }
490
254
 
491
255
  if (!this.noAssert) {
256
+ if (typeof value !== 'number' || value % 1 !== 0) {
257
+ throw TypeError('Illegal value: ' + value + ' (not an integer)')
258
+ }
259
+
260
+ value |= 0
261
+
492
262
  if (typeof offset !== 'number' || offset % 1 !== 0) {
493
263
  throw TypeError('Illegal offset: ' + offset + ' (not an integer)')
494
264
  }
495
265
 
496
266
  offset >>>= 0
497
267
 
498
- if (offset < 0 || offset + length > this.buffer.byteLength) {
499
- throw RangeError('Illegal offset: 0 <= ' + offset + ' (+' + length + ') <= ' + this.buffer.byteLength)
268
+ if (offset < 0 || offset + 0 > this.buffer.byteLength) {
269
+ throw RangeError('Illegal offset: 0 <= ' + offset + ' (+0) <= ' + this.buffer.byteLength)
500
270
  }
501
271
  }
502
272
 
503
- const slice = this.slice(offset, offset + length)
273
+ offset += 1
504
274
 
505
- if (relative) {
506
- this.offset += length
275
+ let capacity0 = this.buffer.byteLength
276
+
277
+ if (offset > capacity0) {
278
+ this.resize((capacity0 *= 2) > offset ? capacity0 : offset)
507
279
  }
508
280
 
509
- return slice
510
- }
281
+ offset -= 1
511
282
 
512
- /**
513
-
514
- * Writes a payload of bytes. This is an alias of {@link ByteBuffer#append}.
515
-
516
- * @function
517
-
518
- * @param {!ByteBuffer|!ArrayBuffer|!Uint8Array|string} source Data to write. If `source` is a ByteBuffer, its offsets
519
-
520
- * will be modified according to the performed read operation.
521
-
522
- * @param {(string|number)=} encoding Encoding if `data` is a string ("base64", "hex", "binary", defaults to "utf8")
523
-
524
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
525
-
526
- * written if omitted.
527
-
528
- * @returns {!ByteBuffer} this
529
-
530
- * @expose
531
-
532
- */
283
+ this.view.setInt8(offset, value)
533
284
 
534
- writeBytes = this.append
285
+ if (relative) {
286
+ this.offset += 1
287
+ }
535
288
 
536
- // types/ints/int8
289
+ return this
290
+ }
537
291
 
538
292
  /**
293
+ * Writes an 8bit signed integer. This is an alias of {@link ByteBuffer#writeInt8}.
294
+ * @function
295
+ * @param {number} value Value to write
296
+ * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
297
+ * @returns {!ByteBuffer} this
298
+ * @expose
299
+ */
300
+ writeByte = this.writeInt8
539
301
 
540
- * Writes an 8bit signed integer.
541
-
542
- * @param {number} value Value to write
543
-
544
- * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
545
-
546
- * @returns {!ByteBuffer} this
547
-
548
- * @expose
549
-
550
- */
551
-
552
- writeInt8(value, offset) {
302
+ /**
303
+ * Writes an 8bit unsigned integer.
304
+ * @param {number} value Value to write
305
+ * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
306
+ * @returns {!ByteBuffer} this
307
+ * @expose
308
+ */
309
+ writeUint8(value, offset) {
553
310
  const relative = typeof offset === 'undefined'
554
311
 
555
312
  if (relative) {
@@ -561,7 +318,7 @@ export class ByteBuffer {
561
318
  throw TypeError('Illegal value: ' + value + ' (not an integer)')
562
319
  }
563
320
 
564
- value |= 0
321
+ value >>>= 0
565
322
 
566
323
  if (typeof offset !== 'number' || offset % 1 !== 0) {
567
324
  throw TypeError('Illegal offset: ' + offset + ' (not an integer)')
@@ -576,15 +333,15 @@ export class ByteBuffer {
576
333
 
577
334
  offset += 1
578
335
 
579
- let capacity0 = this.buffer.byteLength
336
+ let capacity1 = this.buffer.byteLength
580
337
 
581
- if (offset > capacity0) {
582
- this.resize((capacity0 *= 2) > offset ? capacity0 : offset)
338
+ if (offset > capacity1) {
339
+ this.resize((capacity1 *= 2) > offset ? capacity1 : offset)
583
340
  }
584
341
 
585
342
  offset -= 1
586
343
 
587
- this.view.setInt8(offset, value)
344
+ this.view.setUint8(offset, value)
588
345
 
589
346
  if (relative) {
590
347
  this.offset += 1
@@ -594,36 +351,26 @@ export class ByteBuffer {
594
351
  }
595
352
 
596
353
  /**
354
+ * Writes an 8bit unsigned integer. This is an alias of {@link ByteBuffer#writeUint8}.
355
+ * @function
356
+ * @param {number} value Value to write
357
+ * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
358
+ * @returns {!ByteBuffer} this
359
+ * @expose
360
+ */
361
+ writeUInt8 = this.writeUint8
597
362
 
598
- * Writes an 8bit signed integer. This is an alias of {@link ByteBuffer#writeInt8}.
599
-
600
- * @function
601
-
602
- * @param {number} value Value to write
603
-
604
- * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
605
-
606
- * @returns {!ByteBuffer} this
607
-
608
- * @expose
609
-
610
- */
611
-
612
- writeByte = this.writeInt8
363
+ // types/ints/int16
613
364
 
614
365
  /**
615
-
616
- * Reads an 8bit signed integer.
617
-
618
- * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
619
-
620
- * @returns {number} Value read
621
-
622
- * @expose
623
-
624
- */
625
-
626
- readInt8(offset) {
366
+ * Writes a 16bit signed integer.
367
+ * @param {number} value Value to write
368
+ * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
369
+ * @throws {TypeError} If `offset` or `value` is not a valid number
370
+ * @throws {RangeError} If `offset` is out of bounds
371
+ * @expose
372
+ */
373
+ writeInt16(value, offset) {
627
374
  const relative = typeof offset === 'undefined'
628
375
 
629
376
  if (relative) {
@@ -631,57 +378,62 @@ export class ByteBuffer {
631
378
  }
632
379
 
633
380
  if (!this.noAssert) {
381
+ if (typeof value !== 'number' || value % 1 !== 0) {
382
+ throw TypeError('Illegal value: ' + value + ' (not an integer)')
383
+ }
384
+
385
+ value |= 0
386
+
634
387
  if (typeof offset !== 'number' || offset % 1 !== 0) {
635
388
  throw TypeError('Illegal offset: ' + offset + ' (not an integer)')
636
389
  }
637
390
 
638
391
  offset >>>= 0
639
392
 
640
- if (offset < 0 || offset + 1 > this.buffer.byteLength) {
641
- throw RangeError('Illegal offset: 0 <= ' + offset + ' (+1) <= ' + this.buffer.byteLength)
393
+ if (offset < 0 || offset + 0 > this.buffer.byteLength) {
394
+ throw RangeError('Illegal offset: 0 <= ' + offset + ' (+0) <= ' + this.buffer.byteLength)
642
395
  }
643
396
  }
644
397
 
645
- const value = this.view.getInt8(offset)
646
-
647
- if (relative) {
648
- this.offset += 1
649
- }
650
-
651
- return value
652
- }
653
-
654
- /**
655
-
656
- * Reads an 8bit signed integer. This is an alias of {@link ByteBuffer#readInt8}.
398
+ offset += 2
657
399
 
658
- * @function
400
+ let capacity2 = this.buffer.byteLength
659
401
 
660
- * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
402
+ if (offset > capacity2) {
403
+ this.resize((capacity2 *= 2) > offset ? capacity2 : offset)
404
+ }
661
405
 
662
- * @returns {number} Value read
406
+ offset -= 2
663
407
 
664
- * @expose
408
+ this.view.setInt16(offset, value, this.littleEndian)
665
409
 
666
- */
410
+ if (relative) {
411
+ this.offset += 2
412
+ }
667
413
 
668
- readByte = this.readInt8
414
+ return this
415
+ }
669
416
 
670
417
  /**
418
+ * Writes a 16bit signed integer. This is an alias of {@link ByteBuffer#writeInt16}.
419
+ * @function
420
+ * @param {number} value Value to write
421
+ * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
422
+ * @throws {TypeError} If `offset` or `value` is not a valid number
423
+ * @throws {RangeError} If `offset` is out of bounds
424
+ * @expose
425
+ */
426
+ writeShort = this.writeInt16
671
427
 
672
- * Writes an 8bit unsigned integer.
673
-
674
- * @param {number} value Value to write
675
-
676
- * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
677
-
678
- * @returns {!ByteBuffer} this
679
-
680
- * @expose
681
-
682
- */
683
-
684
- writeUint8(value, offset) {
428
+ /**
429
+ * Writes a 16bit unsigned integer.
430
+ * @param {number} value Value to write
431
+ * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
432
+ * @throws {TypeError} If `offset` or `value` is not a valid number
433
+ * @throws {RangeError} If `offset` is out of bounds
434
+ * @expose
435
+ */
436
+ writeUint16(value, offset) {
685
437
  const relative = typeof offset === 'undefined'
686
438
 
687
439
  if (relative) {
@@ -706,118 +458,46 @@ export class ByteBuffer {
706
458
  }
707
459
  }
708
460
 
709
- offset += 1
461
+ offset += 2
710
462
 
711
- let capacity1 = this.buffer.byteLength
463
+ let capacity3 = this.buffer.byteLength
712
464
 
713
- if (offset > capacity1) {
714
- this.resize((capacity1 *= 2) > offset ? capacity1 : offset)
465
+ if (offset > capacity3) {
466
+ this.resize((capacity3 *= 2) > offset ? capacity3 : offset)
715
467
  }
716
468
 
717
- offset -= 1
469
+ offset -= 2
718
470
 
719
- this.view.setUint8(offset, value)
471
+ this.view.setUint16(offset, value, this.littleEndian)
720
472
 
721
473
  if (relative) {
722
- this.offset += 1
474
+ this.offset += 2
723
475
  }
724
476
 
725
477
  return this
726
478
  }
727
479
 
728
480
  /**
481
+ * Writes a 16bit unsigned integer. This is an alias of {@link ByteBuffer#writeUint16}.
482
+ * @function
483
+ * @param {number} value Value to write
484
+ * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
485
+ * @throws {TypeError} If `offset` or `value` is not a valid number
486
+ * @throws {RangeError} If `offset` is out of bounds
487
+ * @expose
488
+ */
489
+ writeUInt16 = this.writeUint16
729
490
 
730
- * Writes an 8bit unsigned integer. This is an alias of {@link ByteBuffer#writeUint8}.
731
-
732
- * @function
733
-
734
- * @param {number} value Value to write
735
-
736
- * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
737
-
738
- * @returns {!ByteBuffer} this
739
-
740
- * @expose
741
-
742
- */
743
-
744
- writeUInt8 = this.writeUint8
745
-
746
- /**
747
-
748
- * Reads an 8bit unsigned integer.
749
-
750
- * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
751
-
752
- * @returns {number} Value read
753
-
754
- * @expose
755
-
756
- */
757
-
758
- readUint8(offset) {
759
- const relative = typeof offset === 'undefined'
760
-
761
- if (relative) {
762
- offset = this.offset
763
- }
764
-
765
- if (!this.noAssert) {
766
- if (typeof offset !== 'number' || offset % 1 !== 0) {
767
- throw TypeError('Illegal offset: ' + offset + ' (not an integer)')
768
- }
769
-
770
- offset >>>= 0
771
-
772
- if (offset < 0 || offset + 1 > this.buffer.byteLength) {
773
- throw RangeError('Illegal offset: 0 <= ' + offset + ' (+1) <= ' + this.buffer.byteLength)
774
- }
775
- }
776
-
777
- const value = this.view.getUint8(offset)
778
-
779
- if (relative) {
780
- this.offset += 1
781
- }
782
-
783
- return value
784
- }
785
-
786
- /**
787
-
788
- * Reads an 8bit unsigned integer. This is an alias of {@link ByteBuffer#readUint8}.
789
-
790
- * @function
791
-
792
- * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
793
-
794
- * @returns {number} Value read
795
-
796
- * @expose
797
-
798
- */
799
-
800
- readUInt8 = this.readUint8
801
-
802
- // types/ints/int16
491
+ // types/ints/int32
803
492
 
804
493
  /**
494
+ * Writes a 32bit unsigned integer.
495
+ * @param {number} value Value to write
496
+ * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
497
+ * @expose
498
+ */
805
499
 
806
- * Writes a 16bit signed integer.
807
-
808
- * @param {number} value Value to write
809
-
810
- * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
811
-
812
- * @throws {TypeError} If `offset` or `value` is not a valid number
813
-
814
- * @throws {RangeError} If `offset` is out of bounds
815
-
816
- * @expose
817
-
818
- */
819
-
820
- writeInt16(value, offset) {
500
+ writeUint32(value, offset) {
821
501
  const relative = typeof offset === 'undefined'
822
502
 
823
503
  if (relative) {
@@ -829,7 +509,7 @@ export class ByteBuffer {
829
509
  throw TypeError('Illegal value: ' + value + ' (not an integer)')
830
510
  }
831
511
 
832
- value |= 0
512
+ value >>>= 0
833
513
 
834
514
  if (typeof offset !== 'number' || offset % 1 !== 0) {
835
515
  throw TypeError('Illegal offset: ' + offset + ' (not an integer)')
@@ -842,65 +522,51 @@ export class ByteBuffer {
842
522
  }
843
523
  }
844
524
 
845
- offset += 2
525
+ offset += 4
846
526
 
847
- let capacity2 = this.buffer.byteLength
527
+ let capacity5 = this.buffer.byteLength
848
528
 
849
- if (offset > capacity2) {
850
- this.resize((capacity2 *= 2) > offset ? capacity2 : offset)
529
+ if (offset > capacity5) {
530
+ this.resize((capacity5 *= 2) > offset ? capacity5 : offset)
851
531
  }
852
532
 
853
- offset -= 2
533
+ offset -= 4
854
534
 
855
- this.view.setInt16(offset, value, this.littleEndian)
535
+ this.view.setUint32(offset, value, this.littleEndian)
856
536
 
857
537
  if (relative) {
858
- this.offset += 2
538
+ this.offset += 4
859
539
  }
860
540
 
861
541
  return this
862
542
  }
863
543
 
864
544
  /**
865
-
866
- * Writes a 16bit signed integer. This is an alias of {@link ByteBuffer#writeInt16}.
867
-
868
- * @function
869
-
870
- * @param {number} value Value to write
871
-
872
- * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
873
-
874
- * @throws {TypeError} If `offset` or `value` is not a valid number
875
-
876
- * @throws {RangeError} If `offset` is out of bounds
877
-
878
- * @expose
879
-
880
- */
881
-
882
- writeShort = this.writeInt16
545
+ * Writes a 32bit unsigned integer. This is an alias of {@link ByteBuffer#writeUint32}.
546
+ * @function
547
+ * @param {number} value Value to write
548
+ * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
549
+ * @expose
550
+ */
551
+ writeUInt32 = this.writeUint32
883
552
 
884
553
  /**
885
-
886
- * Reads a 16bit signed integer.
887
-
888
- * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
889
-
890
- * @returns {number} Value read
891
-
892
- * @throws {TypeError} If `offset` is not a valid number
893
-
894
- * @throws {RangeError} If `offset` is out of bounds
895
-
896
- * @expose
897
-
898
- */
899
-
900
- readInt16(offset) {
554
+ * Appends some data to this ByteBuffer. This will overwrite any contents behind the specified offset up to the appended
555
+ * data's length.
556
+ * @param {!ByteBuffer|!ArrayBuffer|!Uint8Array} source Data to append. If `source` is a ByteBuffer, its offsets
557
+ * will be modified according to the performed read operation.
558
+ * @param {(string|number)=} encoding Encoding if `data` is a string ("base64", "hex", "binary", defaults to "utf8")
559
+ * @param {number=} offset Offset to append at. Will use and increase {@link ByteBuffer#offset} by the number of bytes
560
+ * written if omitted.
561
+ * @returns {!ByteBuffer} this
562
+ * @expose
563
+ * @example A relative `<01 02>03.append(<04 05>)` will result in `<01 02 04 05>, 04 05|`
564
+ * @example An absolute `<01 02>03.append(04 05>, 1)` will result in `<01 04>05, 04 05|`
565
+ */
566
+ append(source, offset) {
901
567
  const relative = typeof offset === 'undefined'
902
568
 
903
- if (typeof offset === 'undefined') {
569
+ if (relative) {
904
570
  offset = this.offset
905
571
  }
906
572
 
@@ -911,1649 +577,141 @@ export class ByteBuffer {
911
577
 
912
578
  offset >>>= 0
913
579
 
914
- if (offset < 0 || offset + 2 > this.buffer.byteLength) {
915
- throw RangeError('Illegal offset: 0 <= ' + offset + ' (+2) <= ' + this.buffer.byteLength)
580
+ if (offset < 0 || offset + 0 > this.buffer.byteLength) {
581
+ throw RangeError('Illegal offset: 0 <= ' + offset + ' (+0) <= ' + this.buffer.byteLength)
916
582
  }
917
583
  }
918
584
 
919
- const value = this.view.getInt16(offset, this.littleEndian)
920
-
921
- if (relative) {
922
- this.offset += 2
585
+ if (!(source instanceof ByteBuffer)) {
586
+ source = ByteBuffer.wrap(source)
923
587
  }
924
588
 
925
- return value
926
- }
927
-
928
- /**
589
+ const length = source.limit - source.offset
929
590
 
930
- * Reads a 16bit signed integer. This is an alias of {@link ByteBuffer#readInt16}.
591
+ if (length <= 0) {
592
+ return this
593
+ }
931
594
 
932
- * @function
595
+ offset += length
933
596
 
934
- * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
597
+ let capacity16 = this.buffer.byteLength
935
598
 
936
- * @returns {number} Value read
599
+ if (offset > capacity16) {
600
+ this.resize((capacity16 *= 2) > offset ? capacity16 : offset)
601
+ }
937
602
 
938
- * @throws {TypeError} If `offset` is not a valid number
603
+ offset -= length
939
604
 
940
- * @throws {RangeError} If `offset` is out of bounds
605
+ new Uint8Array(this.buffer, offset).set(new Uint8Array(source.buffer).subarray(source.offset, source.limit))
941
606
 
942
- * @expose
607
+ source.offset += length
943
608
 
944
- */
609
+ if (relative) {
610
+ this.offset += length
611
+ }
945
612
 
946
- readShort = this.readInt16
613
+ return this
614
+ }
947
615
 
948
616
  /**
949
-
950
- * Writes a 16bit unsigned integer.
951
-
952
- * @param {number} value Value to write
953
-
954
- * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
955
-
956
- * @throws {TypeError} If `offset` or `value` is not a valid number
957
-
958
- * @throws {RangeError} If `offset` is out of bounds
959
-
960
- * @expose
961
-
962
- */
963
-
964
- writeUint16(value, offset) {
965
- const relative = typeof offset === 'undefined'
966
-
967
- if (relative) {
968
- offset = this.offset
969
- }
970
-
971
- if (!this.noAssert) {
972
- if (typeof value !== 'number' || value % 1 !== 0) {
973
- throw TypeError('Illegal value: ' + value + ' (not an integer)')
974
- }
975
-
976
- value >>>= 0
977
-
978
- if (typeof offset !== 'number' || offset % 1 !== 0) {
979
- throw TypeError('Illegal offset: ' + offset + ' (not an integer)')
980
- }
981
-
982
- offset >>>= 0
983
-
984
- if (offset < 0 || offset + 0 > this.buffer.byteLength) {
985
- throw RangeError('Illegal offset: 0 <= ' + offset + ' (+0) <= ' + this.buffer.byteLength)
986
- }
987
- }
988
-
989
- offset += 2
990
-
991
- let capacity3 = this.buffer.byteLength
992
-
993
- if (offset > capacity3) {
994
- this.resize((capacity3 *= 2) > offset ? capacity3 : offset)
995
- }
996
-
997
- offset -= 2
998
-
999
- this.view.setUint16(offset, value, this.littleEndian)
1000
-
1001
- if (relative) {
1002
- this.offset += 2
1003
- }
1004
-
1005
- return this
1006
- }
1007
-
1008
- /**
1009
-
1010
- * Writes a 16bit unsigned integer. This is an alias of {@link ByteBuffer#writeUint16}.
1011
-
1012
- * @function
1013
-
1014
- * @param {number} value Value to write
1015
-
1016
- * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
1017
-
1018
- * @throws {TypeError} If `offset` or `value` is not a valid number
1019
-
1020
- * @throws {RangeError} If `offset` is out of bounds
1021
-
1022
- * @expose
1023
-
1024
- */
1025
-
1026
- writeUInt16 = this.writeUint16
1027
-
1028
- /**
1029
-
1030
- * Reads a 16bit unsigned integer.
1031
-
1032
- * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
1033
-
1034
- * @returns {number} Value read
1035
-
1036
- * @throws {TypeError} If `offset` is not a valid number
1037
-
1038
- * @throws {RangeError} If `offset` is out of bounds
1039
-
1040
- * @expose
1041
-
1042
- */
1043
-
1044
- readUint16(offset) {
1045
- const relative = typeof offset === 'undefined'
1046
-
1047
- if (relative) {
1048
- offset = this.offset
1049
- }
1050
-
1051
- if (!this.noAssert) {
1052
- if (typeof offset !== 'number' || offset % 1 !== 0) {
1053
- throw TypeError('Illegal offset: ' + offset + ' (not an integer)')
1054
- }
1055
-
1056
- offset >>>= 0
1057
-
1058
- if (offset < 0 || offset + 2 > this.buffer.byteLength) {
1059
- throw RangeError('Illegal offset: 0 <= ' + offset + ' (+2) <= ' + this.buffer.byteLength)
1060
- }
1061
- }
1062
-
1063
- const value = this.view.getUint16(offset, this.littleEndian)
1064
-
1065
- if (relative) {
1066
- this.offset += 2
1067
- }
1068
-
1069
- return value
1070
- }
1071
-
1072
- /**
1073
-
1074
- * Reads a 16bit unsigned integer. This is an alias of {@link ByteBuffer#readUint16}.
1075
-
1076
- * @function
1077
-
1078
- * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
1079
-
1080
- * @returns {number} Value read
1081
-
1082
- * @throws {TypeError} If `offset` is not a valid number
1083
-
1084
- * @throws {RangeError} If `offset` is out of bounds
1085
-
1086
- * @expose
1087
-
1088
- */
1089
-
1090
- readUInt16 = this.readUint16
1091
-
1092
- // types/ints/int32
1093
-
1094
- /**
1095
-
1096
- * Writes a 32bit signed integer.
1097
-
1098
- * @param {number} value Value to write
1099
-
1100
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
1101
-
1102
- * @expose
1103
-
1104
- */
1105
-
1106
- writeInt32(value, offset) {
1107
- const relative = typeof offset === 'undefined'
1108
-
1109
- if (relative) {
1110
- offset = this.offset
1111
- }
1112
-
1113
- if (!this.noAssert) {
1114
- if (typeof value !== 'number' || value % 1 !== 0) {
1115
- throw TypeError('Illegal value: ' + value + ' (not an integer)')
1116
- }
1117
-
1118
- value |= 0
1119
-
1120
- if (typeof offset !== 'number' || offset % 1 !== 0) {
1121
- throw TypeError('Illegal offset: ' + offset + ' (not an integer)')
1122
- }
1123
-
1124
- offset >>>= 0
1125
-
1126
- if (offset < 0 || offset + 0 > this.buffer.byteLength) {
1127
- throw RangeError('Illegal offset: 0 <= ' + offset + ' (+0) <= ' + this.buffer.byteLength)
1128
- }
1129
- }
1130
-
1131
- offset += 4
1132
-
1133
- let capacity4 = this.buffer.byteLength
1134
-
1135
- if (offset > capacity4) {
1136
- this.resize((capacity4 *= 2) > offset ? capacity4 : offset)
1137
- }
1138
-
1139
- offset -= 4
1140
-
1141
- this.view.setInt32(offset, value, this.littleEndian)
1142
-
1143
- if (relative) {
1144
- this.offset += 4
1145
- }
1146
-
1147
- return this
1148
- }
1149
-
1150
- /**
1151
-
1152
- * Writes a 32bit signed integer. This is an alias of {@link ByteBuffer#writeInt32}.
1153
-
1154
- * @param {number} value Value to write
1155
-
1156
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
1157
-
1158
- * @expose
1159
-
1160
- */
1161
-
1162
- writeInt = this.writeInt32
1163
-
1164
- /**
1165
-
1166
- * Reads a 32bit signed integer.
1167
-
1168
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
1169
-
1170
- * @returns {number} Value read
1171
-
1172
- * @expose
1173
-
1174
- */
1175
-
1176
- readInt32(offset) {
1177
- const relative = typeof offset === 'undefined'
1178
-
1179
- if (relative) {
1180
- offset = this.offset
1181
- }
1182
-
1183
- if (!this.noAssert) {
1184
- if (typeof offset !== 'number' || offset % 1 !== 0) {
1185
- throw TypeError('Illegal offset: ' + offset + ' (not an integer)')
1186
- }
1187
-
1188
- offset >>>= 0
1189
-
1190
- if (offset < 0 || offset + 4 > this.buffer.byteLength) {
1191
- throw RangeError('Illegal offset: 0 <= ' + offset + ' (+4) <= ' + this.buffer.byteLength)
1192
- }
1193
- }
1194
-
1195
- const value = this.view.getInt32(offset, this.littleEndian)
1196
-
1197
- if (relative) {
1198
- this.offset += 4
1199
- }
1200
-
1201
- return value
1202
- }
1203
-
1204
- /**
1205
-
1206
- * Reads a 32bit signed integer. This is an alias of {@link ByteBuffer#readInt32}.
1207
-
1208
- * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `4` if omitted.
1209
-
1210
- * @returns {number} Value read
1211
-
1212
- * @expose
1213
-
1214
- */
1215
-
1216
- readInt = this.readInt32
1217
-
1218
- /**
1219
-
1220
- * Writes a 32bit unsigned integer.
1221
-
1222
- * @param {number} value Value to write
1223
-
1224
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
1225
-
1226
- * @expose
1227
-
1228
- */
1229
-
1230
- writeUint32(value, offset) {
1231
- const relative = typeof offset === 'undefined'
1232
-
1233
- if (relative) {
1234
- offset = this.offset
1235
- }
1236
-
1237
- if (!this.noAssert) {
1238
- if (typeof value !== 'number' || value % 1 !== 0) {
1239
- throw TypeError('Illegal value: ' + value + ' (not an integer)')
1240
- }
1241
-
1242
- value >>>= 0
1243
-
1244
- if (typeof offset !== 'number' || offset % 1 !== 0) {
1245
- throw TypeError('Illegal offset: ' + offset + ' (not an integer)')
1246
- }
1247
-
1248
- offset >>>= 0
1249
-
1250
- if (offset < 0 || offset + 0 > this.buffer.byteLength) {
1251
- throw RangeError('Illegal offset: 0 <= ' + offset + ' (+0) <= ' + this.buffer.byteLength)
1252
- }
1253
- }
1254
-
1255
- offset += 4
1256
-
1257
- let capacity5 = this.buffer.byteLength
1258
-
1259
- if (offset > capacity5) {
1260
- this.resize((capacity5 *= 2) > offset ? capacity5 : offset)
1261
- }
1262
-
1263
- offset -= 4
1264
-
1265
- this.view.setUint32(offset, value, this.littleEndian)
1266
-
1267
- if (relative) {
1268
- this.offset += 4
1269
- }
1270
-
1271
- return this
1272
- }
1273
-
1274
- /**
1275
-
1276
- * Writes a 32bit unsigned integer. This is an alias of {@link ByteBuffer#writeUint32}.
1277
-
1278
- * @function
1279
-
1280
- * @param {number} value Value to write
1281
-
1282
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
1283
-
1284
- * @expose
1285
-
1286
- */
1287
-
1288
- writeUInt32 = this.writeUint32
1289
-
1290
- /**
1291
-
1292
- * Reads a 32bit unsigned integer.
1293
-
1294
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
1295
-
1296
- * @returns {number} Value read
1297
-
1298
- * @expose
1299
-
1300
- */
1301
-
1302
- readUint32(offset) {
1303
- const relative = typeof offset === 'undefined'
1304
-
1305
- if (relative) {
1306
- offset = this.offset
1307
- }
1308
-
1309
- if (!this.noAssert) {
1310
- if (typeof offset !== 'number' || offset % 1 !== 0) {
1311
- throw TypeError('Illegal offset: ' + offset + ' (not an integer)')
1312
- }
1313
-
1314
- offset >>>= 0
1315
-
1316
- if (offset < 0 || offset + 4 > this.buffer.byteLength) {
1317
- throw RangeError('Illegal offset: 0 <= ' + offset + ' (+4) <= ' + this.buffer.byteLength)
1318
- }
1319
- }
1320
-
1321
- const value = this.view.getUint32(offset, this.littleEndian)
1322
-
1323
- if (relative) {
1324
- this.offset += 4
1325
- }
1326
-
1327
- return value
1328
- }
1329
-
1330
- /**
1331
-
1332
- * Reads a 32bit unsigned integer. This is an alias of {@link ByteBuffer#readUint32}.
1333
-
1334
- * @function
1335
-
1336
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
1337
-
1338
- * @returns {number} Value read
1339
-
1340
- * @expose
1341
-
1342
- */
1343
-
1344
- readUInt32 = this.readUint32
1345
-
1346
- /**
1347
-
1348
- * Writes a 32bit float.
1349
-
1350
- * @param {number} value Value to write
1351
-
1352
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
1353
-
1354
- * @returns {!ByteBuffer} this
1355
-
1356
- * @expose
1357
-
1358
- */
1359
-
1360
- writeFloat32(value, offset) {
1361
- const relative = typeof offset === 'undefined'
1362
-
1363
- if (relative) {
1364
- offset = this.offset
1365
- }
1366
-
1367
- if (!this.noAssert) {
1368
- if (typeof value !== 'number') {
1369
- throw TypeError('Illegal value: ' + value + ' (not a number)')
1370
- }
1371
-
1372
- if (typeof offset !== 'number' || offset % 1 !== 0) {
1373
- throw TypeError('Illegal offset: ' + offset + ' (not an integer)')
1374
- }
1375
-
1376
- offset >>>= 0
1377
-
1378
- if (offset < 0 || offset + 0 > this.buffer.byteLength) {
1379
- throw RangeError('Illegal offset: 0 <= ' + offset + ' (+0) <= ' + this.buffer.byteLength)
1380
- }
1381
- }
1382
-
1383
- offset += 4
1384
-
1385
- let capacity8 = this.buffer.byteLength
1386
-
1387
- if (offset > capacity8) {
1388
- this.resize((capacity8 *= 2) > offset ? capacity8 : offset)
1389
- }
1390
-
1391
- offset -= 4
1392
-
1393
- this.view.setFloat32(offset, value, this.littleEndian)
1394
-
1395
- if (relative) {
1396
- this.offset += 4
1397
- }
1398
-
1399
- return this
1400
- }
1401
-
1402
- /**
1403
-
1404
- * Writes a 32bit float. This is an alias of {@link ByteBuffer#writeFloat32}.
1405
-
1406
- * @function
1407
-
1408
- * @param {number} value Value to write
1409
-
1410
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
1411
-
1412
- * @returns {!ByteBuffer} this
1413
-
1414
- * @expose
1415
-
1416
- */
1417
-
1418
- writeFloat = this.writeFloat32
1419
-
1420
- /**
1421
-
1422
- * Reads a 32bit float.
1423
-
1424
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
1425
-
1426
- * @returns {number}
1427
-
1428
- * @expose
1429
-
1430
- */
1431
-
1432
- readFloat32(offset) {
1433
- const relative = typeof offset === 'undefined'
1434
-
1435
- if (relative) {
1436
- offset = this.offset
1437
- }
1438
-
1439
- if (!this.noAssert) {
1440
- if (typeof offset !== 'number' || offset % 1 !== 0) {
1441
- throw TypeError('Illegal offset: ' + offset + ' (not an integer)')
1442
- }
1443
-
1444
- offset >>>= 0
1445
-
1446
- if (offset < 0 || offset + 4 > this.buffer.byteLength) {
1447
- throw RangeError('Illegal offset: 0 <= ' + offset + ' (+4) <= ' + this.buffer.byteLength)
1448
- }
1449
- }
1450
-
1451
- const value = this.view.getFloat32(offset, this.littleEndian)
1452
-
1453
- if (relative) {
1454
- this.offset += 4
1455
- }
1456
-
1457
- return value
1458
- }
1459
-
1460
- /**
1461
-
1462
- * Reads a 32bit float. This is an alias of {@link ByteBuffer#readFloat32}.
1463
-
1464
- * @function
1465
-
1466
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
1467
-
1468
- * @returns {number}
1469
-
1470
- * @expose
1471
-
1472
- */
1473
-
1474
- readFloat = this.readFloat32
1475
-
1476
- // types/floats/float64
1477
-
1478
- /**
1479
-
1480
- * Writes a 64bit float.
1481
-
1482
- * @param {number} value Value to write
1483
-
1484
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
1485
-
1486
- * @returns {!ByteBuffer} this
1487
-
1488
- * @expose
1489
-
1490
- */
1491
-
1492
- writeFloat64(value, offset) {
1493
- const relative = typeof offset === 'undefined'
1494
-
1495
- if (relative) {
1496
- offset = this.offset
1497
- }
1498
-
1499
- if (!this.noAssert) {
1500
- if (typeof value !== 'number') {
1501
- throw TypeError('Illegal value: ' + value + ' (not a number)')
1502
- }
1503
-
1504
- if (typeof offset !== 'number' || offset % 1 !== 0) {
1505
- throw TypeError('Illegal offset: ' + offset + ' (not an integer)')
1506
- }
1507
-
1508
- offset >>>= 0
1509
-
1510
- if (offset < 0 || offset + 0 > this.buffer.byteLength) {
1511
- throw RangeError('Illegal offset: 0 <= ' + offset + ' (+0) <= ' + this.buffer.byteLength)
1512
- }
1513
- }
1514
-
1515
- offset += 8
1516
-
1517
- let capacity9 = this.buffer.byteLength
1518
-
1519
- if (offset > capacity9) {
1520
- this.resize((capacity9 *= 2) > offset ? capacity9 : offset)
1521
- }
1522
-
1523
- offset -= 8
1524
-
1525
- this.view.setFloat64(offset, value, this.littleEndian)
1526
-
1527
- if (relative) {
1528
- this.offset += 8
1529
- }
1530
-
1531
- return this
1532
- }
1533
-
1534
- /**
1535
-
1536
- * Writes a 64bit float. This is an alias of {@link ByteBuffer#writeFloat64}.
1537
-
1538
- * @function
1539
-
1540
- * @param {number} value Value to write
1541
-
1542
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
1543
-
1544
- * @returns {!ByteBuffer} this
1545
-
1546
- * @expose
1547
-
1548
- */
1549
-
1550
- writeDouble = this.writeFloat64
1551
-
1552
- /**
1553
-
1554
- * Reads a 64bit float.
1555
-
1556
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
1557
-
1558
- * @returns {number}
1559
-
1560
- * @expose
1561
-
1562
- */
1563
-
1564
- readFloat64(offset) {
1565
- const relative = typeof offset === 'undefined'
1566
-
1567
- if (relative) {
1568
- offset = this.offset
1569
- }
1570
-
1571
- if (!this.noAssert) {
1572
- if (typeof offset !== 'number' || offset % 1 !== 0) {
1573
- throw TypeError('Illegal offset: ' + offset + ' (not an integer)')
1574
- }
1575
-
1576
- offset >>>= 0
1577
-
1578
- if (offset < 0 || offset + 8 > this.buffer.byteLength) {
1579
- throw RangeError('Illegal offset: 0 <= ' + offset + ' (+8) <= ' + this.buffer.byteLength)
1580
- }
1581
- }
1582
-
1583
- const value = this.view.getFloat64(offset, this.littleEndian)
1584
-
1585
- if (relative) {
1586
- this.offset += 8
1587
- }
1588
-
1589
- return value
1590
- }
1591
-
1592
- /**
1593
-
1594
- * Reads a 64bit float. This is an alias of {@link ByteBuffer#readFloat64}.
1595
-
1596
- * @function
1597
-
1598
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
1599
-
1600
- * @returns {number}
1601
-
1602
- * @expose
1603
-
1604
- */
1605
-
1606
- readDouble = this.readFloat64
1607
-
1608
- /**
1609
-
1610
- * Appends some data to this ByteBuffer. This will overwrite any contents behind the specified offset up to the appended
1611
-
1612
- * data's length.
1613
-
1614
- * @param {!ByteBuffer|!ArrayBuffer|!Uint8Array} source Data to append. If `source` is a ByteBuffer, its offsets
1615
-
1616
- * will be modified according to the performed read operation.
1617
-
1618
- * @param {(string|number)=} encoding Encoding if `data` is a string ("base64", "hex", "binary", defaults to "utf8")
1619
-
1620
- * @param {number=} offset Offset to append at. Will use and increase {@link ByteBuffer#offset} by the number of bytes
1621
-
1622
- * written if omitted.
1623
-
1624
- * @returns {!ByteBuffer} this
1625
-
1626
- * @expose
1627
-
1628
- * @example A relative `<01 02>03.append(<04 05>)` will result in `<01 02 04 05>, 04 05|`
1629
-
1630
- * @example An absolute `<01 02>03.append(04 05>, 1)` will result in `<01 04>05, 04 05|`
1631
-
1632
- */
1633
-
1634
- append(source, offset) {
1635
- const relative = typeof offset === 'undefined'
1636
-
1637
- if (relative) {
1638
- offset = this.offset
1639
- }
1640
-
1641
- if (!this.noAssert) {
1642
- if (typeof offset !== 'number' || offset % 1 !== 0) {
1643
- throw TypeError('Illegal offset: ' + offset + ' (not an integer)')
1644
- }
1645
-
1646
- offset >>>= 0
1647
-
1648
- if (offset < 0 || offset + 0 > this.buffer.byteLength) {
1649
- throw RangeError('Illegal offset: 0 <= ' + offset + ' (+0) <= ' + this.buffer.byteLength)
1650
- }
1651
- }
1652
-
1653
- if (!(source instanceof ByteBuffer)) {
1654
- source = ByteBuffer.wrap(source)
1655
- }
1656
-
1657
- const length = source.limit - source.offset
1658
-
1659
- if (length <= 0) {
1660
- return this
1661
- }
1662
-
1663
- offset += length
1664
-
1665
- let capacity16 = this.buffer.byteLength
1666
-
1667
- if (offset > capacity16) {
1668
- this.resize((capacity16 *= 2) > offset ? capacity16 : offset)
1669
- }
1670
-
1671
- offset -= length
1672
-
1673
- new Uint8Array(this.buffer, offset).set(new Uint8Array(source.buffer).subarray(source.offset, source.limit))
1674
-
1675
- source.offset += length
1676
-
1677
- if (relative) {
1678
- this.offset += length
1679
- }
1680
-
1681
- return this
1682
- }
1683
-
1684
- /**
1685
-
1686
- * Appends this ByteBuffer's contents to another ByteBuffer. This will overwrite any contents at and after the
1687
-
1688
- specified offset up to the length of this ByteBuffer's data.
1689
-
1690
- * @param {!ByteBuffer} target Target ByteBuffer
1691
-
1692
- * @param {number=} offset Offset to append to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
1693
-
1694
- * read if omitted.
1695
-
1696
- * @returns {!ByteBuffer} this
1697
-
1698
- * @expose
1699
-
1700
- * @see ByteBuffer#append
1701
-
1702
- */
1703
-
1704
- appendTo(target, offset) {
1705
- target.append(this, offset)
1706
-
1707
- return this
1708
- }
1709
-
1710
- /**
1711
-
1712
- * Enables or disables assertions of argument types and offsets. Assertions are enabled by default but you can opt to
1713
-
1714
- * disable them if your code already makes sure that everything is valid.
1715
-
1716
- * @param {boolean} assert `true` to enable assertions, otherwise `false`
1717
-
1718
- * @returns {!ByteBuffer} this
1719
-
1720
- * @expose
1721
-
1722
- */
1723
-
1724
- assert(assert) {
1725
- this.noAssert = !assert
1726
-
1727
- return this
1728
- }
1729
-
1730
- /**
1731
-
1732
- * Gets the capacity of this ByteBuffer's backing buffer.
1733
-
1734
- * @returns {number} Capacity of the backing buffer
1735
-
1736
- * @expose
1737
-
1738
- */
1739
-
1740
- capacity() {
1741
- return this.buffer.byteLength
1742
- }
1743
-
1744
- /**
1745
-
1746
- * Clears this ByteBuffer's offsets by setting {@link ByteBuffer#offset} to `0` and {@link ByteBuffer#limit} to the
1747
-
1748
- * backing buffer's capacity. Discards {@link ByteBuffer#markedOffset}.
1749
-
1750
- * @returns {!ByteBuffer} this
1751
-
1752
- * @expose
1753
-
1754
- */
1755
-
1756
- clear() {
1757
- this.offset = 0
1758
-
1759
- this.limit = this.buffer.byteLength
1760
-
1761
- this.markedOffset = -1
1762
-
1763
- return this
1764
- }
1765
-
1766
- /**
1767
-
1768
- * Creates a cloned instance of this ByteBuffer, preset with this ByteBuffer's values for {@link ByteBuffer#offset},
1769
-
1770
- * {@link ByteBuffer#markedOffset} and {@link ByteBuffer#limit}.
1771
-
1772
- * @param {boolean=} copy Whether to copy the backing buffer or to return another view on the same, defaults to `false`
1773
-
1774
- * @returns {!ByteBuffer} Cloned instance
1775
-
1776
- * @expose
1777
-
1778
- */
1779
-
1780
- clone(copy) {
1781
- const bb = new ByteBuffer(0, this.littleEndian, this.noAssert)
1782
-
1783
- if (copy) {
1784
- bb.buffer = new ArrayBuffer(this.buffer.byteLength)
1785
-
1786
- new Uint8Array(bb.buffer).set(this.buffer)
1787
-
1788
- bb.view = new DataView(bb.buffer)
1789
- } else {
1790
- bb.buffer = this.buffer
1791
-
1792
- bb.view = this.view
1793
- }
1794
-
1795
- bb.offset = this.offset
1796
-
1797
- bb.markedOffset = this.markedOffset
1798
-
1799
- bb.limit = this.limit
1800
-
1801
- return bb
1802
- }
1803
-
1804
- /**
1805
-
1806
- * Compacts this ByteBuffer to be backed by a {@link ByteBuffer#buffer} of its contents' length. Contents are the bytes
1807
-
1808
- * between {@link ByteBuffer#offset} and {@link ByteBuffer#limit}. Will set `offset = 0` and `limit = capacity` and
1809
-
1810
- * adapt {@link ByteBuffer#markedOffset} to the same relative position if set.
1811
-
1812
- * @param {number=} begin Offset to start at, defaults to {@link ByteBuffer#offset}
1813
-
1814
- * @param {number=} end Offset to end at, defaults to {@link ByteBuffer#limit}
1815
-
1816
- * @returns {!ByteBuffer} this
1817
-
1818
- * @expose
1819
-
1820
- */
1821
-
1822
- compact(begin, end) {
1823
- if (typeof begin === 'undefined') {
1824
- begin = this.offset
1825
- }
1826
-
1827
- if (typeof end === 'undefined') {
1828
- end = this.limit
1829
- }
1830
-
1831
- if (!this.noAssert) {
1832
- if (typeof begin !== 'number' || begin % 1 !== 0) {
1833
- throw TypeError('Illegal begin: Not an integer')
1834
- }
1835
-
1836
- begin >>>= 0
1837
-
1838
- if (typeof end !== 'number' || end % 1 !== 0) {
1839
- throw TypeError('Illegal end: Not an integer')
1840
- }
1841
-
1842
- end >>>= 0
1843
-
1844
- if (begin < 0 || begin > end || end > this.buffer.byteLength) {
1845
- throw RangeError('Illegal range: 0 <= ' + begin + ' <= ' + end + ' <= ' + this.buffer.byteLength)
1846
- }
1847
- }
1848
-
1849
- if (begin === 0 && end === this.buffer.byteLength) {
1850
- return this
1851
- }
1852
-
1853
- const len = end - begin
1854
-
1855
- if (len === 0) {
1856
- this.buffer = EMPTY_BUFFER
1857
-
1858
- this.view = new DataView(EMPTY_BUFFER)
1859
-
1860
- if (this.markedOffset >= 0) {
1861
- this.markedOffset -= begin
1862
- }
1863
-
1864
- this.offset = 0
1865
-
1866
- this.limit = 0
1867
-
1868
- return this
1869
- }
1870
-
1871
- const buffer = new ArrayBuffer(len)
1872
-
1873
- new Uint8Array(buffer).set(new Uint8Array(this.buffer).subarray(begin, end))
1874
-
1875
- this.buffer = buffer
1876
-
1877
- this.view = new DataView(buffer)
1878
-
1879
- if (this.markedOffset >= 0) {
1880
- this.markedOffset -= begin
1881
- }
1882
-
1883
- this.offset = 0
1884
-
1885
- this.limit = len
1886
-
1887
- return this
1888
- }
1889
-
1890
- /**
1891
-
1892
- * Creates a copy of this ByteBuffer's contents. Contents are the bytes between {@link ByteBuffer#offset} and
1893
-
1894
- * {@link ByteBuffer#limit}.
1895
-
1896
- * @param {number=} begin Begin offset, defaults to {@link ByteBuffer#offset}.
1897
-
1898
- * @param {number=} end End offset, defaults to {@link ByteBuffer#limit}.
1899
-
1900
- * @returns {!ByteBuffer} Copy
1901
-
1902
- * @expose
1903
-
1904
- */
1905
-
1906
- copy(begin, end) {
1907
- if (typeof begin === 'undefined') {
1908
- begin = this.offset
1909
- }
1910
-
1911
- if (typeof end === 'undefined') {
1912
- end = this.limit
1913
- }
1914
-
1915
- if (!this.noAssert) {
1916
- if (typeof begin !== 'number' || begin % 1 !== 0) {
1917
- throw TypeError('Illegal begin: Not an integer')
1918
- }
1919
-
1920
- begin >>>= 0
1921
-
1922
- if (typeof end !== 'number' || end % 1 !== 0) {
1923
- throw TypeError('Illegal end: Not an integer')
1924
- }
1925
-
1926
- end >>>= 0
1927
-
1928
- if (begin < 0 || begin > end || end > this.buffer.byteLength) {
1929
- throw RangeError('Illegal range: 0 <= ' + begin + ' <= ' + end + ' <= ' + this.buffer.byteLength)
1930
- }
1931
- }
1932
-
1933
- if (begin === end) {
1934
- return new ByteBuffer(0, this.littleEndian, this.noAssert)
1935
- }
1936
-
1937
- const capacity = end - begin
1938
-
1939
- const bb = new ByteBuffer(capacity, this.littleEndian, this.noAssert)
1940
-
1941
- bb.offset = 0
1942
-
1943
- bb.limit = capacity
1944
-
1945
- if (bb.markedOffset >= 0) {
1946
- bb.markedOffset -= begin
1947
- }
1948
-
1949
- this.copyTo(bb, 0, begin, end)
1950
-
1951
- return bb
1952
- }
1953
-
1954
- /**
1955
-
1956
- * Copies this ByteBuffer's contents to another ByteBuffer. Contents are the bytes between {@link ByteBuffer#offset} and
1957
-
1958
- * {@link ByteBuffer#limit}.
1959
-
1960
- * @param {!ByteBuffer} target Target ByteBuffer
1961
-
1962
- * @param {number=} targetOffset Offset to copy to. Will use and increase the target's {@link ByteBuffer#offset}
1963
-
1964
- * by the number of bytes copied if omitted.
1965
-
1966
- * @param {number=} sourceOffset Offset to start copying from. Will use and increase {@link ByteBuffer#offset} by the
1967
-
1968
- * number of bytes copied if omitted.
1969
-
1970
- * @param {number=} sourceLimit Offset to end copying from, defaults to {@link ByteBuffer#limit}
1971
-
1972
- * @returns {!ByteBuffer} this
1973
-
1974
- * @expose
1975
-
1976
- */
1977
-
1978
- copyTo(target, targetOffset, sourceOffset, sourceLimit) {
1979
- const targetRelative = typeof targetOffset === 'undefined'
1980
- const relative = typeof sourceOffset === 'undefined'
1981
-
1982
- if (!this.noAssert) {
1983
- if (!(target instanceof ByteBuffer)) {
1984
- throw TypeError('Illegal target: Not a ByteBuffer')
1985
- }
1986
- }
1987
-
1988
- targetOffset = targetRelative ? target.offset : targetOffset | 0
1989
-
1990
- sourceOffset = relative ? this.offset : sourceOffset | 0
1991
-
1992
- sourceLimit = typeof sourceLimit === 'undefined' ? this.limit : sourceLimit | 0
1993
-
1994
- if (targetOffset < 0 || targetOffset > target.buffer.byteLength) {
1995
- throw RangeError('Illegal target range: 0 <= ' + targetOffset + ' <= ' + target.buffer.byteLength)
1996
- }
1997
-
1998
- if (sourceOffset < 0 || sourceLimit > this.buffer.byteLength) {
1999
- throw RangeError('Illegal source range: 0 <= ' + sourceOffset + ' <= ' + this.buffer.byteLength)
2000
- }
2001
-
2002
- const len = sourceLimit - sourceOffset
2003
-
2004
- if (len === 0) {
2005
- return target
2006
- }
2007
-
2008
- target.ensureCapacity(targetOffset + len)
2009
-
2010
- new Uint8Array(target.buffer).set(new Uint8Array(this.buffer).subarray(sourceOffset, sourceLimit), targetOffset)
2011
-
2012
- if (relative) {
2013
- this.offset += len
2014
- }
2015
-
2016
- if (targetRelative) {
2017
- target.offset += len
2018
- }
2019
-
2020
- return this
2021
- }
2022
-
2023
- /**
2024
-
2025
- * Makes sure that this ByteBuffer is backed by a {@link ByteBuffer#buffer} of at least the specified capacity. If the
2026
-
2027
- * current capacity is exceeded, it will be doubled. If double the current capacity is less than the required capacity,
2028
-
2029
- * the required capacity will be used instead.
2030
-
2031
- * @param {number} capacity Required capacity
2032
-
2033
- * @returns {!ByteBuffer} this
2034
-
2035
- * @expose
2036
-
2037
- */
2038
-
2039
- ensureCapacity(capacity) {
2040
- let current = this.buffer.byteLength
2041
-
2042
- if (current < capacity) {
2043
- return this.resize((current *= 2) > capacity ? current : capacity)
2044
- }
2045
-
2046
- return this
2047
- }
2048
-
2049
- /**
2050
-
2051
- * Overwrites this ByteBuffer's contents with the specified value. Contents are the bytes between
2052
-
2053
- * {@link ByteBuffer#offset} and {@link ByteBuffer#limit}.
2054
-
2055
- * @param {number|string} value Byte value to fill with. If given as a string, the first character is used.
2056
-
2057
- * @param {number=} begin Begin offset. Will use and increase {@link ByteBuffer#offset} by the number of bytes
2058
-
2059
- * written if omitted. defaults to {@link ByteBuffer#offset}.
2060
-
2061
- * @param {number=} end End offset, defaults to {@link ByteBuffer#limit}.
2062
-
2063
- * @returns {!ByteBuffer} this
2064
-
2065
- * @expose
2066
-
2067
- * @example `someByteBuffer.clear().fill(0)` fills the entire backing buffer with zeroes
2068
-
2069
- */
2070
-
2071
- fill(value, begin, end) {
2072
- const relative = typeof begin === 'undefined'
2073
-
2074
- if (relative) {
2075
- begin = this.offset
2076
- }
2077
-
2078
- if (typeof value === 'string' && value.length > 0) {
2079
- value = value.charCodeAt(0)
2080
- }
2081
-
2082
- if (typeof begin === 'undefined') {
2083
- begin = this.offset
2084
- }
2085
-
2086
- if (typeof end === 'undefined') {
2087
- end = this.limit
2088
- }
2089
-
2090
- if (!this.noAssert) {
2091
- if (typeof value !== 'number' || value % 1 !== 0) {
2092
- throw TypeError('Illegal value: ' + value + ' (not an integer)')
2093
- }
2094
-
2095
- value |= 0
2096
-
2097
- if (typeof begin !== 'number' || begin % 1 !== 0) {
2098
- throw TypeError('Illegal begin: Not an integer')
2099
- }
2100
-
2101
- begin >>>= 0
2102
-
2103
- if (typeof end !== 'number' || end % 1 !== 0) {
2104
- throw TypeError('Illegal end: Not an integer')
2105
- }
2106
-
2107
- end >>>= 0
2108
-
2109
- if (begin < 0 || begin > end || end > this.buffer.byteLength) {
2110
- throw RangeError('Illegal range: 0 <= ' + begin + ' <= ' + end + ' <= ' + this.buffer.byteLength)
2111
- }
2112
- }
2113
-
2114
- if (begin >= end) {
2115
- return this
2116
- }
2117
-
2118
- while (begin < end) {
2119
- this.view.setUint8(begin++, value)
2120
- }
2121
-
2122
- if (relative) {
2123
- this.offset = begin
2124
- }
2125
-
2126
- return this
2127
- }
2128
-
2129
- /**
2130
-
2131
- * Makes this ByteBuffer ready for a new sequence of write or relative read operations. Sets `limit = offset` and
2132
-
2133
- * `offset = 0`. Make sure always to flip a ByteBuffer when all relative read or write operations are complete.
2134
-
2135
- * @returns {!ByteBuffer} this
2136
-
2137
- * @expose
2138
-
2139
- */
2140
-
2141
- flip() {
2142
- this.limit = this.offset
2143
-
2144
- this.offset = 0
2145
-
2146
- return this
2147
- }
2148
-
2149
- /**
2150
-
2151
- * Marks an offset on this ByteBuffer to be used later.
2152
-
2153
- * @param {number=} offset Offset to mark. Defaults to {@link ByteBuffer#offset}.
2154
-
2155
- * @returns {!ByteBuffer} this
2156
-
2157
- * @throws {TypeError} If `offset` is not a valid number
2158
-
2159
- * @throws {RangeError} If `offset` is out of bounds
2160
-
2161
- * @see ByteBuffer#reset
2162
-
2163
- * @expose
2164
-
2165
- */
2166
-
2167
- mark(offset) {
2168
- offset = typeof offset === 'undefined' ? this.offset : offset
2169
-
2170
- if (!this.noAssert) {
2171
- if (typeof offset !== 'number' || offset % 1 !== 0) {
2172
- throw TypeError('Illegal offset: ' + offset + ' (not an integer)')
2173
- }
2174
-
2175
- offset >>>= 0
2176
-
2177
- if (offset < 0 || offset + 0 > this.buffer.byteLength) {
2178
- throw RangeError('Illegal offset: 0 <= ' + offset + ' (+0) <= ' + this.buffer.byteLength)
2179
- }
2180
- }
2181
-
2182
- this.markedOffset = offset
2183
-
2184
- return this
2185
- }
2186
-
2187
- /**
2188
-
2189
- * Sets the byte order.
2190
-
2191
- * @param {boolean} littleEndian `true` for little endian byte order, `false` for big endian
2192
-
2193
- * @returns {!ByteBuffer} this
2194
-
2195
- * @expose
2196
-
2197
- */
2198
-
2199
- order(littleEndian) {
2200
- if (!this.noAssert) {
2201
- if (typeof littleEndian !== 'boolean') {
2202
- throw TypeError('Illegal littleEndian: Not a boolean')
2203
- }
2204
- }
2205
-
2206
- this.littleEndian = !!littleEndian
2207
-
2208
- return this
2209
- }
2210
-
2211
- /**
2212
-
2213
- * Switches (to) little endian byte order.
2214
-
2215
- * @param {boolean=} littleEndian Defaults to `true`, otherwise uses big endian
2216
-
2217
- * @returns {!ByteBuffer} this
2218
-
2219
- * @expose
2220
-
2221
- */
2222
-
2223
- LE(littleEndian) {
2224
- this.littleEndian = typeof littleEndian !== 'undefined' ? !!littleEndian : true
2225
-
2226
- return this
2227
- }
2228
-
2229
- /**
2230
-
2231
- * Switches (to) big endian byte order.
2232
-
2233
- * @param {boolean=} bigEndian Defaults to `true`, otherwise uses little endian
2234
-
2235
- * @returns {!ByteBuffer} this
2236
-
2237
- * @expose
2238
-
2239
- */
2240
-
2241
- BE(bigEndian) {
2242
- this.littleEndian = typeof bigEndian !== 'undefined' ? !bigEndian : false
2243
-
2244
- return this
2245
- }
2246
-
2247
- /**
2248
-
2249
- * Prepends some data to this ByteBuffer. This will overwrite any contents before the specified offset up to the
2250
-
2251
- * prepended data's length. If there is not enough space available before the specified `offset`, the backing buffer
2252
-
2253
- * will be resized and its contents moved accordingly.
2254
-
2255
- * @param {!ByteBuffer|!ArrayBuffer} source Data to prepend. If `source` is a ByteBuffer, its offset will be
2256
-
2257
- * modified according to the performed read operation.
2258
-
2259
- * @param {(string|number)=} encoding Encoding if `data` is a string ("base64", "hex", "binary", defaults to "utf8")
2260
-
2261
- * @param {number=} offset Offset to prepend at. Will use and decrease {@link ByteBuffer#offset} by the number of bytes
2262
-
2263
- * prepended if omitted.
2264
-
2265
- * @returns {!ByteBuffer} this
2266
-
2267
- * @expose
2268
-
2269
- * @example A relative `00<01 02 03>.prepend(<04 05>)` results in `<04 05 01 02 03>, 04 05|`
2270
-
2271
- * @example An absolute `00<01 02 03>.prepend(<04 05>, 2)` results in `04<05 02 03>, 04 05|`
2272
-
2273
- */
2274
-
2275
- prepend(source, offset) {
2276
- const relative = typeof offset === 'undefined'
2277
-
2278
- if (relative) {
2279
- offset = this.offset
2280
- }
2281
-
2282
- if (!this.noAssert) {
2283
- if (typeof offset !== 'number' || offset % 1 !== 0) {
2284
- throw TypeError('Illegal offset: ' + offset + ' (not an integer)')
2285
- }
2286
-
2287
- offset >>>= 0
2288
-
2289
- if (offset < 0 || offset + 0 > this.buffer.byteLength) {
2290
- throw RangeError('Illegal offset: 0 <= ' + offset + ' (+0) <= ' + this.buffer.byteLength)
2291
- }
2292
- }
2293
-
2294
- if (!(source instanceof ByteBuffer)) {
2295
- source = ByteBuffer.wrap(source)
2296
- }
2297
-
2298
- const len = source.limit - source.offset
2299
-
2300
- if (len <= 0) {
2301
- return this
2302
- }
2303
-
2304
- const diff = len - offset
2305
-
2306
- if (diff > 0) {
2307
- const buffer = new ArrayBuffer(this.buffer.byteLength + diff)
2308
-
2309
- const arrayView = new Uint8Array(buffer)
2310
-
2311
- arrayView.set(new Uint8Array(this.buffer).subarray(offset, this.buffer.byteLength), len)
2312
-
2313
- this.buffer = buffer
2314
-
2315
- this.view = new DataView(buffer)
2316
-
2317
- this.offset += diff
2318
-
2319
- if (this.markedOffset >= 0) {
2320
- this.markedOffset += diff
2321
- }
2322
-
2323
- this.limit += diff
2324
-
2325
- offset += diff
2326
- } else {
2327
- const arrayView = new Uint8Array(this.buffer)
2328
-
2329
- arrayView.set(new Uint8Array(source.buffer).subarray(source.offset, source.limit), offset - len)
2330
- }
2331
-
2332
- source.offset = source.limit
2333
-
2334
- if (relative) {
2335
- this.offset -= len
2336
- }
2337
-
2338
- return this
2339
- }
2340
-
2341
- /**
2342
-
2343
- * Prepends this ByteBuffer to another ByteBuffer. This will overwrite any contents before the specified offset up to the
2344
-
2345
- * prepended data's length. If there is not enough space available before the specified `offset`, the backing buffer
2346
-
2347
- * will be resized and its contents moved accordingly.
2348
-
2349
- * @param {!ByteBuffer} target Target ByteBuffer
2350
-
2351
- * @param {number=} offset Offset to prepend at. Will use and decrease {@link ByteBuffer#offset} by the number of bytes
2352
-
2353
- * prepended if omitted.
2354
-
2355
- * @returns {!ByteBuffer} this
2356
-
2357
- * @expose
2358
-
2359
- * @see ByteBuffer#prepend
2360
-
2361
- */
2362
-
2363
- prependTo(target, offset) {
2364
- target.prepend(this, offset)
2365
-
2366
- return this
2367
- }
2368
-
2369
- /**
2370
-
2371
- * Gets the number of remaining readable bytes. Contents are the bytes between {@link ByteBuffer#offset} and
2372
-
2373
- * {@link ByteBuffer#limit}, so this returns `limit - offset`.
2374
-
2375
- * @returns {number} Remaining readable bytes. May be negative if `offset > limit`.
2376
-
2377
- * @expose
2378
-
2379
- */
2380
-
2381
- remaining() {
2382
- return this.limit - this.offset
2383
- }
2384
-
2385
- /**
2386
-
2387
- * Resets this ByteBuffer's {@link ByteBuffer#offset}. If an offset has been marked through {@link ByteBuffer#mark}
2388
-
2389
- * before, `offset` will be set to {@link ByteBuffer#markedOffset}, which will then be discarded. If no offset has been
2390
-
2391
- * marked, sets `offset = 0`.
2392
-
2393
- * @returns {!ByteBuffer} this
2394
-
2395
- * @see ByteBuffer#mark
2396
-
2397
- * @expose
2398
-
2399
- */
2400
-
2401
- reset() {
2402
- if (this.markedOffset >= 0) {
2403
- this.offset = this.markedOffset
2404
-
2405
- this.markedOffset = -1
2406
- } else {
2407
- this.offset = 0
2408
- }
2409
-
617
+ * Enables or disables assertions of argument types and offsets. Assertions are enabled by default but you can opt to
618
+ * disable them if your code already makes sure that everything is valid.
619
+ * @param {boolean} assert `true` to enable assertions, otherwise `false`
620
+ * @returns {!ByteBuffer} this
621
+ * @expose
622
+ */
623
+ assert(assert) {
624
+ this.noAssert = !assert
2410
625
  return this
2411
626
  }
2412
627
 
2413
628
  /**
2414
-
2415
- * Resizes this ByteBuffer to be backed by a buffer of at least the given capacity. Will do nothing if already that
2416
-
2417
- * large or larger.
2418
-
2419
- * @param {number} capacity Capacity required
2420
-
2421
- * @returns {!ByteBuffer} this
2422
-
2423
- * @throws {TypeError} If `capacity` is not a number
2424
-
2425
- * @throws {RangeError} If `capacity < 0`
2426
-
2427
- * @expose
2428
-
2429
- */
2430
-
2431
- resize(capacity) {
2432
- if (!this.noAssert) {
2433
- if (typeof capacity !== 'number' || capacity % 1 !== 0) {
2434
- throw TypeError('Illegal capacity: ' + capacity + ' (not an integer)')
2435
- }
2436
-
2437
- capacity |= 0
2438
-
2439
- if (capacity < 0) {
2440
- throw RangeError('Illegal capacity: 0 <= ' + capacity)
2441
- }
2442
- }
2443
-
2444
- if (this.buffer.byteLength < capacity) {
2445
- const buffer = new ArrayBuffer(capacity)
2446
-
2447
- new Uint8Array(buffer).set(new Uint8Array(this.buffer))
2448
-
2449
- this.buffer = buffer
2450
-
2451
- this.view = new DataView(buffer)
2452
- }
2453
-
2454
- return this
629
+ * Gets the capacity of this ByteBuffer's backing buffer.
630
+ * @returns {number} Capacity of the backing buffer
631
+ * @expose
632
+ */
633
+ capacity() {
634
+ return this.buffer.byteLength
2455
635
  }
2456
636
 
2457
637
  /**
2458
-
2459
- * Reverses this ByteBuffer's contents.
2460
-
2461
- * @param {number=} begin Offset to start at, defaults to {@link ByteBuffer#offset}
2462
-
2463
- * @param {number=} end Offset to end at, defaults to {@link ByteBuffer#limit}
2464
-
2465
- * @returns {!ByteBuffer} this
2466
-
2467
- * @expose
2468
-
2469
- */
2470
-
2471
- reverse(begin, end) {
2472
- if (typeof begin === 'undefined') {
2473
- begin = this.offset
2474
- }
2475
-
2476
- if (typeof end === 'undefined') {
2477
- end = this.limit
2478
- }
2479
-
2480
- if (!this.noAssert) {
2481
- if (typeof begin !== 'number' || begin % 1 !== 0) {
2482
- throw TypeError('Illegal begin: Not an integer')
2483
- }
2484
-
2485
- begin >>>= 0
2486
-
2487
- if (typeof end !== 'number' || end % 1 !== 0) {
2488
- throw TypeError('Illegal end: Not an integer')
2489
- }
2490
-
2491
- end >>>= 0
2492
-
2493
- if (begin < 0 || begin > end || end > this.buffer.byteLength) {
2494
- throw RangeError('Illegal range: 0 <= ' + begin + ' <= ' + end + ' <= ' + this.buffer.byteLength)
2495
- }
2496
- }
2497
-
2498
- if (begin === end) {
2499
- return this
638
+ * Creates a cloned instance of this ByteBuffer, preset with this ByteBuffer's values for {@link ByteBuffer#offset},
639
+ * {@link ByteBuffer#markedOffset} and {@link ByteBuffer#limit}.
640
+ * @param {boolean=} copy Whether to copy the backing buffer or to return another view on the same, defaults to `false`
641
+ * @returns {!ByteBuffer} Cloned instance
642
+ * @expose
643
+ */
644
+ clone(copy) {
645
+ const bb = new ByteBuffer(0, this.littleEndian, this.noAssert)
646
+ if (copy) {
647
+ bb.buffer = new ArrayBuffer(this.buffer.byteLength)
648
+ new Uint8Array(bb.buffer).set(this.buffer)
649
+ bb.view = new DataView(bb.buffer)
650
+ } else {
651
+ bb.buffer = this.buffer
652
+ bb.view = this.view
2500
653
  }
2501
654
 
2502
- Array.prototype.reverse.call(new Uint8Array(this.buffer).subarray(begin, end))
2503
-
2504
- this.view = new DataView(this.buffer)
655
+ bb.offset = this.offset
656
+ bb.markedOffset = this.markedOffset
657
+ bb.limit = this.limit
658
+ return bb
659
+ }
2505
660
 
661
+ /**
662
+ * Makes this ByteBuffer ready for a new sequence of write or relative read operations. Sets `limit = offset` and
663
+ * `offset = 0`. Make sure always to flip a ByteBuffer when all relative read or write operations are complete.
664
+ * @returns {!ByteBuffer} this
665
+ * @expose
666
+ */
667
+ flip() {
668
+ this.limit = this.offset
669
+ this.offset = 0
2506
670
  return this
2507
671
  }
2508
672
 
2509
673
  /**
2510
-
2511
- * Skips the next `length` bytes. This will just advance
2512
-
2513
- * @param {number} length Number of bytes to skip. May also be negative to move the offset back.
2514
-
2515
- * @returns {!ByteBuffer} this
2516
-
2517
- * @expose
2518
-
2519
- */
2520
-
2521
- skip(length) {
674
+ * Resizes this ByteBuffer to be backed by a buffer of at least the given capacity. Will do nothing if already that
675
+ * large or larger.
676
+ * @param {number} capacity Capacity required
677
+ * @returns {!ByteBuffer} this
678
+ * @throws {TypeError} If `capacity` is not a number
679
+ * @throws {RangeError} If `capacity < 0`
680
+ * @expose
681
+ */
682
+ resize(capacity) {
2522
683
  if (!this.noAssert) {
2523
- if (typeof length !== 'number' || length % 1 !== 0) {
2524
- throw TypeError('Illegal length: ' + length + ' (not an integer)')
684
+ if (typeof capacity !== 'number' || capacity % 1 !== 0) {
685
+ throw TypeError('Illegal capacity: ' + capacity + ' (not an integer)')
2525
686
  }
2526
687
 
2527
- length |= 0
2528
- }
2529
-
2530
- const offset = this.offset + length
688
+ capacity |= 0
2531
689
 
2532
- if (!this.noAssert) {
2533
- if (offset < 0 || offset > this.buffer.byteLength) {
2534
- throw RangeError('Illegal length: 0 <= ' + this.offset + ' + ' + length + ' <= ' + this.buffer.byteLength)
690
+ if (capacity < 0) {
691
+ throw RangeError('Illegal capacity: 0 <= ' + capacity)
2535
692
  }
2536
693
  }
2537
694
 
2538
- this.offset = offset
2539
-
2540
- return this
2541
- }
2542
-
2543
- /**
2544
-
2545
- * Slices this ByteBuffer by creating a cloned instance with `offset = begin` and `limit = end`.
2546
-
2547
- * @param {number=} begin Begin offset, defaults to {@link ByteBuffer#offset}.
695
+ if (this.buffer.byteLength < capacity) {
696
+ const buffer = new ArrayBuffer(capacity)
2548
697
 
2549
- * @param {number=} end End offset, defaults to {@link ByteBuffer#limit}.
698
+ new Uint8Array(buffer).set(new Uint8Array(this.buffer))
2550
699
 
2551
- * @returns {!ByteBuffer} Clone of this ByteBuffer with slicing applied, backed by the same {@link ByteBuffer#buffer}
700
+ this.buffer = buffer
2552
701
 
2553
- * @expose
702
+ this.view = new DataView(buffer)
703
+ }
2554
704
 
2555
- */
705
+ return this
706
+ }
2556
707
 
708
+ /**
709
+ * Slices this ByteBuffer by creating a cloned instance with `offset = begin` and `limit = end`.
710
+ * @param {number=} begin Begin offset, defaults to {@link ByteBuffer#offset}.
711
+ * @param {number=} end End offset, defaults to {@link ByteBuffer#limit}.
712
+ * @returns {!ByteBuffer} Clone of this ByteBuffer with slicing applied, backed by the same {@link ByteBuffer#buffer}
713
+ * @expose
714
+ */
2557
715
  slice(begin, end) {
2558
716
  if (typeof begin === 'undefined') {
2559
717
  begin = this.offset
@@ -2591,19 +749,12 @@ export class ByteBuffer {
2591
749
  }
2592
750
 
2593
751
  /**
2594
-
2595
- * Writes a 64bit signed integer.
2596
-
2597
- * @param {number|bigint} value Value to write
2598
-
2599
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
2600
-
2601
- * @returns {!ByteBuffer} this
2602
-
2603
- * @expose
2604
-
2605
- */
2606
-
752
+ * Writes a 64bit signed integer.
753
+ * @param {number|bigint} value Value to write
754
+ * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
755
+ * @returns {!ByteBuffer} this
756
+ * @expose
757
+ */
2607
758
  writeInt64(value, offset) {
2608
759
  const relative = typeof offset === 'undefined'
2609
760
 
@@ -2651,89 +802,21 @@ export class ByteBuffer {
2651
802
  }
2652
803
 
2653
804
  /**
2654
-
2655
- * Writes a 64bit signed integer. This is an alias of {@link ByteBuffer#writeInt64}.
2656
-
2657
- * @param {number|!bigint} value Value to write
2658
-
2659
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
2660
-
2661
- * @returns {!ByteBuffer} this
2662
-
2663
- * @expose
2664
-
2665
- */
2666
-
805
+ * Writes a 64bit signed integer. This is an alias of {@link ByteBuffer#writeInt64}.
806
+ * @param {number|!bigint} value Value to write
807
+ * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
808
+ * @returns {!ByteBuffer} this
809
+ * @expose
810
+ */
2667
811
  writeLong = this.writeInt64
2668
812
 
2669
813
  /**
2670
-
2671
- * Reads a 64bit signed integer.
2672
-
2673
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
2674
-
2675
- * @returns {!bigint}
2676
-
2677
- * @expose
2678
-
2679
- */
2680
-
2681
- readInt64(offset) {
2682
- const relative = typeof offset === 'undefined'
2683
-
2684
- if (relative) {
2685
- offset = this.offset
2686
- }
2687
-
2688
- if (!this.noAssert) {
2689
- if (typeof offset !== 'number' || offset % 1 !== 0) {
2690
- throw TypeError('Illegal offset: ' + offset + ' (not an integer)')
2691
- }
2692
-
2693
- offset >>>= 0
2694
-
2695
- if (offset < 0 || offset + 8 > this.buffer.byteLength) {
2696
- throw RangeError('Illegal offset: 0 <= ' + offset + ' (+8) <= ' + this.buffer.byteLength)
2697
- }
2698
- }
2699
-
2700
- const value = this.view.getBigInt64(offset, this.littleEndian)
2701
-
2702
- if (relative) {
2703
- this.offset += 8
2704
- }
2705
-
2706
- return value
2707
- }
2708
-
2709
- /**
2710
-
2711
- * Reads a 64bit signed integer. This is an alias of {@link ByteBuffer#readInt64}.
2712
-
2713
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
2714
-
2715
- * @returns {!bigint}
2716
-
2717
- * @expose
2718
-
2719
- */
2720
-
2721
- readLong = this.readInt64
2722
-
2723
- /**
2724
-
2725
- * Writes a 64bit unsigned integer.
2726
-
2727
- * @param {number|!bigint} value Value to write
2728
-
2729
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
2730
-
2731
- * @returns {!ByteBuffer} this
2732
-
2733
- * @expose
2734
-
2735
- */
2736
-
814
+ * Writes a 64bit unsigned integer.
815
+ * @param {number|!bigint} value Value to write
816
+ * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
817
+ * @returns {!ByteBuffer} this
818
+ * @expose
819
+ */
2737
820
  writeUint64(value, offset) {
2738
821
  const relative = typeof offset === 'undefined'
2739
822
 
@@ -2781,95 +864,23 @@ export class ByteBuffer {
2781
864
  }
2782
865
 
2783
866
  /**
2784
-
2785
- * Writes a 64bit unsigned integer. This is an alias of {@link ByteBuffer#writeUint64}.
2786
-
2787
- * @function
2788
-
2789
- * @param {number|!bigint} value Value to write
2790
-
2791
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
2792
-
2793
- * @returns {!ByteBuffer} this
2794
-
2795
- * @expose
2796
-
2797
- */
2798
-
867
+ * Writes a 64bit unsigned integer. This is an alias of {@link ByteBuffer#writeUint64}.
868
+ * @function
869
+ * @param {number|!bigint} value Value to write
870
+ * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
871
+ * @returns {!ByteBuffer} this
872
+ * @expose
873
+ */
2799
874
  writeUInt64 = this.writeUint64
2800
875
 
2801
876
  /**
2802
-
2803
- * Reads a 64bit unsigned integer.
2804
-
2805
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
2806
-
2807
- * @returns {!bigint}
2808
-
2809
- * @expose
2810
-
2811
- */
2812
-
2813
- readUint64(offset) {
2814
- const relative = typeof offset === 'undefined'
2815
-
2816
- if (relative) {
2817
- offset = this.offset
2818
- }
2819
-
2820
- if (!this.noAssert) {
2821
- if (typeof offset !== 'number' || offset % 1 !== 0) {
2822
- throw TypeError('Illegal offset: ' + offset + ' (not an integer)')
2823
- }
2824
-
2825
- offset >>>= 0
2826
-
2827
- if (offset < 0 || offset + 8 > this.buffer.byteLength) {
2828
- throw RangeError('Illegal offset: 0 <= ' + offset + ' (+8) <= ' + this.buffer.byteLength)
2829
- }
2830
- }
2831
-
2832
- const value = this.view.getBigUint64(offset, this.littleEndian)
2833
-
2834
- if (relative) {
2835
- this.offset += 8
2836
- }
2837
-
2838
- return value
2839
- }
2840
-
2841
- /**
2842
-
2843
- * Reads a 64bit unsigned integer. This is an alias of {@link ByteBuffer#readUint64}.
2844
-
2845
- * @function
2846
-
2847
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
2848
-
2849
- * @returns {!Long}
2850
-
2851
- * @expose
2852
-
2853
- */
2854
-
2855
- readUInt64 = this.readUint64
2856
-
2857
- /**
2858
-
2859
- * Returns a copy of the backing buffer that contains this ByteBuffer's contents. Contents are the bytes between
2860
-
2861
- * {@link ByteBuffer#offset} and {@link ByteBuffer#limit}.
2862
-
2863
- * @param {boolean=} forceCopy If `true` returns a copy, otherwise returns a view referencing the same memory if
2864
-
2865
- * possible. Defaults to `false`
2866
-
2867
- * @returns {!ArrayBuffer} Contents as an ArrayBuffer
2868
-
2869
- * @expose
2870
-
2871
- */
2872
-
877
+ * Returns a copy of the backing buffer that contains this ByteBuffer's contents. Contents are the bytes between
878
+ * {@link ByteBuffer#offset} and {@link ByteBuffer#limit}.
879
+ * @param {boolean=} forceCopy If `true` returns a copy, otherwise returns a view referencing the same memory if
880
+ * possible. Defaults to `false`
881
+ * @returns {!ArrayBuffer} Contents as an ArrayBuffer
882
+ * @expose
883
+ */
2873
884
  toBuffer(forceCopy) {
2874
885
  let offset = this.offset
2875
886
 
@@ -2913,23 +924,14 @@ export class ByteBuffer {
2913
924
  }
2914
925
 
2915
926
  /**
2916
-
2917
- * Returns a raw buffer compacted to contain this ByteBuffer's contents. Contents are the bytes between
2918
-
2919
- * {@link ByteBuffer#offset} and {@link ByteBuffer#limit}. This is an alias of {@link ByteBuffer#toBuffer}.
2920
-
2921
- * @function
2922
-
2923
- * @param {boolean=} forceCopy If `true` returns a copy, otherwise returns a view referencing the same memory.
2924
-
2925
- * Defaults to `false`
2926
-
2927
- * @returns {!ArrayBuffer} Contents as an ArrayBuffer
2928
-
2929
- * @expose
2930
-
2931
- */
2932
-
927
+ * Returns a raw buffer compacted to contain this ByteBuffer's contents. Contents are the bytes between
928
+ * {@link ByteBuffer#offset} and {@link ByteBuffer#limit}. This is an alias of {@link ByteBuffer#toBuffer}.
929
+ * @function
930
+ * @param {boolean=} forceCopy If `true` returns a copy, otherwise returns a view referencing the same memory.
931
+ * Defaults to `false`
932
+ * @returns {!ArrayBuffer} Contents as an ArrayBuffer
933
+ * @expose
934
+ */
2933
935
  toArrayBuffer = this.toBuffer
2934
936
 
2935
937
  writeVarint32(value, offset) {
@@ -2970,44 +972,6 @@ export class ByteBuffer {
2970
972
  return size
2971
973
  }
2972
974
 
2973
- readVarint32 = function (offset) {
2974
- const relative = typeof offset === 'undefined'
2975
- if (relative) offset = this.offset
2976
- if (!this.noAssert) {
2977
- if (typeof offset !== 'number' || offset % 1 !== 0) {
2978
- throw TypeError('Illegal offset: ' + offset + ' (not an integer)')
2979
- }
2980
- offset >>>= 0
2981
- if (offset < 0 || offset + 1 > this.buffer.byteLength) {
2982
- throw RangeError('Illegal offset: 0 <= ' + offset + ' (+' + 1 + ') <= ' + this.buffer.byteLength)
2983
- }
2984
- }
2985
- let c = 0
2986
- let value = 0 >>> 0
2987
- let b
2988
- do {
2989
- if (!this.noAssert && offset > this.limit) {
2990
- const err = Error('Truncated')
2991
- err.truncated = true
2992
- throw err
2993
- }
2994
- b = this.view.getUint8(offset++)
2995
- if (c < 5) {
2996
- value |= (b & 0x7f) << (7 * c)
2997
- }
2998
- ++c
2999
- } while ((b & 0x80) !== 0)
3000
- value |= 0
3001
- if (relative) {
3002
- this.offset = offset
3003
- return value
3004
- }
3005
- return {
3006
- value,
3007
- length: c
3008
- }
3009
- }
3010
-
3011
975
  calculateVarint32(value) {
3012
976
  // ref: src/google/protobuf/io/coded_stream.cc
3013
977
  value = value >>> 0
@@ -3058,132 +1022,7 @@ export class ByteBuffer {
3058
1022
  }
3059
1023
  return offset - start
3060
1024
  }
3061
-
3062
- readVString(offset) {
3063
- const relative = typeof offset === 'undefined'
3064
- if (relative) offset = this.offset
3065
- if (!this.noAssert) {
3066
- if (typeof offset !== 'number' || offset % 1 !== 0) {
3067
- throw TypeError('Illegal offset: ' + offset + ' (not an integer)')
3068
- }
3069
- offset >>>= 0
3070
- if (offset < 0 || offset + 1 > this.buffer.byteLength) {
3071
- throw RangeError('Illegal offset: 0 <= ' + offset + ' (+' + 1 + ') <= ' + this.buffer.byteLength)
3072
- }
3073
- }
3074
- const start = offset
3075
- const len = this.readVarint32(offset)
3076
- const str = this.readUTF8String(len.value, ByteBuffer.METRICS_BYTES, (offset += len.length))
3077
- offset += str.length
3078
- if (relative) {
3079
- this.offset = offset
3080
- return str.string
3081
- } else {
3082
- return {
3083
- string: str.string,
3084
- length: offset - start
3085
- }
3086
- }
3087
- }
3088
-
3089
- readUTF8String(length, metrics, offset) {
3090
- if (typeof metrics === 'number') {
3091
- offset = metrics
3092
- metrics = undefined
3093
- }
3094
- const relative = typeof offset === 'undefined'
3095
- if (relative) offset = this.offset
3096
- if (typeof metrics === 'undefined') metrics = ByteBuffer.METRICS_CHARS
3097
- if (!this.noAssert) {
3098
- if (typeof length !== 'number' || length % 1 !== 0) {
3099
- throw TypeError('Illegal length: ' + length + ' (not an integer)')
3100
- }
3101
- length |= 0
3102
- if (typeof offset !== 'number' || offset % 1 !== 0) {
3103
- throw TypeError('Illegal offset: ' + offset + ' (not an integer)')
3104
- }
3105
- offset >>>= 0
3106
- if (offset < 0 || offset + 0 > this.buffer.byteLength) {
3107
- throw RangeError('Illegal offset: 0 <= ' + offset + ' (+' + 0 + ') <= ' + this.buffer.byteLength)
3108
- }
3109
- }
3110
- let i = 0
3111
- const start = offset
3112
- let sd
3113
- if (metrics === ByteBuffer.METRICS_CHARS) {
3114
- // The same for node and the browser
3115
- sd = stringDestination()
3116
- utfx.decodeUTF8(
3117
- function () {
3118
- return i < length && offset < this.limit ? this.view.getUint8(offset++) : null
3119
- }.bind(this),
3120
- function (cp) {
3121
- ++i
3122
- utfx.UTF8toUTF16(cp, sd)
3123
- }
3124
- )
3125
- if (i !== length) {
3126
- throw RangeError('Illegal range: Truncated data, ' + i + ' == ' + length)
3127
- }
3128
- if (relative) {
3129
- this.offset = offset
3130
- return sd()
3131
- } else {
3132
- return {
3133
- string: sd(),
3134
- length: offset - start
3135
- }
3136
- }
3137
- } else if (metrics === ByteBuffer.METRICS_BYTES) {
3138
- if (!this.noAssert) {
3139
- if (typeof offset !== 'number' || offset % 1 !== 0) {
3140
- throw TypeError('Illegal offset: ' + offset + ' (not an integer)')
3141
- }
3142
- offset >>>= 0
3143
- if (offset < 0 || offset + length > this.buffer.byteLength) {
3144
- throw RangeError('Illegal offset: 0 <= ' + offset + ' (+' + length + ') <= ' + this.buffer.byteLength)
3145
- }
3146
- }
3147
- const k = offset + length
3148
- utfx.decodeUTF8toUTF16(
3149
- function () {
3150
- return offset < k ? this.view.getUint8(offset++) : null
3151
- }.bind(this),
3152
- (sd = stringDestination()),
3153
- this.noAssert
3154
- )
3155
- if (offset !== k) {
3156
- throw RangeError('Illegal range: Truncated data, ' + offset + ' == ' + k)
3157
- }
3158
- if (relative) {
3159
- this.offset = offset
3160
- return sd()
3161
- } else {
3162
- return {
3163
- string: sd(),
3164
- length: offset - start
3165
- }
3166
- }
3167
- } else {
3168
- throw TypeError('Unsupported metrics: ' + metrics)
3169
- }
3170
- }
3171
- }
3172
- function stringDestination() {
3173
- const cs = []
3174
- const ps = []
3175
- return function () {
3176
- if (arguments.length === 0) {
3177
- return ps.join('') + stringFromCharCode.apply(String, cs)
3178
- }
3179
- if (cs.length + arguments.length > 1024) {
3180
- ps.push(stringFromCharCode.apply(String, cs))
3181
- cs.length = 0
3182
- }
3183
- Array.prototype.push.apply(cs, arguments)
3184
- }
3185
1025
  }
3186
- const stringFromCharCode = String.fromCharCode
3187
1026
 
3188
1027
  function stringSource(s) {
3189
1028
  let i = 0