-
Notifications
You must be signed in to change notification settings - Fork 3
/
bytebuf.ts
993 lines (900 loc) · 27.7 KB
/
bytebuf.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
export { ByteBuf }
export type { IntResult, StringResult }
/**
* A byte buffer.
*
* Supports the following data types:
* - Bool
* - Int (8, 16, 32, 64)
* - Uint (8, 16, 32, 64)
* - BigInt (64)
* - VarInt (unsigned, signed, and zigzag)
* - String (utf8, utf16, and delimited)
* - TypedArray
*
* @extends DataView
*/
class ByteBuf extends DataView {
/**
* Creates a byte buffer.
* @param source The data source.
* @param byteOffset The byte offset.
* @param byteLength The byte length.
* @returns A byte buffer.
*/
static from(source: BufferSource, byteOffset?: number, byteLength?: number): ByteBuf {
const buffer = source instanceof ArrayBuffer ? source : source.buffer
if (ArrayBuffer.isView(source)) {
byteOffset = source.byteOffset + (byteOffset || 0)
}
return byteLength === undefined
? new ByteBuf(buffer, byteOffset)
: new ByteBuf(buffer, byteOffset, Math.min(source.byteLength, byteLength))
}
/**
* The byte offset.
*/
#byteOffset: number = super.byteOffset
/**
* The byte offset.
* @returns The byte offset.
*/
get byteOffset(): number {
return this.#byteOffset
}
/**
* The number of remaining bytes.
* @returns The number of bytes remaining.
*/
get bytesRemaining(): number {
return this.byteLength - this.#byteOffset
}
/**
* Skips the byte offset.
* @param byteLength The byte length.
*/
skip(byteLength: number): void {
this.#byteOffset += byteLength
}
/**
* Resets the byte offset.
*/
reset(): void {
this.#byteOffset = super.byteOffset
}
/**
* Clears the byte buffer.
*/
clear(): void {
this.getUint8Array(0).fill(0)
}
/**
* Gets a boolean.
* @param byteOffset The byte offset.
*/
getBool(byteOffset: number): boolean {
return this.getInt8(byteOffset) !== 0
}
/**
* Reads the next boolean.
*/
readBool(): boolean {
return this.getInt8(this.#byteOffset++) !== 0
}
/**
* Sets a boolean.
* @param byteOffset The byte offset.
* @param value The value.
*/
setBool(byteOffset: number, value: boolean): void {
super.setInt8(byteOffset, value ? 1 : 0)
}
/**
* Writes the next boolean.
* @param value The value.
*/
writeBool(value: boolean): void {
this.setInt8(this.#byteOffset++, value ? 1 : 0)
}
/**
* Gets an signed byte.
* @param byteOffset The byte offset.
* @returns The value.
*/
getInt8(byteOffset: number): number {
return super.getInt8(byteOffset)
}
/**
* Reads the next signed byte.
* @returns The value.
*/
readInt8(): number {
return this.getInt8(this.#byteOffset++)
}
/**
* Sets a signed byte.
* @param byteOffset The byte offset.
* @param value The value.
*/
setInt8(byteOffset: number, value: number): void {
super.setInt8(byteOffset, value)
}
/**
* Writes the next signed byte.
* @param value The value.
*/
writeInt8(value: number): void {
this.setInt8(this.#byteOffset++, value)
}
/**
* Gets an unsigned byte.
* @param byteOffset The byte offset.
* @returns The value.
*/
getUint8(byteOffset: number): number {
return super.getUint8(byteOffset)
}
/**
* Reads the next unsigned byte.
* @returns The value.
*/
readUint8(): number {
return this.getUint8(this.#byteOffset++)
}
/**
* Sets an unsigned byte.
* @param byteOffset The byte offset.
* @param value The value.
*/
setUint8(byteOffset: number, value: number): void {
super.setUint8(byteOffset, value)
}
/**
* Writes the next signed byte.
* @param value The value.
*/
writeUint8(value: number): void {
this.setUint8(this.#byteOffset++, value)
}
/**
* Gets an signed short.
* @param byteOffset The byte offset.
* @param littleEndian If the value is little endian.
* @returns The value.
*/
getInt16(byteOffset: number, littleEndian?: boolean): number {
return super.getInt16(byteOffset, littleEndian)
}
/**
* Reads the next signed short.
* @param littleEndian If the value is little endian.
* @returns The value.
*/
readInt16(littleEndian?: boolean): number {
const value = this.getInt16(this.#byteOffset, littleEndian)
this.#byteOffset += 2
return value
}
/**
* Sets a signed short.
* @param byteOffset The byte offset.
* @param value The value.
* @param littleEndian If the value is little endian.
*/
setInt16(byteOffset: number, value: number, littleEndian?: boolean): void {
super.setInt16(byteOffset, value, littleEndian)
}
/**
* Writes the next signed short.
* @param value The value.
* @param littleEndian If the value is little endian.
*/
writeInt16(value: number, littleEndian?: boolean): void {
this.setInt16(this.#byteOffset, value, littleEndian)
this.#byteOffset += 2
}
/**
* Gets an unsigned short.
* @param byteOffset The byte offset.
* @param littleEndian If the value is little endian.
* @returns The value.
*/
getUint16(byteOffset: number, littleEndian?: boolean): number {
return super.getUint16(byteOffset, littleEndian)
}
/**
* Reads the next unsigned short.
* @param littleEndian If the value is little endian.
* @returns The value.
*/
readUint16(littleEndian?: boolean): number {
const value = this.getUint16(this.#byteOffset, littleEndian)
this.#byteOffset += 2
return value
}
/**
* Sets an unsigned short.
* @param byteOffset The byte offset.
* @param value The value.
* @param littleEndian If the value is little endian.
*/
setUint16(byteOffset: number, value: number, littleEndian?: boolean): void {
super.setUint16(byteOffset, value, littleEndian)
}
/**
* Writes the next signed short.
* @param value The value.
* @param littleEndian If the value is little endian.
*/
writeUint16(value: number, littleEndian?: boolean): void {
this.setUint16(this.#byteOffset, value, littleEndian)
this.#byteOffset += 2
}
/**
* Gets an signed integer.
* @param byteOffset The byte offset.
* @param littleEndian If the value is little endian.
* @returns The value.
*/
getInt32(byteOffset: number, littleEndian?: boolean): number {
return super.getInt32(byteOffset, littleEndian)
}
/**
* Reads the next signed integer.
* @param littleEndian If the value is little endian.
* @returns The value.
*/
readInt32(littleEndian?: boolean): number {
const value = this.getInt32(this.#byteOffset, littleEndian)
this.#byteOffset += 4
return value
}
/**
* Sets a signed integer.
* @param byteOffset The byte offset.
* @param value The value.
* @param littleEndian If the value is little endian.
*/
setInt32(byteOffset: number, value: number, littleEndian?: boolean): void {
super.setInt32(byteOffset, value, littleEndian)
}
/**
* Writes the next signed integer.
* @param value The value.
* @param littleEndian If the value is little endian.
*/
writeInt32(value: number, littleEndian?: boolean): void {
this.setInt32(this.#byteOffset, value, littleEndian)
this.#byteOffset += 4
}
/**
* Gets an unsigned integer.
* @param byteOffset The byte offset.
* @param littleEndian If the value is little endian.
* @returns The value.
*/
getUint32(byteOffset: number, littleEndian?: boolean): number {
return super.getUint32(byteOffset, littleEndian)
}
/**
* Reads the next unsigned integer.
* @param littleEndian If the value is little endian.
* @returns The value.
*/
readUint32(littleEndian?: boolean): number {
const value = this.getUint32(this.#byteOffset, littleEndian)
this.#byteOffset += 4
return value
}
/**
* Sets an unsigned integer.
* @param byteOffset The byte offset.
* @param value The value.
* @param littleEndian If the value is little endian.
*/
setUint32(byteOffset: number, value: number, littleEndian?: boolean): void {
super.setUint32(byteOffset, value, littleEndian)
}
/**
* Writes the next signed integer.
* @param value The value.
* @param littleEndian If the value is little endian.
*/
writeUint32(value: number, littleEndian?: boolean): void {
this.setUint32(this.#byteOffset, value, littleEndian)
this.#byteOffset += 4
}
/**
* Gets a float.
* @param byteOffset The byte offset.
* @param littleEndian If the value is little endian.
* @returns The value.
*/
getFloat32(byteOffset: number, littleEndian?: boolean): number {
return super.getFloat32(byteOffset, littleEndian)
}
/**
* Reads the next float.
* @param littleEndian If the value is little endian.
* @returns The value.
*/
readFloat32(littleEndian?: boolean): number {
const value = this.getFloat32(this.#byteOffset, littleEndian)
this.#byteOffset += 4
return value
}
/**
* Sets a float.
* @param byteOffset The byte offset.
* @param value The value.
* @param littleEndian If the value is little endian.
*/
setFloat32(byteOffset: number, value: number, littleEndian?: boolean): void {
super.setFloat32(byteOffset, value, littleEndian)
}
/**
* Writes the next float.
* @param value The value.
* @param littleEndian If the value is little endian.
*/
writeFloat32(value: number, littleEndian?: boolean): void {
this.setFloat32(this.#byteOffset, value, littleEndian)
this.#byteOffset += 4
}
/**
* Gets a double.
* @param byteOffset The byte offset.
* @param littleEndian If the value is little endian.
* @returns The value.
*/
getFloat64(byteOffset: number, littleEndian?: boolean): number {
return super.getFloat64(byteOffset, littleEndian)
}
/**
* Reads the next double.
* @param littleEndian If the value is little endian.
* @returns The value.
*/
readFloat64(littleEndian?: boolean): number {
const value = this.getFloat64(this.#byteOffset, littleEndian)
this.#byteOffset += 8
return value
}
/**
* Sets a double.
* @param byteOffset The byte offset.
* @param value The value.
* @param littleEndian If the value is little endian.
*/
setFloat64(byteOffset: number, value: number, littleEndian?: boolean): void {
super.setFloat64(byteOffset, value, littleEndian)
}
/**
* Writes the next double.
* @param value The value.
* @param littleEndian If the value is little endian.
*/
writeFloat64(value: number, littleEndian?: boolean): void {
this.setFloat64(this.#byteOffset, value, littleEndian)
this.#byteOffset += 8
}
/**
* Gets an signed long.
* @param byteOffset The byte offset.
* @param littleEndian If the value is little endian.
* @returns The value.
*/
getBigInt64(byteOffset: number, littleEndian?: boolean): bigint {
return super.getBigInt64(byteOffset, littleEndian)
}
/**
* Reads the next signed long.
* @param littleEndian If the value is little endian.
* @returns The value.
*/
readBigInt64(littleEndian?: boolean): bigint {
const value = this.getBigInt64(this.#byteOffset, littleEndian)
this.#byteOffset += 8
return value
}
/**
* Sets a signed long.
* @param byteOffset The byte offset.
* @param value The value.
* @param littleEndian If the value is little endian.
*/
setBigInt64(byteOffset: number, value: bigint, littleEndian?: boolean): void {
super.setBigInt64(byteOffset, value, littleEndian)
}
/**
* Writes the next signed long.
* @param value The value.
* @param littleEndian If the value is little endian.
*/
writeBigInt64(value: bigint, littleEndian?: boolean): void {
this.setBigInt64(this.#byteOffset, value, littleEndian)
this.#byteOffset += 8
}
/**
* Gets an unsigned long.
* @param byteOffset The byte offset.
* @param littleEndian If the value is little endian.
* @returns The value.
*/
getBigUint64(byteOffset: number, littleEndian?: boolean): bigint {
return super.getBigUint64(byteOffset, littleEndian)
}
/**
* Reads the next unsigned long.
* @param littleEndian If the value is little endian.
* @returns The value.
*/
readBigUint64(littleEndian?: boolean): bigint {
const value = this.getBigUint64(this.#byteOffset, littleEndian)
this.#byteOffset += 8
return value
}
/**
* Sets an unsigned long.
* @param byteOffset The byte offset.
* @param value The value.
* @param littleEndian If the value is little endian.
*/
setBigUint64(byteOffset: number, value: bigint, littleEndian?: boolean): void {
super.setBigUint64(byteOffset, value, littleEndian)
}
/**
* Writes the next unsigned long.
* @param value The value.
* @param littleEndian If the value is little endian.
*/
writeBigUint64(value: bigint, littleEndian?: boolean): void {
this.setBigUint64(this.#byteOffset, value, littleEndian)
this.#byteOffset += 8
}
/**
* Gets an signed long.
* @param byteOffset The byte offset.
* @param littleEndian If the value is little endian.
* @returns The value.
*/
getInt64(byteOffset: number, littleEndian?: boolean): number {
return Number(this.getBigInt64(byteOffset, littleEndian))
}
/**
* Reads the next signed long.
* @param littleEndian If the value is little endian.
* @returns The value.
*/
readInt64(littleEndian?: boolean): number {
const value = this.getInt64(this.#byteOffset, littleEndian)
this.#byteOffset += 8
return value
}
/**
* Sets a signed long.
* @param byteOffset The byte offset.
* @param value The value.
* @param littleEndian If the value is little endian.
*/
setInt64(byteOffset: number, value: number, littleEndian?: boolean): void {
this.setBigInt64(byteOffset, BigInt(value), littleEndian)
}
/**
* Writes the next signed long.
* @param value The value.
* @param littleEndian If the value is little endian.
*/
writeInt64(value: number, littleEndian?: boolean): void {
this.setInt64(this.#byteOffset, value, littleEndian)
this.#byteOffset += 8
}
/**
* Gets an unsigned long.
* @param byteOffset The byte offset.
* @param littleEndian If the value is little endian.
* @returns The value.
*/
getUint64(byteOffset: number, littleEndian?: boolean): number {
return Number(this.getBigUint64(byteOffset, littleEndian))
}
/**
* Reads the next unsigned long.
* @param littleEndian If the value is little endian.
* @returns The value.
*/
readUint64(littleEndian?: boolean): number {
const value = this.getUint64(this.#byteOffset, littleEndian)
this.#byteOffset += 8
return value
}
/**
* Sets an unsigned long.
* @param byteOffset The byte offset.
* @param value The value.
* @param littleEndian If the value is little endian.
*/
setUint64(byteOffset: number, value: number, littleEndian?: boolean): void {
this.setBigUint64(byteOffset, BigInt(value), littleEndian)
}
/**
* Writes the next signed long.
* @param value The value.
* @param littleEndian If the value is little endian.
*/
writeUint64(value: number, littleEndian?: boolean): void {
this.setUint64(this.#byteOffset, value, littleEndian)
this.#byteOffset += 8
}
/**
* Gets a signed, variable-length integer.
* @param byteOffset The byte offset.
* @param maxByteLength The maximum byte length.
* @returns The result.
* @link https://developers.google.com/protocol-buffers/docs/encoding#varints
*/
getVarInt(byteOffset: number, maxByteLength: number = 10): IntResult {
let value = 0
let byteLength = 0
for (let bitShift = 0; byteLength < maxByteLength; bitShift += 7) {
const byte = this.getUint8(byteOffset + byteLength++)
value |= (byte & 0b0111_1111) << bitShift
if ((byte & 0b1000_0000) === 0) {
return { value, byteLength }
}
}
throw new RangeError("VarInt must be between 1 and " + maxByteLength + " bytes.")
}
/**
* Reads the next signed, variable-length integer.
* @param maxByteLength The maximum byte length.
* @returns The value.
* @link https://developers.google.com/protocol-buffers/docs/encoding#varints
*/
readVarInt(maxByteLength?: number): number {
const { value, byteLength } = this.getVarInt(this.#byteOffset, maxByteLength)
this.#byteOffset += byteLength
return value
}
/**
* Sets a signed, variable-length integer.
* @param byteOffset The byte offset.
* @param value The value.
* @param maxByteLength The maximum byte length.
* @returns The byte length.
* @link https://developers.google.com/protocol-buffers/docs/encoding#varints
*/
setVarInt(byteOffset: number, value: number, maxByteLength: number = 10): number {
for (let byteLength = 1; byteLength <= maxByteLength; byteLength++) {
let byte = value & 0b0111_1111
value >>>= 7
if (value !== 0) {
byte |= 0b1000_0000
}
this.setUint8(byteOffset++, byte)
if (value === 0) {
return byteLength
}
}
throw new RangeError("VarInt must be between 1 and " + maxByteLength + " bytes.")
}
/**
* Writes the next signed, variable-length integer.
* @param value The value.
* @param maxByteLength The maximum byte length.
* @link https://developers.google.com/protocol-buffers/docs/encoding#varints
*/
writeVarInt(value: number, maxByteLength?: number): void {
const byteLength = this.setVarInt(this.#byteOffset, value, maxByteLength)
this.#byteOffset += byteLength
}
/**
* Gets an unsigned, variable-length integer.
* @param byteOffset The byte offset.
* @param maxByteLength The maximum byte length.
* @returns The result.
* @link https://developers.google.com/protocol-buffers/docs/encoding#varints
*/
getVarUint(byteOffset: number, maxByteLength?: number): IntResult {
const { value, byteLength } = this.getVarInt(byteOffset, maxByteLength)
return { value: value >>> 0, byteLength }
}
/**
* Reads the next unsigned, variable-length integer.
* @param maxByteLength The maximum byte length.
* @returns The value.
* @link https://developers.google.com/protocol-buffers/docs/encoding#varints
*/
readVarUint(maxByteLength?: number): number {
const { value, byteLength } = this.getVarUint(this.#byteOffset, maxByteLength)
this.#byteOffset += byteLength
return value
}
/**
* Sets an unsigned, variable-length integer.
* @param byteOffset The byte offset.
* @param value The value.
* @param maxByteLength The maximum byte length.
* @returns The byte length.
* @link https://developers.google.com/protocol-buffers/docs/encoding#varints
*/
setVarUint(byteOffset: number, value: number, maxByteLength?: number): number {
return this.setVarInt(byteOffset, value >>> 0, maxByteLength)
}
/**
* Writes the next unsigned, variable-length integer.
* @param value The value.
* @param maxByteLength The maximum byte length.
* @link https://developers.google.com/protocol-buffers/docs/encoding#varints
*/
writeVarUint(value: number, maxByteLength?: number): void {
const byteLength = this.setVarUint(this.#byteOffset, value, maxByteLength)
this.#byteOffset += byteLength
}
/**
* Gets a signed, variable-length integer with zigzag encoding.
* @param byteOffset The byte offset.
* @param maxByteLength The maximum byte length.
* @returns The result.
* @link https://developers.google.com/protocol-buffers/docs/encoding#signed_integers
*/
getVarZint(byteOffset: number, maxByteLength?: number): IntResult {
const { value, byteLength } = this.getVarInt(byteOffset, maxByteLength)
return { value: (value >> 1) ^ -(value & 1), byteLength }
}
/**
* Reads the next signed, variable-length integer with zigzag encoding.
* @param maxByteLength The maximum byte length.
* @returns The value.
* @link https://developers.google.com/protocol-buffers/docs/encoding#varints
*/
readVarZint(maxByteLength?: number): number {
const { value, byteLength } = this.getVarZint(this.#byteOffset, maxByteLength)
this.#byteOffset += byteLength
return value
}
/**
* Sets a signed, variable-length integer with zigzag encoding.
* @param byteOffset The byte offset.
* @param value The value.
* @param maxByteLength The maximum byte length.
* @returns The byte length.
* @link https://developers.google.com/protocol-buffers/docs/encoding#signed_integers
*/
setVarZint(byteOffset: number, value: number, maxByteLength?: number): number {
return this.setVarInt(byteOffset, (value >> 31) ^ (value << 1), maxByteLength)
}
/**
* Writes the next unsigned, variable-length integer with zigzag encoding.
* @param value The value.
* @param maxByteLength The maximum byte length.
* @link https://developers.google.com/protocol-buffers/docs/encoding#varints
*/
writeVarZint(value: number, maxByteLength?: number): void {
const byteLength = this.setVarZint(this.#byteOffset, value, maxByteLength)
this.#byteOffset += byteLength
}
/**
* Gets an array of unsigned bytes.
* @param byteOffset The byte offset.
* @param byteLength The byte length.
* @returns The value.
*/
getUint8Array(byteOffset: number, byteLength?: number): Uint8Array {
return new Uint8Array(this.buffer, super.byteOffset + byteOffset, byteLength)
}
/**
* Reads the next array of unsigned bytes.
* @param byteLength The byte length.
* @returns The value.
*/
readUint8Array(byteLength?: number): Uint8Array {
const value = this.getUint8Array(this.#byteOffset, byteLength)
this.#byteOffset += value.byteLength
return value
}
/**
* Sets an array of unsigned bytes.
* @param byteOffset The byte offset.
* @param value The value.
*/
setUint8Array(byteOffset: number, value: Uint8Array): void {
const byteLength = value.byteLength
this.getUint8Array(byteOffset, byteLength).set(value)
}
/**
* Writes the next array of unsigned bytes.
* @param value The value.
*/
writeUint8Array(value: Uint8Array): void {
this.setUint8Array(this.#byteOffset, value)
this.#byteOffset += value.byteLength
}
/**
* Gets an array of unsigned shorts.
* @param byteOffset The byte offset.
* @param byteLength The byte length.
* @returns The value.
*/
getUint16Array(byteOffset: number, byteLength?: number): Uint16Array {
if (byteLength !== undefined) {
byteLength = Math.floor(byteLength / 2)
}
return new Uint16Array(this.buffer, super.byteOffset + byteOffset, byteLength)
}
/**
* Reads the next array of unsigned shorts.
* @param byteLength The byte length.
* @returns The value.
*/
readUint16Array(byteLength?: number): Uint16Array {
const value = this.getUint16Array(this.#byteOffset, byteLength)
this.#byteOffset += value.byteLength
return value
}
/**
* Sets an array of unsigned bytes.
* @param byteOffset The byte offset.
* @param value The value.
*/
setUint16Array(byteOffset: number, value: Uint16Array): void {
const byteLength = value.byteLength
this.getUint16Array(byteOffset, byteLength).set(value)
}
/**
* Writes the next array of unsigned bytes.
* @param value The value.
*/
writeUint16Array(value: Uint16Array): void {
this.setUint16Array(this.#byteOffset, value)
this.#byteOffset += value.byteLength
}
/**
* Gets a string.
* @param byteOffset The byte offset.
* @param byteLength The byte length.
* @param byteEncoding The byte encoding.
* @returns The value.
*/
getString(byteOffset: number, byteLength?: number, byteEncoding?: BufferEncoding): string {
const decoder = new TextDecoder(byteEncoding || "utf-8")
const encoded = this.getUint8Array(byteOffset, byteLength)
return decoder.decode(encoded)
}
/**
* Reads the next string.
* @param byteLength The byte length.
* @param byteEncoding The byte encoding.
* @returns The value.
*/
readString(byteLength?: number, byteEncoding?: BufferEncoding): string {
const value = this.getString(this.#byteOffset, byteLength, byteEncoding)
if (byteLength === undefined) {
this.#byteOffset = this.byteLength
} else {
this.#byteOffset += byteLength
}
return value
}
/**
* Sets a string.
* @param byteOffset The byte offset.
* @param value The string.
* @param byteEncoding The byte encoding.
* @returns The byte length.
*/
setString(byteOffset: number, value: string, byteEncoding?: BufferEncoding): number {
// TODO: add support for "utf-16-be" and "utf-16-le"
if (byteEncoding && byteEncoding !== "utf-8") {
throw new TypeError("String encoding '" + byteEncoding + "' is not supported")
}
const encoder = new TextEncoder()
const byteLength = Math.min(this.byteLength - byteOffset, value.length * 4)
const destination = this.getUint8Array(byteOffset, byteLength)
const { written } = encoder.encodeInto(value, destination)
return written || 0
}
/**
* Writes the next a string.
* @param value The string.
* @param byteEncoding The byte encoding.
*/
writeString(value: string, byteEncoding?: BufferEncoding): void {
const byteLength = this.setString(this.#byteOffset, value, byteEncoding)
this.#byteOffset += byteLength
}
/**
* Gets a String with a variable-length delimeter.
* @param byteOffset The byte offset.
* @param maxByteLength The maximum byte length.
* @returns The result.
* @link https://developers.google.com/protocol-buffers/docs/encoding#strings
*/
getVarString(byteOffset: number, maxByteLength?: number): StringResult {
const {
value: byteLength,
byteLength: delimeterOffset } = this.getVarUint(byteOffset)
if (maxByteLength !== undefined && byteLength > maxByteLength) {
throw new RangeError("VarString must be less than or equal to " + maxByteLength + " bytes.")
}
const value = this.getString(byteOffset + delimeterOffset, byteLength)
return { value, byteLength: byteLength + delimeterOffset }
}
/**
* Reads the next String with a variable-length delimeter.
* @param maxByteLength The maximum byte length.
* @returns The value.
* @link https://developers.google.com/protocol-buffers/docs/encoding#strings
*/
readVarString(maxByteLength?: number): string {
const { value, byteLength } = this.getVarString(this.#byteOffset, maxByteLength)
this.#byteOffset += byteLength
return value
}
/**
* Sets a String with a variable-length delimeter.
* @param byteOffset The byte offset.
* @param value The value.
* @returns The byte length.
* @link https://developers.google.com/protocol-buffers/docs/encoding#strings
*/
setVarString(byteOffset: number, value: string): number {
const encoder = new TextEncoder()
const encoded = encoder.encode(value)
const byteLength = encoded.byteLength
const delimeterOffset = this.setVarInt(byteOffset, byteLength)
this.setUint8Array(byteOffset + delimeterOffset, encoded)
return delimeterOffset + byteLength
}
/**
* Writes the next String with a variable-length delimeter.
* @param value The value.
* @link https://developers.google.com/protocol-buffers/docs/encoding#strings
*/
writeVarString(value: string): number {
const byteLength = this.setVarString(this.#byteOffset, value)
this.#byteOffset += byteLength
return byteLength
}
/**
* Formats to a string.
* @param format The string format.
* @returns The string.
*/
toString(format?: string): string {
return Array.prototype.map.call(this.getUint8Array(0), function(byte: number): string {
switch(format) {
case "hex":
return ("00" + byte.toString(16)).slice(-2)
default:
return byte.toString(10)
}
}).join(" ")
}
}
/**
* A number result.
*/
interface IntResult {
/**
* The byte length.
*/
readonly byteLength: number
/**
* The value.
*/
readonly value: number
}
/**
* A string result.
*/
interface StringResult {
/**
* The byte length.
*/
readonly byteLength: number
/**
* The value.
*/
readonly value: string
}