1
+ import DataReader from "./data_reader.ts" ;
2
+
1
3
/**
2
4
* This class represents a binary reader. It is similar to DataView but has an internal pointer to
3
5
* automatically pop the bytes that have been already read.
4
6
*/
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
+
9
10
/**
10
11
* @param buffer The buffer to read from
11
12
*/
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
45
16
}
46
17
47
18
/**
@@ -50,96 +21,96 @@ export default class BinaryReader {
50
21
* @returns The `Int8` read.
51
22
*/
52
23
public readInt8 ( ) : number {
53
- return this . read ( ( ) => this . bufferView . getInt8 ( this . pointer ) , 1 ) ;
24
+ return new this . bufferView ( this . read ( 1 ) ) . getInt8 ( 0 )
54
25
}
55
26
56
27
/**
57
28
* This instance function reads a `Uint8`.
58
29
*
59
30
* @returns The `Uint8` read.
60
31
*/
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 )
63
34
}
64
35
65
36
/**
66
37
* This instance function reads a `Int16` (2 bytes).
67
38
*
68
39
* @returns The `Int16` read.
69
40
*/
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 )
72
43
}
73
44
74
45
/**
75
46
* This instance function reads a `Uint16` (2 bytes).
76
47
*
77
48
* @returns The `Uint16` read.
78
49
*/
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 ) ;
81
52
}
82
53
83
54
/**
84
55
* This instance function reads a `Int32` (4 bytes).
85
56
*
86
57
* @returns The `Int32` read.
87
58
*/
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 ) ;
90
61
}
91
62
92
63
/**
93
64
* This instance function reads a `Uint32` (4 bytes).
94
65
*
95
66
* @returns The `Uint32` read.
96
67
*/
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 ) ;
99
70
}
100
71
101
72
/**
102
73
* This instance function reads a `Int64` (8 bytes).
103
74
*
104
75
* @returns The `Int64` read.
105
76
*/
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 ) ;
108
79
}
109
80
110
81
/**
111
82
* This instance function reads a `Uint64` (8 bytes).
112
83
*
113
84
* @returns The `Uint64` read.
114
85
*/
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 ) ;
117
88
}
118
89
119
90
/**
120
91
* This instance function reads a `Float16` (2 bytes).
121
92
*
122
93
* @returns The `Float16` read.
123
94
*/
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 ) ;
126
97
}
127
98
128
99
/**
129
100
* This instance function reads a `Float32` (4 bytes).
130
101
*
131
102
* @returns The `Float32` read.
132
103
*/
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 ) ;
135
106
}
136
107
137
108
/**
138
109
* This instance function reads a `Float64` (8 bytes).
139
110
*
140
111
* @returns The `Float54` read.
141
112
*/
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 ) ;
144
115
}
145
116
}
0 commit comments