1 | /*
|
---|
2 | * This is a AssemblyScript port of the original Java version, which was written by
|
---|
3 | * Gil Tene as described in
|
---|
4 | * https://github.com/HdrHistogram/HdrHistogram
|
---|
5 | * and released to the public domain, as explained at
|
---|
6 | * http://creativecommons.org/publicdomain/zero/1.0/
|
---|
7 | */
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * Mimic Java's ByteBufffer with big endian order
|
---|
11 | */
|
---|
12 | class ByteBuffer {
|
---|
13 | position: i32;
|
---|
14 |
|
---|
15 | data: Uint8Array;
|
---|
16 |
|
---|
17 | int64ArrayForConvert: Uint64Array;
|
---|
18 | int32ArrayForConvert: Uint32Array;
|
---|
19 | int8ArrayForConvertInt32: Uint8Array;
|
---|
20 | int8ArrayForConvertInt64: Uint8Array;
|
---|
21 |
|
---|
22 | static allocate(size: i32 = 16): ByteBuffer {
|
---|
23 | return new ByteBuffer(new Uint8Array(size));
|
---|
24 | }
|
---|
25 |
|
---|
26 | constructor(data: Uint8Array) {
|
---|
27 | this.position = 0;
|
---|
28 | this.data = data;
|
---|
29 | this.int64ArrayForConvert = new Uint64Array(1);
|
---|
30 | this.int32ArrayForConvert = new Uint32Array(1);
|
---|
31 | this.int8ArrayForConvertInt32 = Uint8Array.wrap(
|
---|
32 | this.int32ArrayForConvert.buffer
|
---|
33 | );
|
---|
34 | this.int8ArrayForConvertInt64 = Uint8Array.wrap(
|
---|
35 | this.int64ArrayForConvert.buffer
|
---|
36 | );
|
---|
37 | }
|
---|
38 |
|
---|
39 | put(value: u8): void {
|
---|
40 | if (this.position === this.data.length) {
|
---|
41 | const oldArray = this.data;
|
---|
42 | this.data = new Uint8Array(this.data.length << 1);
|
---|
43 | this.data.set(oldArray);
|
---|
44 | }
|
---|
45 | unchecked((this.data[this.position] = value));
|
---|
46 | this.position++;
|
---|
47 | }
|
---|
48 |
|
---|
49 | putInt32(value: u32): void {
|
---|
50 | if (this.data.length - this.position < 4) {
|
---|
51 | const oldArray = this.data;
|
---|
52 | this.data = new Uint8Array((this.data.length << 1) + 4);
|
---|
53 | this.data.set(oldArray);
|
---|
54 | }
|
---|
55 | unchecked((this.int32ArrayForConvert[0] = value));
|
---|
56 | this.data.set(this.int8ArrayForConvertInt32.reverse(), this.position);
|
---|
57 | this.position += 4;
|
---|
58 | }
|
---|
59 |
|
---|
60 | putInt64(value: u64): void {
|
---|
61 | if (this.data.length - this.position < 8) {
|
---|
62 | const oldArray = this.data;
|
---|
63 | this.data = new Uint8Array((this.data.length << 1) + 8);
|
---|
64 | this.data.set(oldArray);
|
---|
65 | }
|
---|
66 | unchecked((this.int64ArrayForConvert[0] = value));
|
---|
67 | this.data.set(this.int8ArrayForConvertInt64.reverse(), this.position);
|
---|
68 | this.position += 8;
|
---|
69 | }
|
---|
70 |
|
---|
71 | putArray(array: Uint8Array): void {
|
---|
72 | if (this.data.length - this.position < array.byteLength) {
|
---|
73 | const oldArray = this.data;
|
---|
74 | this.data = new Uint8Array(this.position + array.byteLength);
|
---|
75 | this.data.set(oldArray);
|
---|
76 | }
|
---|
77 | this.data.set(array, this.position);
|
---|
78 | this.position += array.byteLength;
|
---|
79 | }
|
---|
80 |
|
---|
81 | get(): u8 {
|
---|
82 | const value = unchecked(this.data[this.position]);
|
---|
83 | this.position++;
|
---|
84 | return value;
|
---|
85 | }
|
---|
86 |
|
---|
87 | getInt32(): u32 {
|
---|
88 | this.int8ArrayForConvertInt32.set(
|
---|
89 | this.data.slice(this.position, this.position + 4).reverse()
|
---|
90 | );
|
---|
91 | const value = unchecked(this.int32ArrayForConvert[0]);
|
---|
92 | this.position += 4;
|
---|
93 | return value;
|
---|
94 | }
|
---|
95 |
|
---|
96 | getInt64(): u64 {
|
---|
97 | this.int8ArrayForConvertInt64.set(
|
---|
98 | this.data.slice(this.position, this.position + 8).reverse()
|
---|
99 | );
|
---|
100 | const value = unchecked(this.int64ArrayForConvert[0]);
|
---|
101 | this.position += 8;
|
---|
102 | return value;
|
---|
103 | }
|
---|
104 |
|
---|
105 | resetPosition(): void {
|
---|
106 | this.position = 0;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | export default ByteBuffer;
|
---|