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
type FrequentieTable = Record<string, number>;
type IntermediaryNode = [string, number, Node[]];
type LeafNode = [string, number];
type Node = IntermediaryNode | LeafNode;
type HuffmanTree = [IntermediaryNode];
type Encoding = number[];
type EncodedData = number[];
type EncodingTable = Record<string, Encoding>;
export class BinaryReader {
protected chunk: number;
protected pos: number;
protected bitPos: number;
protected bitCount: number;
view: DataView;
constructor(arrayBuffer: ArrayBuffer) {
this.view = new DataView(arrayBuffer);
this.pos = 0;
this.bitPos = -1;
this.chunk = 0;
this.bitCount = 0;
}
protected getData(type = "Uint8") {
if (this.view.byteLength > this.pos) {
this.bitPos = -1;
// @ts-ignore
return this.view[`get${type}`](this.pos++);
}
throw new RangeError();
}
get buffer() {
return this.view.buffer;
}
getBytePosition() {
return this.pos;
}
seek(pos: number) {
const oldPos = this.pos;
this.pos = pos;
return oldPos;
}
peak(index = this.pos + 1) {
if (this.view.byteLength > index && index > -1) {
return this.view.getUint8(index);
}
return null;
}
peakBit() {
const chunk = this.chunk;
const pos = this.pos;
const bitPos = this.bitPos;
const bitCount = this.bitCount;
const bit = this.getBit();
this.bitPos = bitPos;
this.chunk = chunk;
this.pos = pos;
this.bitCount = bitCount;
return bit;
}
getPadSize() {
if (this.chunk === null) {
return 0;
} else {
const bitCount = getBitCount(this.chunk);
return 8 - bitCount;
}
}
getBitPos() {
return getBitCount(this.chunk) - 1 + this.getPadSize();
}
getBit() {
if (this.bitPos === -1) {
this.chunk = this.getData();
this.bitPos = this.getBitPos();
this.bitCount = getBitCount(this.chunk);
}
if (this.chunk === null) return null;
const bitCount = this.bitCount;
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
May 16, 2024