forked from y-scope/yscope-log-viewer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataInputStream.js
180 lines (166 loc) · 5.51 KB
/
DataInputStream.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* EOF error thrown by DataInputStream
*/
class DataInputStreamEOFError extends Error {
/**
* Constructor
* @param {number} bufLen The length of the buffer when the error occurred
* @param {number} requiredLen The required length of the buffer when the
* error occurred
* @param {string} message
*/
constructor (bufLen, requiredLen, message= "") {
let formattedMessage = `[bufLen=${bufLen}, requiredLen=${requiredLen}]`;
if ("" !== message) {
formattedMessage += ` ${message}`;
}
super(formattedMessage);
this.name = "DataInputStreamEOFError";
this.bufLen = bufLen;
this.requiredLen = requiredLen;
}
}
/**
* Class to decode primitive types from a byte stream (similar to Java's
* DataInputStream class)
*/
class DataInputStream {
/**
* Constructor
* @param {ArrayBufferLike} arrayBuffer The array buffer that's backing this
* @param {boolean} [isLittleEndian] Byte endianness
* class
*/
constructor (arrayBuffer, isLittleEndian= false) {
this._dataView = new DataView(arrayBuffer);
this._isLittleEndian = isLittleEndian;
this._byteIx = 0;
}
/**
* Seeks to the given index
* @param {number} idx
*/
seek (idx) {
if (idx > this._dataView.byteLength) {
this._byteIx = this._dataView.byteLength;
throw new DataInputStreamEOFError(this._dataView.byteLength, idx);
}
this._byteIx = idx;
}
/**
* @return {number} The position of the read head in the stream
*/
getPos () {
return this._byteIx;
}
/**
* Reads the specified amount of data
* @param {number} length
* @return {Uint8Array} The data read
*/
readFully (length) {
const requiredLen = this._byteIx + length;
if (this._dataView.byteLength < requiredLen) {
this._byteIx = this._dataView.byteLength;
throw new DataInputStreamEOFError(this._dataView.byteLength, requiredLen);
}
const val = new Uint8Array(this._dataView.buffer, this._byteIx, length);
this._byteIx += length;
return val;
}
/**
* Reads an unsigned byte
* @return {number} The read byte
*/
readUnsignedByte () {
const requiredLen = this._byteIx + 1;
if (this._dataView.byteLength < requiredLen) {
this._byteIx = this._dataView.byteLength;
throw new DataInputStreamEOFError(this._dataView.byteLength, requiredLen);
}
return this._dataView.getUint8(this._byteIx++);
}
/**
* Reads a signed byte
* @return {number} The read byte
*/
readSignedByte () {
const requiredLen = this._byteIx + 1;
if (this._dataView.byteLength < requiredLen) {
this._byteIx = this._dataView.byteLength;
throw new DataInputStreamEOFError(this._dataView.byteLength, requiredLen);
}
return this._dataView.getInt8(this._byteIx++);
}
/**
* Reads an unsigned short
* @return {number} The read short
*/
readUnsignedShort () {
const requiredLen = this._byteIx + 2;
if (this._dataView.byteLength < requiredLen) {
this._byteIx = this._dataView.byteLength;
throw new DataInputStreamEOFError(this._dataView.byteLength, requiredLen);
}
const val = this._dataView.getUint16(this._byteIx, this._isLittleEndian);
this._byteIx += 2;
return val;
}
/**
* Reads a signed short
* @return {number} The read short
*/
readSignedShort () {
const requiredLen = this._byteIx + 2;
if (this._dataView.byteLength < requiredLen) {
this._byteIx = this._dataView.byteLength;
throw new DataInputStreamEOFError(this._dataView.byteLength, requiredLen);
}
const val = this._dataView.getInt16(this._byteIx, this._isLittleEndian);
this._byteIx += 2;
return val;
}
/**
* Reads an int
* @return {number} The read int
*/
readInt () {
const requiredLen = this._byteIx + 4;
if (this._dataView.byteLength < requiredLen) {
this._byteIx = this._dataView.byteLength;
throw new DataInputStreamEOFError(this._dataView.byteLength, requiredLen);
}
const val = this._dataView.getInt32(this._byteIx, this._isLittleEndian);
this._byteIx += 4;
return val;
}
/**
* Reads a signed long int (64 bit)
* @return {BigInt} The read signed long int
*/
readSignedLong(){
const requiredLen = this._byteIx + 8;
if (this._dataView.byteLength < requiredLen) {
this._byteIx = this._dataView.byteLength;
throw new DataInputStreamEOFError(this._dataView.byteLength, requiredLen);
}
const val = this._dataView.getBigInt64(this._byteIx, this._isLittleEndian);
this._byteIx += 8;
return val;
}
/**
* Reads an unsigned long int (64 bit)
* @return {BigInt} The read unsigned long int
*/
readUnsignedLong(){
const requiredLen = this._byteIx + 8;
if (this._dataView.byteLength < requiredLen) {
this._byteIx = this._dataView.byteLength;
throw new DataInputStreamEOFError(this._dataView.byteLength, requiredLen);
}
const val = this._dataView.getBigUint64(this._byteIx, this._isLittleEndian);
this._byteIx += 8;
return val;
}
}
export {DataInputStream, DataInputStreamEOFError};