From 34b834a7539b338458b3414da4ea39392ab4b523 Mon Sep 17 00:00:00 2001 From: Rainer Dreyer Date: Fri, 16 Nov 2012 00:45:18 +0200 Subject: [PATCH] Added concat() Fixes toots/buffer-browserify#6 --- index.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/index.js b/index.js index b13ca2d..258695d 100644 --- a/index.js +++ b/index.js @@ -392,6 +392,39 @@ Buffer.isBuffer = function isBuffer(b) { return b instanceof Buffer || b instanceof SlowBuffer; }; +Buffer.concat = function (list, totalLength) { + if (!Array.isArray(list)) { + throw new Error("Usage: Buffer.concat(list, [totalLength])\n \ + list should be an Array."); + } + + if (list.length === 0) { + return new Buffer(0); + } else if (list.length === 1) { + return list[0]; + } + + if (typeof totalLength !== 'number') { + totalLength = 0; + for (var i = 0; i < list.length; i++) { + var buf = list[i]; + totalLength += buf.length; + } + } + + var buffer = new Buffer(totalLength); + var pos = 0; + for (var i = 0; i < list.length; i++) { + var buf = list[i]; + // To deal with string objects + if (!Buffer.isBuffer(buf)) { + buf = new Buffer(buf); + } + buf.copy(buffer, pos); + pos += buf.length; + } + return buffer; +}; // Inspect Buffer.prototype.inspect = function inspect() { -- 2.34.1