-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbyte-array-source.js
More file actions
50 lines (41 loc) · 1.05 KB
/
Copy pathbyte-array-source.js
File metadata and controls
50 lines (41 loc) · 1.05 KB
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
export default class ByteArraySource {
constructor(byteArray) {
if (byteArray instanceof Uint8Array) {
this.rawData = byteArray;
}
this.fp = 0;
}
readRaw(bytes) {
let raw = this.rawData.slice(this.fp, this.fp + bytes);
this.fp += bytes;
return raw;
}
readInt() {
return this.readByte() |
this.readByte() << 8 |
this.readByte() << 16 |
this.readByte() << 24;
}
readShort() {
return (this.readByte()<<16 | this.readByte() << 24)>>16;
}
readUnsignedShort() {
return this.readByte() | this.readByte() << 8;
}
readByte() {
let val = this.rawData[this.fp];
this.fp ++;
return val;
}
skip(bytes) {
this.fp += bytes;
}
readFloat() {
let i = this.readInt();
let buffer = new ArrayBuffer(4);
let intView = new Int32Array(buffer);
let floatView = new Float32Array(buffer);
intView[0] = i;
return floatView[0];
}
}