Initial commit
[yaffs-website] / node_modules / stdout-stream / node_modules / readable-stream / lib / internal / streams / BufferList.js
1 'use strict';
2
3 var Buffer = require('buffer').Buffer;
4 /*<replacement>*/
5 var bufferShim = require('buffer-shims');
6 /*</replacement>*/
7
8 module.exports = BufferList;
9
10 function BufferList() {
11   this.head = null;
12   this.tail = null;
13   this.length = 0;
14 }
15
16 BufferList.prototype.push = function (v) {
17   var entry = { data: v, next: null };
18   if (this.length > 0) this.tail.next = entry;else this.head = entry;
19   this.tail = entry;
20   ++this.length;
21 };
22
23 BufferList.prototype.unshift = function (v) {
24   var entry = { data: v, next: this.head };
25   if (this.length === 0) this.tail = entry;
26   this.head = entry;
27   ++this.length;
28 };
29
30 BufferList.prototype.shift = function () {
31   if (this.length === 0) return;
32   var ret = this.head.data;
33   if (this.length === 1) this.head = this.tail = null;else this.head = this.head.next;
34   --this.length;
35   return ret;
36 };
37
38 BufferList.prototype.clear = function () {
39   this.head = this.tail = null;
40   this.length = 0;
41 };
42
43 BufferList.prototype.join = function (s) {
44   if (this.length === 0) return '';
45   var p = this.head;
46   var ret = '' + p.data;
47   while (p = p.next) {
48     ret += s + p.data;
49   }return ret;
50 };
51
52 BufferList.prototype.concat = function (n) {
53   if (this.length === 0) return bufferShim.alloc(0);
54   if (this.length === 1) return this.head.data;
55   var ret = bufferShim.allocUnsafe(n >>> 0);
56   var p = this.head;
57   var i = 0;
58   while (p) {
59     p.data.copy(ret, i);
60     i += p.data.length;
61     p = p.next;
62   }
63   return ret;
64 };