-
Notifications
You must be signed in to change notification settings - Fork 390
/
Copy pathstream.js
38 lines (32 loc) · 820 Bytes
/
stream.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
// Stream
var Stream = function(data) {
this.data = data;
this.len = this.data.length;
this.pos = 0;
this.read = function(n) {
if (this.pos + n > this.data.length)
throw new Error("Attempted to read past end of stream.");
return this.data.substring(this.pos, this.pos += n);
}
this.readToEnd = function() {
return this.read(this.data.length - this.pos);
}
this.readBytes = function(n) {
var s = this.read(n);
var a = [];
for (var i = 0; i < s.length; i++) {
a.push(s.charCodeAt(i));
}
return a;
}
this.readByte = function() {
return this.readBytes(1)[0];
}
this.readUnsigned = function() { // Little-endian.
var a = this.readBytes(2);
return (a[1] << 8) + a[0];
}
}
if (typeof exports != 'undefined') {
exports.Stream = Stream;
}