1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | const { pow, floor } = Math;
|
---|
4 | const TWO_POW_32 = pow(2, 32);
|
---|
5 | /**
|
---|
6 | * Mimic Java's ByteBufffer with big endian order
|
---|
7 | */
|
---|
8 | class ByteBuffer {
|
---|
9 | constructor(data) {
|
---|
10 | this.position = 0;
|
---|
11 | this.data = data;
|
---|
12 | this.int32ArrayForConvert = new Uint32Array(1);
|
---|
13 | this.int8ArrayForConvert = new Uint8Array(this.int32ArrayForConvert.buffer);
|
---|
14 | }
|
---|
15 | static allocate(size = 16) {
|
---|
16 | return new ByteBuffer(new Uint8Array(size));
|
---|
17 | }
|
---|
18 | put(value) {
|
---|
19 | if (this.position === this.data.length) {
|
---|
20 | const oldArray = this.data;
|
---|
21 | this.data = new Uint8Array(this.data.length * 2);
|
---|
22 | this.data.set(oldArray);
|
---|
23 | }
|
---|
24 | this.data[this.position] = value;
|
---|
25 | this.position++;
|
---|
26 | }
|
---|
27 | putInt32(value) {
|
---|
28 | if (this.data.length - this.position < 4) {
|
---|
29 | const oldArray = this.data;
|
---|
30 | this.data = new Uint8Array(this.data.length * 2 + 4);
|
---|
31 | this.data.set(oldArray);
|
---|
32 | }
|
---|
33 | this.int32ArrayForConvert[0] = value;
|
---|
34 | this.data.set(this.int8ArrayForConvert.reverse(), this.position);
|
---|
35 | this.position += 4;
|
---|
36 | }
|
---|
37 | putInt64(value) {
|
---|
38 | this.putInt32(floor(value / TWO_POW_32));
|
---|
39 | this.putInt32(value);
|
---|
40 | }
|
---|
41 | putArray(array) {
|
---|
42 | if (this.data.length - this.position < array.byteLength) {
|
---|
43 | const oldArray = this.data;
|
---|
44 | this.data = new Uint8Array(this.position + array.byteLength);
|
---|
45 | this.data.set(oldArray);
|
---|
46 | }
|
---|
47 | this.data.set(array, this.position);
|
---|
48 | this.position += array.byteLength;
|
---|
49 | }
|
---|
50 | get() {
|
---|
51 | const value = this.data[this.position];
|
---|
52 | this.position++;
|
---|
53 | return value;
|
---|
54 | }
|
---|
55 | getInt32() {
|
---|
56 | this.int8ArrayForConvert.set(this.data.slice(this.position, this.position + 4).reverse());
|
---|
57 | const value = this.int32ArrayForConvert[0];
|
---|
58 | this.position += 4;
|
---|
59 | return value;
|
---|
60 | }
|
---|
61 | getInt64() {
|
---|
62 | const high = this.getInt32();
|
---|
63 | const low = this.getInt32();
|
---|
64 | return high * TWO_POW_32 + low;
|
---|
65 | }
|
---|
66 | resetPosition() {
|
---|
67 | this.position = 0;
|
---|
68 | }
|
---|
69 | }
|
---|
70 | exports.default = ByteBuffer;
|
---|
71 | //# sourceMappingURL=ByteBuffer.js.map |
---|