1 var DuplexStream = require('readable-stream/duplex')
2 , util = require('util')
5 function BufferList (callback) {
6 if (!(this instanceof BufferList))
7 return new BufferList(callback)
12 if (typeof callback == 'function') {
13 this._callback = callback
15 var piper = function piper (err) {
22 this.on('pipe', function onPipe (src) {
23 src.on('error', piper)
25 this.on('unpipe', function onUnpipe (src) {
26 src.removeListener('error', piper)
32 DuplexStream.call(this)
36 util.inherits(BufferList, DuplexStream)
39 BufferList.prototype._offset = function _offset (offset) {
40 var tot = 0, i = 0, _t
41 for (; i < this._bufs.length; i++) {
42 _t = tot + this._bufs[i].length
44 return [ i, offset - tot ]
50 BufferList.prototype.append = function append (buf) {
54 if (Array.isArray(buf)) {
55 for (; i < buf.length; i++)
57 } else if (buf instanceof BufferList) {
58 // unwrap argument into individual BufferLists
59 for (; i < buf._bufs.length; i++)
60 this.append(buf._bufs[i])
61 } else if (buf != null) {
62 // coerce number arguments to strings, since Buffer(number) does
63 // uninitialized memory allocation
64 if (typeof buf == 'number')
67 newBuf = Buffer.isBuffer(buf) ? buf : new Buffer(buf)
68 this._bufs.push(newBuf)
69 this.length += newBuf.length
76 BufferList.prototype._write = function _write (buf, encoding, callback) {
79 if (typeof callback == 'function')
84 BufferList.prototype._read = function _read (size) {
86 return this.push(null)
88 size = Math.min(size, this.length)
89 this.push(this.slice(0, size))
94 BufferList.prototype.end = function end (chunk) {
95 DuplexStream.prototype.end.call(this, chunk)
98 this._callback(null, this.slice())
104 BufferList.prototype.get = function get (index) {
105 return this.slice(index, index + 1)[0]
109 BufferList.prototype.slice = function slice (start, end) {
110 return this.copy(null, 0, start, end)
114 BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
115 if (typeof srcStart != 'number' || srcStart < 0)
117 if (typeof srcEnd != 'number' || srcEnd > this.length)
119 if (srcStart >= this.length)
120 return dst || new Buffer(0)
122 return dst || new Buffer(0)
125 , off = this._offset(srcStart)
126 , len = srcEnd - srcStart
128 , bufoff = (copy && dstStart) || 0
133 // copy/slice everything
134 if (srcStart === 0 && srcEnd == this.length) {
135 if (!copy) // slice, just return a full concat
136 return Buffer.concat(this._bufs)
138 // copy, need to copy individual buffers
139 for (i = 0; i < this._bufs.length; i++) {
140 this._bufs[i].copy(dst, bufoff)
141 bufoff += this._bufs[i].length
147 // easy, cheap case where it's a subset of one of the buffers
148 if (bytes <= this._bufs[off[0]].length - start) {
150 ? this._bufs[off[0]].copy(dst, dstStart, start, start + bytes)
151 : this._bufs[off[0]].slice(start, start + bytes)
154 if (!copy) // a slice, we need something to copy in to
155 dst = new Buffer(len)
157 for (i = off[0]; i < this._bufs.length; i++) {
158 l = this._bufs[i].length - start
161 this._bufs[i].copy(dst, bufoff, start)
163 this._bufs[i].copy(dst, bufoff, start, start + bytes)
177 BufferList.prototype.toString = function toString (encoding, start, end) {
178 return this.slice(start, end).toString(encoding)
181 BufferList.prototype.consume = function consume (bytes) {
182 while (this._bufs.length) {
183 if (bytes >= this._bufs[0].length) {
184 bytes -= this._bufs[0].length
185 this.length -= this._bufs[0].length
188 this._bufs[0] = this._bufs[0].slice(bytes)
197 BufferList.prototype.duplicate = function duplicate () {
199 , copy = new BufferList()
201 for (; i < this._bufs.length; i++)
202 copy.append(this._bufs[i])
208 BufferList.prototype.destroy = function destroy () {
209 this._bufs.length = 0
233 for (var m in methods) {
235 BufferList.prototype[m] = function (offset) {
236 return this.slice(offset, offset + methods[m])[m](0)
243 module.exports = BufferList