var DuplexStream=require("readable-stream/duplex"),util=require("util"),Buffer=require("safe-buffer").Buffer;function BufferList(t){if(!(this instanceof BufferList))return new BufferList(t);if(this._bufs=[],this.length=0,"function"==typeof t){this._callback=t;var e=function(t){this._callback&&(this._callback(t),this._callback=null)}.bind(this);this.on("pipe",function(t){t.on("error",e)}),this.on("unpipe",function(t){t.removeListener("error",e)})}else this.append(t);DuplexStream.call(this)}util.inherits(BufferList,DuplexStream),BufferList.prototype._offset=function(t){var e,i=0,f=0;if(0===t)return[0,0];for(;f<this._bufs.length;f++){if(t<(e=i+this._bufs[f].length)||f==this._bufs.length-1)return[f,t-i];i=e}},BufferList.prototype.append=function(t){var e=0;if(Buffer.isBuffer(t))this._appendBuffer(t);else if(Array.isArray(t))for(;e<t.length;e++)this.append(t[e]);else if(t instanceof BufferList)for(;e<t._bufs.length;e++)this.append(t._bufs[e]);else null!=t&&("number"==typeof t&&(t=t.toString()),this._appendBuffer(Buffer.from(t)));return this},BufferList.prototype._appendBuffer=function(t){this._bufs.push(t),this.length+=t.length},BufferList.prototype._write=function(t,e,i){this._appendBuffer(t),"function"==typeof i&&i()},BufferList.prototype._read=function(t){if(!this.length)return this.push(null);t=Math.min(t,this.length),this.push(this.slice(0,t)),this.consume(t)},BufferList.prototype.end=function(t){DuplexStream.prototype.end.call(this,t),this._callback&&(this._callback(null,this.slice()),this._callback=null)},BufferList.prototype.get=function(t){return this.slice(t,t+1)[0]},BufferList.prototype.slice=function(t,e){return"number"==typeof t&&t<0&&(t+=this.length),"number"==typeof e&&e<0&&(e+=this.length),this.copy(null,0,t,e)},BufferList.prototype.copy=function(t,e,i,f){if(("number"!=typeof i||i<0)&&(i=0),("number"!=typeof f||f>this.length)&&(f=this.length),i>=this.length)return t||Buffer.alloc(0);if(f<=0)return t||Buffer.alloc(0);var s,r,n=!!t,u=this._offset(i),h=f-i,l=h,o=n&&e||0,p=u[1];if(0===i&&f==this.length){if(!n)return 1===this._bufs.length?this._bufs[0]:Buffer.concat(this._bufs,this.length);for(r=0;r<this._bufs.length;r++)this._bufs[r].copy(t,o),o+=this._bufs[r].length;return t}if(l<=this._bufs[u[0]].length-p)return n?this._bufs[u[0]].copy(t,e,p,p+l):this._bufs[u[0]].slice(p,p+l);for(n||(t=Buffer.allocUnsafe(h)),r=u[0];r<this._bufs.length;r++){if(!(l>(s=this._bufs[r].length-p))){this._bufs[r].copy(t,o,p,p+l);break}this._bufs[r].copy(t,o,p),o+=s,l-=s,p&&(p=0)}return t},BufferList.prototype.shallowSlice=function(t,e){t=t||0,e=e||this.length,t<0&&(t+=this.length),e<0&&(e+=this.length);var i=this._offset(t),f=this._offset(e),s=this._bufs.slice(i[0],f[0]+1);return 0==f[1]?s.pop():s[s.length-1]=s[s.length-1].slice(0,f[1]),0!=i[1]&&(s[0]=s[0].slice(i[1])),new BufferList(s)},BufferList.prototype.toString=function(t,e,i){return this.slice(e,i).toString(t)},BufferList.prototype.consume=function(t){for(;this._bufs.length;){if(!(t>=this._bufs[0].length)){this._bufs[0]=this._bufs[0].slice(t),this.length-=t;break}t-=this._bufs[0].length,this.length-=this._bufs[0].length,this._bufs.shift()}return this},BufferList.prototype.duplicate=function(){for(var t=0,e=new BufferList;t<this._bufs.length;t++)e.append(this._bufs[t]);return e},BufferList.prototype.destroy=function(){this._bufs.length=0,this.length=0,this.push(null)},function(){var t={readDoubleBE:8,readDoubleLE:8,readFloatBE:4,readFloatLE:4,readInt32BE:4,readInt32LE:4,readUInt32BE:4,readUInt32LE:4,readInt16BE:2,readInt16LE:2,readUInt16BE:2,readUInt16LE:2,readInt8:1,readUInt8:1};for(var e in t)!function(e){BufferList.prototype[e]=function(i){return this.slice(i,i+t[e])[e](0)}}(e)}(),module.exports=BufferList;