Skip to content

Commit 65a0717

Browse files
committed
Refactoring
1 parent a1cae00 commit 65a0717

File tree

3 files changed

+63
-58
lines changed

3 files changed

+63
-58
lines changed

src/binary_reader.ts

Lines changed: 29 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,18 @@
1+
import DataReader from "./data_reader.ts";
2+
13
/**
24
* This class represents a binary reader. It is similar to DataView but has an internal pointer to
35
* automatically pop the bytes that have been already read.
46
*/
5-
export default class BinaryReader {
6-
private bufferView : DataView;
7-
private pointer : number = 0;
8-
7+
export default class BinaryReader extends DataReader {
8+
private readonly bufferView : DataViewConstructor;
9+
910
/**
1011
* @param buffer The buffer to read from
1112
*/
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);
13+
constructor(buffer : ArrayBuffer) {
14+
super(buffer);
15+
this.bufferView = DataView
4516
}
4617

4718
/**
@@ -50,96 +21,96 @@ export default class BinaryReader {
5021
* @returns The `Int8` read.
5122
*/
5223
public readInt8(): number {
53-
return this.read(() => this.bufferView.getInt8(this.pointer), 1);
24+
return new this.bufferView(this.read(1)).getInt8(0)
5425
}
5526

5627
/**
5728
* This instance function reads a `Uint8`.
5829
*
5930
* @returns The `Uint8` read.
6031
*/
61-
public readUint8(): number {
62-
return this.read(() => this.bufferView.getUint8(this.pointer), 1);
32+
public readUint8() : number {
33+
return new this.bufferView(this.read(1)).getUint8(0)
6334
}
6435

6536
/**
6637
* This instance function reads a `Int16` (2 bytes).
6738
*
6839
* @returns The `Int16` read.
6940
*/
70-
public readInt16(littleEndian: boolean = true): number {
71-
return this.read(() => this.bufferView.getInt16(this.pointer, littleEndian), 2);
41+
public readInt16(littleEndian? : boolean) : number {
42+
return new this.bufferView(this.read(2)).getInt16(0, littleEndian)
7243
}
7344

7445
/**
7546
* This instance function reads a `Uint16` (2 bytes).
7647
*
7748
* @returns The `Uint16` read.
7849
*/
79-
public readUint16(littleEndian: boolean = true): number {
80-
return this.read(() => this.bufferView.getUint16(this.pointer, littleEndian), 2);
50+
public readUint16(littleEndian? : boolean) : number {
51+
return new this.bufferView(this.read(2)).getUint16(0, littleEndian);
8152
}
8253

8354
/**
8455
* This instance function reads a `Int32` (4 bytes).
8556
*
8657
* @returns The `Int32` read.
8758
*/
88-
public readInt32(littleEndian: boolean = true): number {
89-
return this.read(() => this.bufferView.getInt32(this.pointer, littleEndian), 4);
59+
public readInt32(littleEndian? : boolean): number {
60+
return new this.bufferView(this.read(4)).getInt32(0, littleEndian);
9061
}
9162

9263
/**
9364
* This instance function reads a `Uint32` (4 bytes).
9465
*
9566
* @returns The `Uint32` read.
9667
*/
97-
public readUint32(littleEndian: boolean = true): number {
98-
return this.read(() => this.bufferView.getUint32(this.pointer, littleEndian), 4);
68+
public readUint32(littleEndian? : boolean): number {
69+
return new this.bufferView(this.read(4)).getUint32(0, littleEndian);
9970
}
10071

10172
/**
10273
* This instance function reads a `Int64` (8 bytes).
10374
*
10475
* @returns The `Int64` read.
10576
*/
106-
public readInt64(littleEndian: boolean = true): bigint {
107-
return this.read(() => this.bufferView.getBigInt64(this.pointer, littleEndian), 8);
77+
public readInt64(littleEndian? : boolean) : bigint {
78+
return new this.bufferView(this.read(8)).getBigInt64(0, littleEndian);
10879
}
10980

11081
/**
11182
* This instance function reads a `Uint64` (8 bytes).
11283
*
11384
* @returns The `Uint64` read.
11485
*/
115-
public readUint64(littleEndian: boolean = true): bigint {
116-
return this.read(() => this.bufferView.getBigUint64(this.pointer, littleEndian), 8);
86+
public readUint64(littleEndian? : boolean) : bigint {
87+
return new this.bufferView(this.read(8)).getBigUint64(0, littleEndian);
11788
}
11889

11990
/**
12091
* This instance function reads a `Float16` (2 bytes).
12192
*
12293
* @returns The `Float16` read.
12394
*/
124-
public readFloat16(littleEndian: boolean = true): number {
125-
return this.read(() => this.bufferView.getFloat16(this.pointer, littleEndian), 2);
95+
public readFloat16(littleEndian? : boolean) : number {
96+
return new this.bufferView(this.read(2)).getFloat16(0, littleEndian);
12697
}
12798

12899
/**
129100
* This instance function reads a `Float32` (4 bytes).
130101
*
131102
* @returns The `Float32` read.
132103
*/
133-
public readFloat32(littleEndian: boolean = true): number {
134-
return this.read(() => this.bufferView.getFloat32(this.pointer, littleEndian), 4);
104+
public readFloat32(littleEndian? : boolean) : number {
105+
return new this.bufferView(this.read(4)).getFloat32(0, littleEndian);
135106
}
136107

137108
/**
138109
* This instance function reads a `Float64` (8 bytes).
139110
*
140111
* @returns The `Float54` read.
141112
*/
142-
public readFloat64(littleEndian: boolean = true): number {
143-
return this.read(() => this.bufferView.getFloat64(this.pointer, littleEndian), 8);
113+
public readFloat64(littleEndian? : boolean): number {
114+
return new this.bufferView(this.read(8)).getFloat64(0, littleEndian);
144115
}
145116
}

src/data_reader.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export default class DataReader {
2+
public readonly buffer : ArrayBuffer;
3+
public pointer : number = 0;
4+
5+
/**
6+
* @param buffer The buffer to read from
7+
*/
8+
constructor(buffer : ArrayBuffer) {
9+
this.buffer = buffer;
10+
}
11+
12+
/**
13+
* This getter returns the buffer that is left to read.
14+
*
15+
* @returns The buffer that is left.
16+
*/
17+
public get bufferLeft() : ArrayBuffer {
18+
return this.buffer.slice(this.pointer);
19+
}
20+
21+
/**
22+
* This instance function reads `n` bytes and pushes the pointer accordingly.
23+
*
24+
* @param bytes How many bytes to read
25+
* @returns The value read by `readFunction`.
26+
*/
27+
public read(bytes : number = 1) : ArrayBuffer {
28+
const value = this.buffer.slice(this.pointer, this.pointer + bytes)
29+
this.pointer += bytes;
30+
return value;
31+
}
32+
}

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import DataReader from "./data_reader.ts";
12
import BinaryReader from "./binary_reader.ts";
23

34
export default {
5+
DataReader,
46
BinaryReader
57
}

0 commit comments

Comments
 (0)