-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.ts
1507 lines (1409 loc) · 61.8 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2013-2014 Daniel Wirtz <dcode@dcode.io>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* @license bytebuffer.js (c) 2015 Daniel Wirtz <dcode@dcode.io>
* Backing buffer: ArrayBuffer, Accessor: DataView
* Released under the Apache License, Version 2.0
* see: https://github.com/dcodeIO/bytebuffer.js for details
* @module @xmcl/bytebuffer
*/
export class ByteBuffer {
/**
* ByteBuffer version.
* @type {string}
* @const
* @expose
*/
static VERSION = '0.0.1'
/**
* Little endian constant that can be used instead of its boolean value. Evaluates to `true`.
* @type {boolean}
* @const
* @expose
*/
static LITTLE_ENDIAN = true
/**
* Big endian constant that can be used instead of its boolean value. Evaluates to `false`.
* @type {boolean}
* @const
* @expose
*/
static BIG_ENDIAN = false
/**
* Default initial capacity of `16`.
* @type {number}
* @expose
*/
static DEFAULT_CAPACITY = 16
/**
* Default endianess of `false` for big endian.
* @type {boolean}
* @expose
*/
static DEFAULT_ENDIAN: boolean = ByteBuffer.BIG_ENDIAN
/**
* Default no assertions flag of `false`.
* @type {boolean}
* @expose
*/
static DEFAULT_NOASSERT = false
/**
* Backing ArrayBuffer.
* @type {!ArrayBuffer}
* @expose
*/
buffer: ArrayBuffer
/**
* DataView utilized to manipulate the backing buffer. Becomes `null` if the backing buffer has a capacity of `0`.
* @type {?DataView}
* @expose
*/
view: DataView
/**
* Absolute read/write offset.
* @type {number}
* @expose
* @see ByteBuffer#flip
* @see ByteBuffer#clear
*/
offset: number
/**
* Marked offset.
* @type {number}
* @expose
* @see ByteBuffer#mark
* @see ByteBuffer#reset
*/
markedOffset: number
/**
* Absolute limit of the contained data. Set to the backing buffer's capacity upon allocation.
* @type {number}
* @expose
* @see ByteBuffer#flip
* @see ByteBuffer#clear
*/
limit: number
/**
* Whether to use little endian byte order, defaults to `false` for big endian.
* @type {boolean}
* @expose
*/
littleEndian: boolean
/**
* Whether to skip assertions of offsets and values, defaults to `false`.
* @type {boolean}
* @expose
*/
noAssert: boolean
/**
* Constructs a new ByteBuffer.
* @class The swiss army knife for binary data in JavaScript.
* @exports ByteBuffer
* @constructor
* @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
* @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
* {@link ByteBuffer.DEFAULT_ENDIAN}.
* @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
* {@link ByteBuffer.DEFAULT_NOASSERT}.
* @expose
*/
constructor(capacity?: number, littleEndian?: boolean, noAssert?: boolean) {
if (typeof capacity === 'undefined') { capacity = ByteBuffer.DEFAULT_CAPACITY }
if (typeof littleEndian === 'undefined') { littleEndian = ByteBuffer.DEFAULT_ENDIAN }
if (typeof noAssert === 'undefined') { noAssert = ByteBuffer.DEFAULT_NOASSERT }
if (!noAssert) {
capacity = capacity | 0
if (capacity < 0) { throw RangeError('Illegal capacity') }
littleEndian = !!littleEndian
noAssert = !!noAssert
}
this.buffer = capacity === 0 ? EMPTY_BUFFER : new ArrayBuffer(capacity)
this.view = capacity === 0 ? new DataView(EMPTY_BUFFER) : new DataView(this.buffer)
this.offset = 0
this.markedOffset = -1
this.limit = capacity
this.littleEndian = littleEndian
this.noAssert = noAssert
}
/**
* Gets the accessor type.
* @returns {Function} `Buffer` under node.js, `Uint8Array` respectively `DataView` in the browser (classes)
* @expose
*/
static accessor = function (): Function {
return DataView
}
/**
* Allocates a new ByteBuffer backed by a buffer of the specified capacity.
* @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
* @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
* {@link ByteBuffer.DEFAULT_ENDIAN}.
* @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
* {@link ByteBuffer.DEFAULT_NOASSERT}.
* @returns {!ByteBuffer}
* @expose
*/
static allocate = function (capacity?: number, littleEndian?: boolean, noAssert?: boolean): ByteBuffer {
return new ByteBuffer(capacity, littleEndian, noAssert)
}
/**
* Concatenates multiple ByteBuffers into one.
* @param {!Array.<!ByteBuffer|!ArrayBuffer|!Uint8Array>} buffers Buffers to concatenate
* @param {(string|boolean)=} encoding String encoding if `buffers` contains a string ("base64", "hex", "binary",
* defaults to "utf8")
* @param {boolean=} littleEndian Whether to use little or big endian byte order for the resulting ByteBuffer. Defaults
* to {@link ByteBuffer.DEFAULT_ENDIAN}.
* @param {boolean=} noAssert Whether to skip assertions of offsets and values for the resulting ByteBuffer. Defaults to
* {@link ByteBuffer.DEFAULT_NOASSERT}.
* @returns {!ByteBuffer} Concatenated ByteBuffer
* @expose
*/
static concat = function (buffers: Array<ByteBuffer | ArrayBuffer | Uint8Array>, littleEndian?: boolean, noAssert?: boolean): ByteBuffer {
let capacity = 0
const k = buffers.length
let length
for (let i = 0, length; i < k; ++i) {
const buf = buffers[i]
if (!(buf instanceof ByteBuffer)) { buffers[i] = ByteBuffer.wrap(buf) }
// @ts-ignore
length = buffers[i].limit - buffers[i].offset
if (length > 0) { capacity += length }
}
if (capacity === 0) { return new ByteBuffer(0, littleEndian, noAssert) }
const bb = new ByteBuffer(capacity, littleEndian, noAssert)
let bi
const view = new Uint8Array(bb.buffer)
let i = 0
while (i < k) {
bi = buffers[i++]
// @ts-ignore
length = bi.limit - bi.offset
if (length <= 0) { continue }
// @ts-ignore
view.set(new Uint8Array(bi.buffer).subarray(bi.offset, bi.limit), bb.offset)
bb.offset += length
}
bb.limit = bb.offset
bb.offset = 0
return bb
}
/**
* Gets the backing buffer type.
* @returns {Function} `Buffer` under node.js, `ArrayBuffer` in the browser (classes)
* @expose
*/
static type = function (): Function {
return ArrayBuffer
}
/**
* Wraps a buffer or a string. Sets the allocated ByteBuffer's {@link ByteBuffer#offset} to `0` and its
* {@link ByteBuffer#limit} to the length of the wrapped data.
* @param {!ByteBuffer|!ArrayBuffer|!Uint8Array|string|!Array.<number>} buffer Anything that can be wrapped
* @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
* {@link ByteBuffer.DEFAULT_ENDIAN}.
* @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
* {@link ByteBuffer.DEFAULT_NOASSERT}.
* @returns {!ByteBuffer} A ByteBuffer wrapping `buffer`
* @expose
*/
static wrap = function (buffer: ArrayBuffer | number[], littleEndian?: boolean, noAssert?: boolean): ByteBuffer {
if (buffer === null || typeof buffer !== 'object') { throw TypeError('Illegal buffer') }
let bb
if (buffer instanceof ByteBuffer) {
bb = buffer.clone()
bb.markedOffset = -1
return bb
}
if (buffer instanceof Uint8Array) { // Extract ArrayBuffer from Uint8Array
bb = new ByteBuffer(0, littleEndian, noAssert)
if (buffer.length > 0) { // Avoid references to more than one EMPTY_BUFFER
bb.buffer = buffer.buffer
bb.offset = buffer.byteOffset
bb.limit = buffer.byteOffset + buffer.byteLength
bb.view = new DataView(buffer.buffer)
}
} else if (buffer instanceof ArrayBuffer) { // Reuse ArrayBuffer
bb = new ByteBuffer(0, littleEndian, noAssert)
if (buffer.byteLength > 0) {
bb.buffer = buffer
bb.offset = 0
bb.limit = buffer.byteLength
bb.view = buffer.byteLength > 0 ? new DataView(buffer) : new DataView(EMPTY_BUFFER)
}
} else if (Object.prototype.toString.call(buffer) === '[object Array]') { // Create from octets
bb = new ByteBuffer(buffer.length, littleEndian, noAssert)
bb.limit = buffer.length
for (let i = 0; i < buffer.length; ++i) { bb.view.setUint8(i, buffer[i]) }
} else { throw TypeError('Illegal buffer') } // Otherwise fail
return bb
}
/**
* Reads the specified number of bytes.
* @param {number} length Number of bytes to read
* @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `length` if omitted.
* @returns {!ByteBuffer}
* @expose
*/
readBytes(length: number, offset?: number): ByteBuffer {
const relative = typeof offset === 'undefined'
if (relative) { offset = this.offset }
if (!this.noAssert) {
if (typeof offset !== 'number' || offset % 1 !== 0) { throw TypeError('Illegal offset: ' + offset + ' (not an integer)') }
offset >>>= 0
if (offset < 0 || offset + length > this.buffer.byteLength) { throw RangeError('Illegal offset: 0 <= ' + offset + ' (+' + length + ') <= ' + this.buffer.byteLength) }
}
const slice = this.slice(offset, offset! + length)
if (relative) { this.offset += length }
return slice
}
/**
* Writes a payload of bytes. This is an alias of {@link ByteBuffer#append}.
* @function
* @param {!ByteBuffer|!ArrayBuffer|!Uint8Array|string} source Data to write. If `source` is a ByteBuffer, its offsets
* will be modified according to the performed read operation.
* @param {(string|number)=} encoding Encoding if `data` is a string ("base64", "hex", "binary", defaults to "utf8")
* @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
* written if omitted.
* @returns {!ByteBuffer} this
* @expose
*/
writeBytes = this.append
// types/ints/int8
/**
* Writes an 8bit signed integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
* @returns {!ByteBuffer} this
* @expose
*/
writeInt8(value: number, offset?: number): ByteBuffer {
const relative = typeof offset === 'undefined'
if (relative) { offset = this.offset }
if (!this.noAssert) {
if (typeof value !== 'number' || value % 1 !== 0) { throw TypeError('Illegal value: ' + value + ' (not an integer)') }
value |= 0
if (typeof offset !== 'number' || offset % 1 !== 0) { throw TypeError('Illegal offset: ' + offset + ' (not an integer)') }
offset >>>= 0
if (offset < 0 || offset + 0 > this.buffer.byteLength) { throw RangeError('Illegal offset: 0 <= ' + offset + ' (+' + 0 + ') <= ' + this.buffer.byteLength) }
}
offset! += 1
let capacity0 = this.buffer.byteLength
if (offset! > capacity0) { this.resize((capacity0 *= 2) > offset! ? capacity0 : offset!) }
offset! -= 1
this.view.setInt8(offset!, value)
if (relative) { this.offset += 1 }
return this
}
/**
* Writes an 8bit signed integer. This is an alias of {@link ByteBuffer#writeInt8}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
* @returns {!ByteBuffer} this
* @expose
*/
writeByte = this.writeInt8
/**
* Reads an 8bit signed integer.
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
* @returns {number} Value read
* @expose
*/
readInt8(offset?: number): number {
const relative = typeof offset === 'undefined'
if (relative) { offset = this.offset }
if (!this.noAssert) {
if (typeof offset !== 'number' || offset % 1 !== 0) { throw TypeError('Illegal offset: ' + offset + ' (not an integer)') }
offset >>>= 0
if (offset < 0 || offset + 1 > this.buffer.byteLength) { throw RangeError('Illegal offset: 0 <= ' + offset + ' (+' + 1 + ') <= ' + this.buffer.byteLength) }
}
const value = this.view.getInt8(offset!)
if (relative) { this.offset += 1 }
return value
}
/**
* Reads an 8bit signed integer. This is an alias of {@link ByteBuffer#readInt8}.
* @function
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
* @returns {number} Value read
* @expose
*/
readByte = this.readInt8
/**
* Writes an 8bit unsigned integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
* @returns {!ByteBuffer} this
* @expose
*/
writeUint8(value: number, offset?: number): ByteBuffer {
const relative = typeof offset === 'undefined'
if (relative) { offset = this.offset }
if (!this.noAssert) {
if (typeof value !== 'number' || value % 1 !== 0) { throw TypeError('Illegal value: ' + value + ' (not an integer)') }
value >>>= 0
if (typeof offset !== 'number' || offset % 1 !== 0) { throw TypeError('Illegal offset: ' + offset + ' (not an integer)') }
offset >>>= 0
if (offset < 0 || offset + 0 > this.buffer.byteLength) { throw RangeError('Illegal offset: 0 <= ' + offset + ' (+' + 0 + ') <= ' + this.buffer.byteLength) }
}
offset! += 1
let capacity1 = this.buffer.byteLength
if (offset! > capacity1) { this.resize((capacity1 *= 2) > offset! ? capacity1 : offset!) }
offset! -= 1
this.view.setUint8(offset!, value)
if (relative) { this.offset += 1 }
return this
}
/**
* Writes an 8bit unsigned integer. This is an alias of {@link ByteBuffer#writeUint8}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
* @returns {!ByteBuffer} this
* @expose
*/
writeUInt8 = this.writeUint8
/**
* Reads an 8bit unsigned integer.
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
* @returns {number} Value read
* @expose
*/
readUint8(offset?: number): number {
const relative = typeof offset === 'undefined'
if (relative) { offset = this.offset }
if (!this.noAssert) {
if (typeof offset !== 'number' || offset % 1 !== 0) { throw TypeError('Illegal offset: ' + offset + ' (not an integer)') }
offset >>>= 0
if (offset < 0 || offset + 1 > this.buffer.byteLength) { throw RangeError('Illegal offset: 0 <= ' + offset + ' (+' + 1 + ') <= ' + this.buffer.byteLength) }
}
const value = this.view.getUint8(offset!)
if (relative) { this.offset += 1 }
return value
}
/**
* Reads an 8bit unsigned integer. This is an alias of {@link ByteBuffer#readUint8}.
* @function
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
* @returns {number} Value read
* @expose
*/
readUInt8 = this.readUint8
// types/ints/int16
/**
* Writes a 16bit signed integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
* @throws {TypeError} If `offset` or `value` is not a valid number
* @throws {RangeError} If `offset` is out of bounds
* @expose
*/
writeInt16(value: number, offset?: number) {
const relative = typeof offset === 'undefined'
if (relative) { offset = this.offset }
if (!this.noAssert) {
if (typeof value !== 'number' || value % 1 !== 0) { throw TypeError('Illegal value: ' + value + ' (not an integer)') }
value |= 0
if (typeof offset !== 'number' || offset % 1 !== 0) { throw TypeError('Illegal offset: ' + offset + ' (not an integer)') }
offset >>>= 0
if (offset < 0 || offset + 0 > this.buffer.byteLength) { throw RangeError('Illegal offset: 0 <= ' + offset + ' (+' + 0 + ') <= ' + this.buffer.byteLength) }
}
offset! += 2
let capacity2 = this.buffer.byteLength
if (offset! > capacity2) { this.resize((capacity2 *= 2) > offset! ? capacity2 : offset!) }
offset! -= 2
this.view.setInt16(offset!, value, this.littleEndian)
if (relative) { this.offset += 2 }
return this
}
/**
* Writes a 16bit signed integer. This is an alias of {@link ByteBuffer#writeInt16}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
* @throws {TypeError} If `offset` or `value` is not a valid number
* @throws {RangeError} If `offset` is out of bounds
* @expose
*/
writeShort = this.writeInt16
/**
* Reads a 16bit signed integer.
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
* @returns {number} Value read
* @throws {TypeError} If `offset` is not a valid number
* @throws {RangeError} If `offset` is out of bounds
* @expose
*/
readInt16(offset?: number): number {
const relative = typeof offset === 'undefined'
if (typeof offset === 'undefined') { offset = this.offset }
if (!this.noAssert) {
if (typeof offset !== 'number' || offset % 1 !== 0) { throw TypeError('Illegal offset: ' + offset + ' (not an integer)') }
offset >>>= 0
if (offset < 0 || offset + 2 > this.buffer.byteLength) { throw RangeError('Illegal offset: 0 <= ' + offset + ' (+' + 2 + ') <= ' + this.buffer.byteLength) }
}
const value = this.view.getInt16(offset, this.littleEndian)
if (relative) { this.offset += 2 }
return value
}
/**
* Reads a 16bit signed integer. This is an alias of {@link ByteBuffer#readInt16}.
* @function
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
* @returns {number} Value read
* @throws {TypeError} If `offset` is not a valid number
* @throws {RangeError} If `offset` is out of bounds
* @expose
*/
readShort = this.readInt16
/**
* Writes a 16bit unsigned integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
* @throws {TypeError} If `offset` or `value` is not a valid number
* @throws {RangeError} If `offset` is out of bounds
* @expose
*/
writeUint16(value: number, offset?: number) {
const relative = typeof offset === 'undefined'
if (relative) { offset = this.offset }
if (!this.noAssert) {
if (typeof value !== 'number' || value % 1 !== 0) { throw TypeError('Illegal value: ' + value + ' (not an integer)') }
value >>>= 0
if (typeof offset !== 'number' || offset % 1 !== 0) { throw TypeError('Illegal offset: ' + offset + ' (not an integer)') }
offset >>>= 0
if (offset < 0 || offset + 0 > this.buffer.byteLength) { throw RangeError('Illegal offset: 0 <= ' + offset + ' (+' + 0 + ') <= ' + this.buffer.byteLength) }
}
offset! += 2
let capacity3 = this.buffer.byteLength
if (offset! > capacity3) { this.resize((capacity3 *= 2) > offset! ? capacity3 : offset!) }
offset! -= 2
this.view.setUint16(offset!, value, this.littleEndian)
if (relative) { this.offset += 2 }
return this
}
/**
* Writes a 16bit unsigned integer. This is an alias of {@link ByteBuffer#writeUint16}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
* @throws {TypeError} If `offset` or `value` is not a valid number
* @throws {RangeError} If `offset` is out of bounds
* @expose
*/
writeUInt16 = this.writeUint16
/**
* Reads a 16bit unsigned integer.
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
* @returns {number} Value read
* @throws {TypeError} If `offset` is not a valid number
* @throws {RangeError} If `offset` is out of bounds
* @expose
*/
readUint16(offset?: number): number {
const relative = typeof offset === 'undefined'
if (relative) { offset = this.offset }
if (!this.noAssert) {
if (typeof offset !== 'number' || offset % 1 !== 0) { throw TypeError('Illegal offset: ' + offset + ' (not an integer)') }
offset >>>= 0
if (offset < 0 || offset + 2 > this.buffer.byteLength) { throw RangeError('Illegal offset: 0 <= ' + offset + ' (+' + 2 + ') <= ' + this.buffer.byteLength) }
}
const value = this.view.getUint16(offset!, this.littleEndian)
if (relative) { this.offset += 2 }
return value
}
/**
* Reads a 16bit unsigned integer. This is an alias of {@link ByteBuffer#readUint16}.
* @function
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
* @returns {number} Value read
* @throws {TypeError} If `offset` is not a valid number
* @throws {RangeError} If `offset` is out of bounds
* @expose
*/
readUInt16 = this.readUint16
// types/ints/int32
/**
* Writes a 32bit signed integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
* @expose
*/
writeInt32(value: number, offset?: number) {
const relative = typeof offset === 'undefined'
if (relative) { offset = this.offset }
if (!this.noAssert) {
if (typeof value !== 'number' || value % 1 !== 0) { throw TypeError('Illegal value: ' + value + ' (not an integer)') }
value |= 0
if (typeof offset !== 'number' || offset % 1 !== 0) { throw TypeError('Illegal offset: ' + offset + ' (not an integer)') }
offset >>>= 0
if (offset < 0 || offset + 0 > this.buffer.byteLength) { throw RangeError('Illegal offset: 0 <= ' + offset + ' (+' + 0 + ') <= ' + this.buffer.byteLength) }
}
offset! += 4
let capacity4 = this.buffer.byteLength
if (offset! > capacity4) { this.resize((capacity4 *= 2) > offset! ? capacity4 : offset!) }
offset! -= 4
this.view.setInt32(offset!, value, this.littleEndian)
if (relative) { this.offset += 4 }
return this
}
/**
* Writes a 32bit signed integer. This is an alias of {@link ByteBuffer#writeInt32}.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
* @expose
*/
writeInt = this.writeInt32
/**
* Reads a 32bit signed integer.
* @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
* @returns {number} Value read
* @expose
*/
readInt32(offset?: number): number {
const relative = typeof offset === 'undefined'
if (relative) { offset = this.offset }
if (!this.noAssert) {
if (typeof offset !== 'number' || offset % 1 !== 0) { throw TypeError('Illegal offset: ' + offset + ' (not an integer)') }
offset >>>= 0
if (offset < 0 || offset + 4 > this.buffer.byteLength) { throw RangeError('Illegal offset: 0 <= ' + offset + ' (+' + 4 + ') <= ' + this.buffer.byteLength) }
}
const value = this.view.getInt32(offset!, this.littleEndian)
if (relative) { this.offset += 4 }
return value
}
/**
* Reads a 32bit signed integer. This is an alias of {@link ByteBuffer#readInt32}.
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `4` if omitted.
* @returns {number} Value read
* @expose
*/
readInt = this.readInt32
/**
* Writes a 32bit unsigned integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
* @expose
*/
writeUint32(value: number, offset?: number) {
const relative = typeof offset === 'undefined'
if (relative) { offset = this.offset }
if (!this.noAssert) {
if (typeof value !== 'number' || value % 1 !== 0) { throw TypeError('Illegal value: ' + value + ' (not an integer)') }
value >>>= 0
if (typeof offset !== 'number' || offset % 1 !== 0) { throw TypeError('Illegal offset: ' + offset + ' (not an integer)') }
offset >>>= 0
if (offset < 0 || offset + 0 > this.buffer.byteLength) { throw RangeError('Illegal offset: 0 <= ' + offset + ' (+' + 0 + ') <= ' + this.buffer.byteLength) }
}
offset! += 4
let capacity5 = this.buffer.byteLength
if (offset! > capacity5) { this.resize((capacity5 *= 2) > offset! ? capacity5 : offset!) }
offset! -= 4
this.view.setUint32(offset!, value, this.littleEndian)
if (relative) { this.offset += 4 }
return this
}
/**
* Writes a 32bit unsigned integer. This is an alias of {@link ByteBuffer#writeUint32}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
* @expose
*/
writeUInt32 = this.writeUint32
/**
* Reads a 32bit unsigned integer.
* @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
* @returns {number} Value read
* @expose
*/
readUint32(offset?: number): number {
const relative = typeof offset === 'undefined'
if (relative) { offset = this.offset }
if (!this.noAssert) {
if (typeof offset !== 'number' || offset % 1 !== 0) { throw TypeError('Illegal offset: ' + offset + ' (not an integer)') }
offset >>>= 0
if (offset < 0 || offset + 4 > this.buffer.byteLength) { throw RangeError('Illegal offset: 0 <= ' + offset + ' (+' + 4 + ') <= ' + this.buffer.byteLength) }
}
const value = this.view.getUint32(offset!, this.littleEndian)
if (relative) { this.offset += 4 }
return value
}
/**
* Reads a 32bit unsigned integer. This is an alias of {@link ByteBuffer#readUint32}.
* @function
* @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
* @returns {number} Value read
* @expose
*/
readUInt32 = this.readUint32
/**
* Writes a 32bit float.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
* @returns {!ByteBuffer} this
* @expose
*/
writeFloat32(value: number, offset?: number): ByteBuffer {
const relative = typeof offset === 'undefined'
if (relative) { offset = this.offset }
if (!this.noAssert) {
if (typeof value !== 'number') { throw TypeError('Illegal value: ' + value + ' (not a number)') }
if (typeof offset !== 'number' || offset % 1 !== 0) { throw TypeError('Illegal offset: ' + offset + ' (not an integer)') }
offset >>>= 0
if (offset < 0 || offset + 0 > this.buffer.byteLength) { throw RangeError('Illegal offset: 0 <= ' + offset + ' (+' + 0 + ') <= ' + this.buffer.byteLength) }
}
offset! += 4
let capacity8 = this.buffer.byteLength
if (offset! > capacity8) { this.resize((capacity8 *= 2) > offset! ? capacity8 : offset!) }
offset! -= 4
this.view.setFloat32(offset!, value, this.littleEndian)
if (relative) { this.offset += 4 }
return this
}
/**
* Writes a 32bit float. This is an alias of {@link ByteBuffer#writeFloat32}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
* @returns {!ByteBuffer} this
* @expose
*/
writeFloat = this.writeFloat32
/**
* Reads a 32bit float.
* @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
* @returns {number}
* @expose
*/
readFloat32(offset?: number): number {
const relative = typeof offset === 'undefined'
if (relative) { offset = this.offset }
if (!this.noAssert) {
if (typeof offset !== 'number' || offset % 1 !== 0) { throw TypeError('Illegal offset: ' + offset + ' (not an integer)') }
offset >>>= 0
if (offset < 0 || offset + 4 > this.buffer.byteLength) { throw RangeError('Illegal offset: 0 <= ' + offset + ' (+' + 4 + ') <= ' + this.buffer.byteLength) }
}
const value = this.view.getFloat32(offset!, this.littleEndian)
if (relative) { this.offset += 4 }
return value
}
/**
* Reads a 32bit float. This is an alias of {@link ByteBuffer#readFloat32}.
* @function
* @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
* @returns {number}
* @expose
*/
readFloat = this.readFloat32
// types/floats/float64
/**
* Writes a 64bit float.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
* @returns {!ByteBuffer} this
* @expose
*/
writeFloat64(value: number, offset?: number): ByteBuffer {
const relative = typeof offset === 'undefined'
if (relative) { offset = this.offset }
if (!this.noAssert) {
if (typeof value !== 'number') { throw TypeError('Illegal value: ' + value + ' (not a number)') }
if (typeof offset !== 'number' || offset % 1 !== 0) { throw TypeError('Illegal offset: ' + offset + ' (not an integer)') }
offset >>>= 0
if (offset < 0 || offset + 0 > this.buffer.byteLength) { throw RangeError('Illegal offset: 0 <= ' + offset + ' (+' + 0 + ') <= ' + this.buffer.byteLength) }
}
offset! += 8
let capacity9 = this.buffer.byteLength
if (offset! > capacity9) { this.resize((capacity9 *= 2) > offset! ? capacity9 : offset!) }
offset! -= 8
this.view.setFloat64(offset!, value, this.littleEndian)
if (relative) { this.offset += 8 }
return this
}
/**
* Writes a 64bit float. This is an alias of {@link ByteBuffer#writeFloat64}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
* @returns {!ByteBuffer} this
* @expose
*/
writeDouble = this.writeFloat64
/**
* Reads a 64bit float.
* @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
* @returns {number}
* @expose
*/
readFloat64(offset?: number): number {
const relative = typeof offset === 'undefined'
if (relative) { offset = this.offset }
if (!this.noAssert) {
if (typeof offset !== 'number' || offset % 1 !== 0) { throw TypeError('Illegal offset: ' + offset + ' (not an integer)') }
offset >>>= 0
if (offset < 0 || offset + 8 > this.buffer.byteLength) { throw RangeError('Illegal offset: 0 <= ' + offset + ' (+' + 8 + ') <= ' + this.buffer.byteLength) }
}
const value = this.view.getFloat64(offset!, this.littleEndian)
if (relative) { this.offset += 8 }
return value
}
/**
* Reads a 64bit float. This is an alias of {@link ByteBuffer#readFloat64}.
* @function
* @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
* @returns {number}
* @expose
*/
readDouble = this.readFloat64
/**
* Appends some data to this ByteBuffer. This will overwrite any contents behind the specified offset up to the appended
* data's length.
* @param {!ByteBuffer|!ArrayBuffer|!Uint8Array} source Data to append. If `source` is a ByteBuffer, its offsets
* will be modified according to the performed read operation.
* @param {(string|number)=} encoding Encoding if `data` is a string ("base64", "hex", "binary", defaults to "utf8")
* @param {number=} offset Offset to append at. Will use and increase {@link ByteBuffer#offset} by the number of bytes
* written if omitted.
* @returns {!ByteBuffer} this
* @expose
* @example A relative `<01 02>03.append(<04 05>)` will result in `<01 02 04 05>, 04 05|`
* @example An absolute `<01 02>03.append(04 05>, 1)` will result in `<01 04>05, 04 05|`
*/
append(source: ByteBuffer | ArrayBuffer | Uint8Array, offset?: number): ByteBuffer {
const relative = typeof offset === 'undefined'
if (relative) { offset = this.offset }
if (!this.noAssert) {
if (typeof offset !== 'number' || offset % 1 !== 0) { throw TypeError('Illegal offset: ' + offset + ' (not an integer)') }
offset >>>= 0
if (offset < 0 || offset + 0 > this.buffer.byteLength) { throw RangeError('Illegal offset: 0 <= ' + offset + ' (+' + 0 + ') <= ' + this.buffer.byteLength) }
}
if (!(source instanceof ByteBuffer)) { source = ByteBuffer.wrap(source) }
const length = source.limit - source.offset
if (length <= 0) { return this } // Nothing to append
offset! += length
let capacity16 = this.buffer.byteLength
if (offset! > capacity16) { this.resize((capacity16 *= 2) > offset! ? capacity16 : offset!) }
offset! -= length
new Uint8Array(this.buffer, offset).set(new Uint8Array(source.buffer).subarray(source.offset, source.limit))
source.offset += length
if (relative) { this.offset += length }
return this
}
/**
* Appends this ByteBuffer's contents to another ByteBuffer. This will overwrite any contents at and after the
specified offset up to the length of this ByteBuffer's data.
* @param {!ByteBuffer} target Target ByteBuffer
* @param {number=} offset Offset to append to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
* read if omitted.
* @returns {!ByteBuffer} this
* @expose
* @see ByteBuffer#append
*/
appendTo(target: ByteBuffer, offset?: number): ByteBuffer {
target.append(this, offset)
return this
}
/**
* Enables or disables assertions of argument types and offsets. Assertions are enabled by default but you can opt to
* disable them if your code already makes sure that everything is valid.
* @param {boolean} assert `true` to enable assertions, otherwise `false`
* @returns {!ByteBuffer} this
* @expose
*/
assert(assert: boolean): ByteBuffer {
this.noAssert = !assert
return this
}
/**
* Gets the capacity of this ByteBuffer's backing buffer.
* @returns {number} Capacity of the backing buffer
* @expose
*/
capacity(): number {
return this.buffer.byteLength
}
/**
* Clears this ByteBuffer's offsets by setting {@link ByteBuffer#offset} to `0` and {@link ByteBuffer#limit} to the
* backing buffer's capacity. Discards {@link ByteBuffer#markedOffset}.
* @returns {!ByteBuffer} this
* @expose
*/
clear(): ByteBuffer {
this.offset = 0
this.limit = this.buffer.byteLength
this.markedOffset = -1
return this
}
/**
* Creates a cloned instance of this ByteBuffer, preset with this ByteBuffer's values for {@link ByteBuffer#offset},
* {@link ByteBuffer#markedOffset} and {@link ByteBuffer#limit}.
* @param {boolean=} copy Whether to copy the backing buffer or to return another view on the same, defaults to `false`
* @returns {!ByteBuffer} Cloned instance
* @expose
*/
clone(copy?: boolean): ByteBuffer {
const bb = new ByteBuffer(0, this.littleEndian, this.noAssert)
if (copy) {
bb.buffer = new ArrayBuffer(this.buffer.byteLength)
new Uint8Array(bb.buffer).set(this.buffer as any)
bb.view = new DataView(bb.buffer)
} else {
bb.buffer = this.buffer
bb.view = this.view
}
bb.offset = this.offset
bb.markedOffset = this.markedOffset
bb.limit = this.limit
return bb
}
/**
* Compacts this ByteBuffer to be backed by a {@link ByteBuffer#buffer} of its contents' length. Contents are the bytes
* between {@link ByteBuffer#offset} and {@link ByteBuffer#limit}. Will set `offset = 0` and `limit = capacity` and
* adapt {@link ByteBuffer#markedOffset} to the same relative position if set.
* @param {number=} begin Offset to start at, defaults to {@link ByteBuffer#offset}
* @param {number=} end Offset to end at, defaults to {@link ByteBuffer#limit}
* @returns {!ByteBuffer} this
* @expose
*/
compact(begin: number | undefined, end: number | undefined): ByteBuffer {
if (typeof begin === 'undefined') { begin = this.offset }
if (typeof end === 'undefined') { end = this.limit }
if (!this.noAssert) {
if (typeof begin !== 'number' || begin % 1 !== 0) { throw TypeError('Illegal begin: Not an integer') }
begin >>>= 0
if (typeof end !== 'number' || end % 1 !== 0) { throw TypeError('Illegal end: Not an integer') }
end >>>= 0
if (begin < 0 || begin > end || end > this.buffer.byteLength) { throw RangeError('Illegal range: 0 <= ' + begin + ' <= ' + end + ' <= ' + this.buffer.byteLength) }
}
if (begin === 0 && end === this.buffer.byteLength) { return this } // Already compacted
const len = end - begin
if (len === 0) {
this.buffer = EMPTY_BUFFER
this.view = new DataView(EMPTY_BUFFER)
if (this.markedOffset >= 0) { this.markedOffset -= begin }
this.offset = 0
this.limit = 0
return this
}
const buffer = new ArrayBuffer(len)
new Uint8Array(buffer).set(new Uint8Array(this.buffer).subarray(begin, end))
this.buffer = buffer
this.view = new DataView(buffer)
if (this.markedOffset >= 0) { this.markedOffset -= begin }
this.offset = 0
this.limit = len
return this
}
/**
* Creates a copy of this ByteBuffer's contents. Contents are the bytes between {@link ByteBuffer#offset} and
* {@link ByteBuffer#limit}.
* @param {number=} begin Begin offset, defaults to {@link ByteBuffer#offset}.
* @param {number=} end End offset, defaults to {@link ByteBuffer#limit}.
* @returns {!ByteBuffer} Copy
* @expose
*/
copy(begin: number | undefined, end: number | undefined): ByteBuffer {
if (typeof begin === 'undefined') { begin = this.offset }
if (typeof end === 'undefined') { end = this.limit }
if (!this.noAssert) {
if (typeof begin !== 'number' || begin % 1 !== 0) { throw TypeError('Illegal begin: Not an integer') }
begin >>>= 0
if (typeof end !== 'number' || end % 1 !== 0) { throw TypeError('Illegal end: Not an integer') }
end >>>= 0
if (begin < 0 || begin > end || end > this.buffer.byteLength) { throw RangeError('Illegal range: 0 <= ' + begin + ' <= ' + end + ' <= ' + this.buffer.byteLength) }
}
if (begin === end) { return new ByteBuffer(0, this.littleEndian, this.noAssert) }
const capacity = end - begin
const bb = new ByteBuffer(capacity, this.littleEndian, this.noAssert)
bb.offset = 0
bb.limit = capacity
if (bb.markedOffset >= 0) { bb.markedOffset -= begin }
this.copyTo(bb, 0, begin, end)
return bb
}
/**
* Copies this ByteBuffer's contents to another ByteBuffer. Contents are the bytes between {@link ByteBuffer#offset} and
* {@link ByteBuffer#limit}.
* @param {!ByteBuffer} target Target ByteBuffer