-
Notifications
You must be signed in to change notification settings - Fork 1
/
Unpacker.ts
43 lines (37 loc) · 1019 Bytes
/
Unpacker.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
import Uint1Array from "uint1array";
export class Unpacker {
buf: ArrayBuffer;
bytes: Uint8Array;
bits: Uint1Array;
position: number = 0;
constructor(public compressed: Uint8Array) {
this.buf = new ArrayBuffer(compressed.length);
this.bytes = new Uint8Array(this.buf);
this.bits = new Uint1Array(this.buf);
this.bytes.set(compressed);
}
readInt32() {
const bits = this.readBits(32);
const binary = bits.reverse().join("");
const length = parseInt(binary, 2);
return length;
}
readInt8() {
const bits = this.readBits(8);
const binary = bits.reverse().join("");
const length = parseInt(binary, 2);
return length;
}
readBits(n: number) {
const peekBits = this.bits.subarray(this.position, this.position + n);
this.dropBits(n);
return Array.from(peekBits);
}
dropBits(n: number) {
this.position += n;
}
peek(n: number) {
const peekBits = this.bits.subarray(this.position, this.position + n);
return peekBits;
}
}