-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBufferedReader.js
76 lines (75 loc) · 1.96 KB
/
BufferedReader.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
const BUF_SIZE = 1024 * 10;
export class BufferedReader {
constructor(reader) {
this.r = reader;
this.len = 0;
this.pos = 0;
this.eof = false;
this.decoder = new TextDecoder();
}
async read(len) {
//console.log(this.buf.length - this.pos, this.buf.length)
if (this.len - this.pos == 0) {
if (this.eof) {
throw new Error("eof");
}
const { value, done } = await this.r.read(new Uint8Array(BUF_SIZE));
//console.log("read1", value, done, this.r);
if (value === null) {
throw new Error("eof");
}
this.buf = value;
//console.log("read", this.buf?.length);
this.len = value?.length;
this.pos = 0;
this.eof = done;
}
//console.log("BUF", this.buf)
const res = new Uint8Array(len);
let off = 0;
for (;;) {
//console.log("for", len, this.len, this.pos);
const remain = this.len - this.pos;
if (len <= remain) {
for (let i = 0; i < len; i++) {
res[off + i] = this.buf[this.pos + i];
}
this.pos += len;
return res;
} else {
for (let i = 0; i < remain; i++) {
res[off + i] = this.buf[this.pos + i];
}
off += remain;
len -= remain;
if (this.eof) {
throw new Error("eof");
}
const { value, done } = await this.r.read(new Uint8Array(BUF_SIZE));
//console.log("read2", value?.length, done);
this.buf = value;
this.pos = 0;
this.len = value?.length;
this.eof = done;
}
}
}
async readString(len) {
return this.decoder.decode(await this.read(len));
}
async readInt() {
const b = await this.read(4);
return b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24);
}
async readShort() {
const b = await this.read(2);
return b[0] | (b[1] << 8);
}
async readByte() {
const b = await this.read(1);
return b[0];
}
async close() {
await this.r.close();
}
}