Skip to content

Commit 8610821

Browse files
authored
Modernise the datastream class (#424)
1 parent 528f648 commit 8610821

File tree

1 file changed

+60
-52
lines changed

1 file changed

+60
-52
lines changed

utils.js

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -929,56 +929,56 @@ export function ungzip(data) {
929929
return finalData;
930930
}
931931

932-
export function DataStream(name_, data_, dontUnzip_) {
933-
const self = this;
934-
self.name = name_;
935-
self.pos = 0;
936-
self.data = stringToUint8Array(data_);
937-
if (!dontUnzip_ && self.data && self.data.length > 4 && self.data[0] === 0x1f && self.data[1] === 0x8b) {
938-
console.log("Ungzipping " + name_);
939-
self.data = ungzip(self.data);
940-
}
941-
if (!self.data) {
942-
throw new Error("No data in " + name_);
932+
export class DataStream {
933+
constructor(name, data, dontUnzip) {
934+
this.name = name;
935+
this.pos = 0;
936+
this.data = stringToUint8Array(data);
937+
if (!dontUnzip && this.data && this.data.length > 4 && this.data[0] === 0x1f && this.data[1] === 0x8b) {
938+
console.log("Ungzipping " + name);
939+
this.data = ungzip(this.data);
940+
}
941+
if (!this.data) {
942+
throw new Error("No data in " + name);
943+
}
944+
this.end = this.data.length;
943945
}
944946

945-
self.end = self.data.length;
946-
947-
self.bytesLeft = function () {
948-
return self.end - self.pos;
949-
};
947+
bytesLeft() {
948+
return this.end - this.pos;
949+
}
950950

951-
self.eof = function () {
952-
return self.bytesLeft() === 0;
953-
};
951+
eof() {
952+
return this.bytesLeft() === 0;
953+
}
954954

955-
self.advance = function (distance) {
956-
if (self.bytesLeft() < distance) throw new RangeError("EOF in " + self.name);
957-
self.pos += distance;
958-
return self.pos - distance;
959-
};
955+
advance(distance) {
956+
if (this.bytesLeft() < distance) throw new RangeError("EOF in " + this.name);
957+
this.pos += distance;
958+
return this.pos - distance;
959+
}
960960

961-
self.readFloat32 = function (pos) {
962-
if (pos === undefined) pos = self.advance(4);
963-
return readFloat32(self.data, pos);
964-
};
961+
readFloat32(pos) {
962+
if (pos === undefined) pos = this.advance(4);
963+
return readFloat32(this.data, pos);
964+
}
965965

966-
self.readInt32 = function (pos) {
967-
if (pos === undefined) pos = self.advance(4);
968-
return readInt32(self.data, pos);
969-
};
966+
readInt32(pos) {
967+
if (pos === undefined) pos = this.advance(4);
968+
return readInt32(this.data, pos);
969+
}
970970

971-
self.readInt16 = function (pos) {
972-
if (pos === undefined) pos = self.advance(2);
973-
return readInt16(self.data, pos);
974-
};
971+
readInt16(pos) {
972+
if (pos === undefined) pos = this.advance(2);
973+
return readInt16(this.data, pos);
974+
}
975975

976-
self.readByte = function (pos) {
977-
if (pos === undefined) pos = self.advance(1);
978-
return self.data[pos];
979-
};
976+
readByte(pos) {
977+
if (pos === undefined) pos = this.advance(1);
978+
return this.data[pos];
979+
}
980980

981-
self.readNulString = function (pos, maxLength) {
981+
readNulString(pos, maxLength) {
982982
if (!maxLength) maxLength = 1024;
983983
let posToUse = pos === undefined ? self.pos : pos;
984984
let result = "";
@@ -989,24 +989,24 @@ export function DataStream(name_, data_, dontUnzip_) {
989989
if (maxLength === 0) return "";
990990
if (pos === undefined) self.pos = posToUse;
991991
return result;
992-
};
992+
}
993993

994-
self.substream = function (posOrLength, length) {
994+
substream(posOrLength, length) {
995995
let pos;
996996
if (length === undefined) {
997997
length = posOrLength;
998-
pos = self.advance(length);
998+
pos = this.advance(length);
999999
} else {
10001000
pos = posOrLength;
1001-
if (pos + length >= self.end) throw new RangeError("EOF in " + self.name);
1001+
if (pos + length >= this.end) throw new RangeError("EOF in " + this.name);
10021002
}
1003-
return new DataStream(self.name + ".sub", self.data.subarray(pos, pos + length));
1004-
};
1003+
return new DataStream(this.name + ".sub", this.data.subarray(pos, pos + length));
1004+
}
10051005

1006-
self.seek = function (to) {
1007-
if (to >= self.end) throw new RangeError("Seek out of range in " + self.name);
1008-
self.pos = to;
1009-
};
1006+
seek(to) {
1007+
if (to >= this.end) throw new RangeError("Seek out of range in " + this.name);
1008+
this.pos = to;
1009+
}
10101010
}
10111011

10121012
export function makeFast32(u32) {
@@ -1117,28 +1117,36 @@ export class Fifo {
11171117
this._wPtr = 0;
11181118
this._rPtr = 0;
11191119
}
1120+
11201121
/** @returns {number} */
1121-
get size() { return this._size; }
1122+
get size() {
1123+
return this._size;
1124+
}
1125+
11221126
/** @returns {boolean} */
11231127
get full() {
11241128
return this._size === this._buffer.length;
11251129
}
1130+
11261131
/** @returns {boolean} */
11271132
get empty() {
11281133
return this._size === 0;
11291134
}
1135+
11301136
clear() {
11311137
this._size = 0;
11321138
this._wPtr = 0;
11331139
this._rPtr = 0;
11341140
}
1141+
11351142
/** @type {Number} b */
11361143
put(b) {
11371144
if (this.full) return;
11381145
this._buffer[this._wPtr % this._buffer.length] = b;
11391146
this._wPtr++;
11401147
this._size++;
11411148
}
1149+
11421150
/** @returns {Number} */
11431151
get() {
11441152
if (this.empty) return;

0 commit comments

Comments
 (0)