1 var DuplexStream = require('readable-stream/duplex')
2 , util = require('util')
4 function BufferList (callback) {
5 if (!(this instanceof BufferList))
6 return new BufferList(callback)
11 if (typeof callback == 'function') {
12 this._callback = callback
14 var piper = function (err) {
21 this.on('pipe', function (src) {
22 src.on('error', piper)
24 this.on('unpipe', function (src) {
25 src.removeListener('error', piper)
28 else if (Buffer.isBuffer(callback))
30 else if (Array.isArray(callback)) {
31 callback.forEach(function (b) {
32 Buffer.isBuffer(b) && this.append(b)
36 DuplexStream.call(this)
39 util.inherits(BufferList, DuplexStream)
41 BufferList.prototype._offset = function (offset) {
42 var tot = 0, i = 0, _t
43 for (; i < this._bufs.length; i++) {
44 _t = tot + this._bufs[i].length
46 return [ i, offset - tot ]
51 BufferList.prototype.append = function (buf) {
52 var isBuffer = Buffer.isBuffer(buf) ||
53 buf instanceof BufferList
55 // coerce number arguments to strings, since Buffer(number) does
56 // uninitialized memory allocation
57 if (typeof buf == 'number')
60 if (buf instanceof BufferList) {
61 this._bufs.push.apply(this._bufs, buf._bufs)
62 this.length += buf.length
64 this._bufs.push(isBuffer ? buf : new Buffer(buf))
65 this.length += buf.length
71 BufferList.prototype._write = function (buf, encoding, callback) {
77 BufferList.prototype._read = function (size) {
79 return this.push(null)
80 size = Math.min(size, this.length)
81 this.push(this.slice(0, size))
85 BufferList.prototype.end = function (chunk) {
86 DuplexStream.prototype.end.call(this, chunk)
89 this._callback(null, this.slice())
94 BufferList.prototype.get = function (index) {
95 return this.slice(index, index + 1)[0]
98 BufferList.prototype.slice = function (start, end) {
99 return this.copy(null, 0, start, end)
102 BufferList.prototype.copy = function (dst, dstStart, srcStart, srcEnd) {
103 if (typeof srcStart != 'number' || srcStart < 0)
105 if (typeof srcEnd != 'number' || srcEnd > this.length)
107 if (srcStart >= this.length)
108 return dst || new Buffer(0)
110 return dst || new Buffer(0)
113 , off = this._offset(srcStart)
114 , len = srcEnd - srcStart
116 , bufoff = (copy && dstStart) || 0
121 // copy/slice everything
122 if (srcStart === 0 && srcEnd == this.length) {
123 if (!copy) // slice, just return a full concat
124 return Buffer.concat(this._bufs)
126 // copy, need to copy individual buffers
127 for (i = 0; i < this._bufs.length; i++) {
128 this._bufs[i].copy(dst, bufoff)
129 bufoff += this._bufs[i].length
135 // easy, cheap case where it's a subset of one of the buffers
136 if (bytes <= this._bufs[off[0]].length - start) {
138 ? this._bufs[off[0]].copy(dst, dstStart, start, start + bytes)
139 : this._bufs[off[0]].slice(start, start + bytes)
142 if (!copy) // a slice, we need something to copy in to
143 dst = new Buffer(len)
145 for (i = off[0]; i < this._bufs.length; i++) {
146 l = this._bufs[i].length - start
149 this._bufs[i].copy(dst, bufoff, start)
151 this._bufs[i].copy(dst, bufoff, start, start + bytes)
165 BufferList.prototype.toString = function (encoding, start, end) {
166 return this.slice(start, end).toString(encoding)
169 BufferList.prototype.consume = function (bytes) {
170 while (this._bufs.length) {
171 if (bytes >= this._bufs[0].length) {
172 bytes -= this._bufs[0].length
173 this.length -= this._bufs[0].length
176 this._bufs[0] = this._bufs[0].slice(bytes)
184 BufferList.prototype.duplicate = function () {
186 , copy = new BufferList()
188 for (; i < this._bufs.length; i++)
189 copy.append(this._bufs[i])
194 BufferList.prototype.destroy = function () {
195 this._bufs.length = 0;
218 for (var m in methods) {
220 BufferList.prototype[m] = function (offset) {
221 return this.slice(offset, offset + methods[m])[m](0)
227 module.exports = BufferList