Skip to content

Commit a1cae00

Browse files
committed
Initial
1 parent 0bb5934 commit a1cae00

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

src/binary_reader.ts

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/**
2+
* This class represents a binary reader. It is similar to DataView but has an internal pointer to
3+
* automatically pop the bytes that have been already read.
4+
*/
5+
export default class BinaryReader {
6+
private bufferView : DataView;
7+
private pointer : number = 0;
8+
9+
/**
10+
* @param buffer The buffer to read from
11+
*/
12+
constructor(buffer : ArrayBufferLike) {
13+
this.bufferView = new DataView(buffer);
14+
}
15+
16+
/**
17+
* This instance function reads `n` bytes and pushes the pointer accordingly.
18+
*
19+
* @param readFunction The function that should be used for reading `n` bytes
20+
* @param bytes How many bytes to read
21+
* @returns The value read by `readFunction`.
22+
*/
23+
private read<ReadFunction>(readFunction: () => ReadFunction, bytes: number): ReadFunction {
24+
const value = readFunction();
25+
this.pointer += bytes;
26+
return value;
27+
}
28+
29+
/**
30+
* This getter returns the entire buffer.
31+
*
32+
* @returns The entire buffer.
33+
*/
34+
public get buffer() : ArrayBufferLike {
35+
return this.bufferView.buffer;
36+
}
37+
38+
/**
39+
* This getter returns the buffer that is left to read.
40+
*
41+
* @returns The buffer that is left.
42+
*/
43+
public get bufferLeft() : ArrayBufferLike {
44+
return this.bufferView.buffer.slice(this.pointer);
45+
}
46+
47+
/**
48+
* This instance function reads a `Int8`.
49+
*
50+
* @returns The `Int8` read.
51+
*/
52+
public readInt8(): number {
53+
return this.read(() => this.bufferView.getInt8(this.pointer), 1);
54+
}
55+
56+
/**
57+
* This instance function reads a `Uint8`.
58+
*
59+
* @returns The `Uint8` read.
60+
*/
61+
public readUint8(): number {
62+
return this.read(() => this.bufferView.getUint8(this.pointer), 1);
63+
}
64+
65+
/**
66+
* This instance function reads a `Int16` (2 bytes).
67+
*
68+
* @returns The `Int16` read.
69+
*/
70+
public readInt16(littleEndian: boolean = true): number {
71+
return this.read(() => this.bufferView.getInt16(this.pointer, littleEndian), 2);
72+
}
73+
74+
/**
75+
* This instance function reads a `Uint16` (2 bytes).
76+
*
77+
* @returns The `Uint16` read.
78+
*/
79+
public readUint16(littleEndian: boolean = true): number {
80+
return this.read(() => this.bufferView.getUint16(this.pointer, littleEndian), 2);
81+
}
82+
83+
/**
84+
* This instance function reads a `Int32` (4 bytes).
85+
*
86+
* @returns The `Int32` read.
87+
*/
88+
public readInt32(littleEndian: boolean = true): number {
89+
return this.read(() => this.bufferView.getInt32(this.pointer, littleEndian), 4);
90+
}
91+
92+
/**
93+
* This instance function reads a `Uint32` (4 bytes).
94+
*
95+
* @returns The `Uint32` read.
96+
*/
97+
public readUint32(littleEndian: boolean = true): number {
98+
return this.read(() => this.bufferView.getUint32(this.pointer, littleEndian), 4);
99+
}
100+
101+
/**
102+
* This instance function reads a `Int64` (8 bytes).
103+
*
104+
* @returns The `Int64` read.
105+
*/
106+
public readInt64(littleEndian: boolean = true): bigint {
107+
return this.read(() => this.bufferView.getBigInt64(this.pointer, littleEndian), 8);
108+
}
109+
110+
/**
111+
* This instance function reads a `Uint64` (8 bytes).
112+
*
113+
* @returns The `Uint64` read.
114+
*/
115+
public readUint64(littleEndian: boolean = true): bigint {
116+
return this.read(() => this.bufferView.getBigUint64(this.pointer, littleEndian), 8);
117+
}
118+
119+
/**
120+
* This instance function reads a `Float16` (2 bytes).
121+
*
122+
* @returns The `Float16` read.
123+
*/
124+
public readFloat16(littleEndian: boolean = true): number {
125+
return this.read(() => this.bufferView.getFloat16(this.pointer, littleEndian), 2);
126+
}
127+
128+
/**
129+
* This instance function reads a `Float32` (4 bytes).
130+
*
131+
* @returns The `Float32` read.
132+
*/
133+
public readFloat32(littleEndian: boolean = true): number {
134+
return this.read(() => this.bufferView.getFloat32(this.pointer, littleEndian), 4);
135+
}
136+
137+
/**
138+
* This instance function reads a `Float64` (8 bytes).
139+
*
140+
* @returns The `Float54` read.
141+
*/
142+
public readFloat64(littleEndian: boolean = true): number {
143+
return this.read(() => this.bufferView.getFloat64(this.pointer, littleEndian), 8);
144+
}
145+
}

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import BinaryReader from "./binary_reader.ts";
2+
3+
export default {
4+
BinaryReader
5+
}

0 commit comments

Comments
 (0)