-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaryReader.ts
53 lines (49 loc) · 1.19 KB
/
binaryReader.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
export class BinaryReader {
binaryBuffer: Buffer;
pointer: number;
constructor(binaryBuffer: Buffer) {
this.binaryBuffer = binaryBuffer;
this.pointer = 0;
}
readByte(): number
{
return this.binaryBuffer.readUInt8(this.pointer++);
}
readWord(): number // u32
{
let v = this.binaryBuffer.readUInt32LE(this.pointer);
this.pointer += 4;
return v;
}
readFloat(): number // f32
{
let v = this.binaryBuffer.readFloatLE(this.pointer);
this.pointer += 4;
return v;
}
readDouble(): number // f64
{
let v = this.binaryBuffer.readDoubleLE(this.pointer);
this.pointer += 8;
return v;
}
readVarInt(): number
{
let v = 0;
let shift = 0;
let b: number;
do {
b = this.readByte();
v |= (b & 0x7F) << shift;
shift += 7;
} while (b >= 0x80);
return v;
}
readString(): string
{
let length = this.readVarInt();
let str = this.binaryBuffer.toString("utf-8", this.pointer, this.pointer + length);
this.pointer += length;
return str;
}
}