stream_reader.js
1.82 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
'use strict';
var StreamReader = function() {
this._queue = [];
this._queueSize = 0;
this._offset = 0;
};
StreamReader.prototype.put = function(buffer) {
if (!buffer || buffer.length === 0) return;
if (!buffer.copy) buffer = new Buffer(buffer);
this._queue.push(buffer);
this._queueSize += buffer.length;
};
StreamReader.prototype.read = function(length) {
if (length > this._queueSize) return null;
if (length === 0) return new Buffer(0);
this._queueSize -= length;
var queue = this._queue,
remain = length,
first = queue[0],
buffers, buffer;
if (first.length >= length) {
if (first.length === length) {
return queue.shift();
} else {
buffer = first.slice(0, length);
queue[0] = first.slice(length);
return buffer;
}
}
for (var i = 0, n = queue.length; i < n; i++) {
if (remain < queue[i].length) break;
remain -= queue[i].length;
}
buffers = queue.splice(0, i);
if (remain > 0 && queue.length > 0) {
buffers.push(queue[0].slice(0, remain));
queue[0] = queue[0].slice(remain);
}
return this._concat(buffers, length);
};
StreamReader.prototype.eachByte = function(callback, context) {
var buffer, n, index;
while (this._queue.length > 0) {
buffer = this._queue[0];
n = buffer.length;
while (this._offset < n) {
index = this._offset;
this._offset += 1;
callback.call(context, buffer[index]);
}
this._offset = 0;
this._queue.shift();
}
};
StreamReader.prototype._concat = function(buffers, length) {
if (Buffer.concat) return Buffer.concat(buffers, length);
var buffer = new Buffer(length),
offset = 0;
for (var i = 0, n = buffers.length; i < n; i++) {
buffers[i].copy(buffer, offset);
offset += buffers[i].length;
}
return buffer;
};
module.exports = StreamReader;