readable_streambuffer.js
3.03 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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
var stream = require("stream"),
constants = require("./constants"),
util = require("util");
var ReadableStreamBuffer = module.exports = function(opts) {
var that = this;
stream.Stream.call(this);
opts = opts || {};
var frequency = opts.hasOwnProperty("frequency") ? opts.frequency : constants.DEFAULT_FREQUENCY;
var chunkSize = opts.chunkSize || constants.DEFAULT_CHUNK_SIZE;
var initialSize = opts.initialSize || constants.DEFAULT_INITIAL_SIZE;
var incrementAmount = opts.incrementAmount || constants.DEFAULT_INCREMENT_AMOUNT;
var size = 0;
var buffer = new Buffer(initialSize);
var encoding = null;
this.readable = true;
this.writable = false;
var sendData = function() {
var amount = Math.min(chunkSize, size);
if (amount > 0) {
var chunk = null;
if(encoding) {
chunk = buffer.toString(encoding, 0, amount);
}
else {
chunk = new Buffer(amount);
buffer.copy(chunk, 0, 0, amount);
}
that.emit("data", chunk);
if(amount < buffer.length)
buffer.copy(buffer, 0, amount, size);
size -= amount;
}
if(size === 0 && !that.readable) {
that.emit("end");
that.emit("close");
if (sendData && sendData.interval) {
clearInterval(sendData.interval);
sendData.interval = null;
}
}
};
this.size = function() {
return size;
};
this.maxSize = function() {
return buffer.length;
};
var increaseBufferIfNecessary = function(incomingDataSize) {
if((buffer.length - size) < incomingDataSize) {
var factor = Math.ceil((incomingDataSize - (buffer.length - size)) / incrementAmount);
var newBuffer = new Buffer(buffer.length + (incrementAmount * factor));
buffer.copy(newBuffer, 0, 0, size);
buffer = newBuffer;
}
};
this.put = function(data, encoding) {
if(!that.readable) return;
var wasEmpty = size === 0;
if(Buffer.isBuffer(data)) {
increaseBufferIfNecessary(data.length);
data.copy(buffer, size, 0);
size += data.length;
}
else {
data = data + "";
var dataSizeInBytes = Buffer.byteLength(data);
increaseBufferIfNecessary(dataSizeInBytes);
buffer.write(data, size, encoding || "utf8");
size += dataSizeInBytes;
}
if (wasEmpty && size > 0) {
this.emit('readable')
}
if (!this.isPaused && !frequency) {
while (size > 0) {
sendData();
}
}
};
this.pause = function() {
this.isPaused = true;
if(sendData && sendData.interval) {
clearInterval(sendData.interval);
delete sendData.interval;
}
};
this.resume = function() {
this.isPaused = false;
if(sendData && !sendData.interval && frequency > 0) {
sendData.interval = setInterval(sendData, frequency);
}
};
this.destroy = function() {
that.emit("end");
if(sendData.interval) clearInterval(sendData.interval);
sendData = null;
that.readable = false;
that.emit("close");
};
this.destroySoon = function() {
that.readable = false;
if (!sendData.interval) {
that.emit("end");
that.emit("close");
}
};
this.setEncoding = function(_encoding) {
encoding = _encoding;
};
this.resume();
};
util.inherits(ReadableStreamBuffer, stream.Stream);