forked from EasyDarwin/EasyDarwin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer-pool.js
53 lines (44 loc) · 881 Bytes
/
buffer-pool.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
const Readable = require('stream').Readable;
class BufferPool extends Readable {
constructor(genFun, options) {
super(options);
this.gFun = genFun;
}
_read(size) {
}
init() {
this.readBytes = 0;
this.poolBytes = 0;
this.needBytes = 0;
this.gFun.next(false);
}
stop() {
try {
this.gFun.next(true);
} catch (e) {
// console.log(e);
}
}
push(buf) {
super.push(buf);
this.poolBytes += buf.length;
this.readBytes += buf.length;
if (this.needBytes > 0 && this.needBytes <= this.poolBytes) {
this.gFun.next(false);
}
}
read(size) {
this.poolBytes -= size;
return super.read(size);
}
need(size) {
let ret = this.poolBytes < size;
if (ret) {
this.needBytes = size;
} else {
this.needBytes = 0;
}
return ret;
}
}
module.exports = BufferPool