/
index.js
66 lines (58 loc) · 1.47 KB
/
index.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
/**
* BufferQueue constructor.
*/
function BufferQueue () {
this._buffers = [];
this._buffersLength = 0;
}
module.exports = BufferQueue;
/**
* Removes the first `n` bytes out of the queue
* and returns them. If `n` is greater than the current
* buffer size, return as much as possible.
*
* @param {Number} bytes
* @return {Buffer}
*/
BufferQueue.prototype.shift = function (bytes) {
// If trying to shift more space than the internal buffer has, cap it
// at the current size of the queue.
bytes = bytes > this._buffersLength ? this._buffersLength : bytes;
var data = Buffer.concat(this._buffers, this._buffersLength);
var front = data.slice(0, bytes);
this.empty();
this.push(data.slice(bytes));
return front;
};
/**
* Merges the buffer into the buffer queue.
*
* @param {Buffer} buffer
*/
BufferQueue.prototype.push = function (buffer) {
this._buffers.push(buffer);
this._buffersLength += buffer.length;
};
/**
* Clears out the internal buffer queue.
*/
BufferQueue.prototype.empty = function () {
this._buffers.length = 0;
this._buffersLength = 0;
};
/**
* Returns the size of all the queued buffers.
*/
BufferQueue.prototype.length = function () {
return this._buffersLength;
};
/**
* Returns the aggregate buffer and empties the internal queue.
*
* @return {Buffer}
*/
BufferQueue.prototype.drain = function () {
var data = Buffer.concat(this._buffers, this.buffersLength);
this._buffers.length = this._buffersLength = 0;
return data;
};