node/lib/internal/streams/BufferList.js
Luigi Pinca 8f830ca896
stream: remove unreachable code
To avoid a function call `BufferList.prototype.concat()` is not called
when there is only a buffer in the list. That buffer is instead
accessed directly.

Backport-PR-URL: https://github.com/nodejs/node/pull/19483
PR-URL: https://github.com/nodejs/node/pull/18239
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2018-03-29 23:25:37 -04:00

71 lines
1.3 KiB
JavaScript

'use strict';
const Buffer = require('buffer').Buffer;
module.exports = BufferList;
function BufferList() {
this.head = null;
this.tail = null;
this.length = 0;
}
BufferList.prototype.push = function(v) {
const entry = { data: v, next: null };
if (this.length > 0)
this.tail.next = entry;
else
this.head = entry;
this.tail = entry;
++this.length;
};
BufferList.prototype.unshift = function(v) {
const entry = { data: v, next: this.head };
if (this.length === 0)
this.tail = entry;
this.head = entry;
++this.length;
};
BufferList.prototype.shift = function() {
if (this.length === 0)
return;
const ret = this.head.data;
if (this.length === 1)
this.head = this.tail = null;
else
this.head = this.head.next;
--this.length;
return ret;
};
BufferList.prototype.clear = function() {
this.head = this.tail = null;
this.length = 0;
};
BufferList.prototype.join = function(s) {
if (this.length === 0)
return '';
var p = this.head;
var ret = '' + p.data;
while (p = p.next)
ret += s + p.data;
return ret;
};
BufferList.prototype.concat = function(n) {
if (this.length === 0)
return Buffer.alloc(0);
const ret = Buffer.allocUnsafe(n >>> 0);
var p = this.head;
var i = 0;
while (p) {
p.data.copy(ret, i);
i += p.data.length;
p = p.next;
}
return ret;
};