Skip to content

Commit

Permalink
Fix to allow for null buffers to be passed in.
Browse files Browse the repository at this point in the history
  • Loading branch information
justinfreitag committed Mar 5, 2012
1 parent 02c54b2 commit 2362e79
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions bufferjs/concat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,30 @@
"use strict";

function concat(bufs) {
var buffer, length = 0, index = 0;

if (!Array.isArray(bufs)) {
bufs = Array.prototype.slice.call(arguments);
}
for (var i=0, l=bufs.length; i<l; i++) {
buffer = bufs[i];
if (!Buffer.isBuffer(buffer)) {
buffer = bufs[i] = new Buffer(buffer);

var bufsToConcat = [], length = 0;
bufsToConcat.forEach(function (buf) {
if (buf) {
if (!Buffer.isBuffer(buf)) {
buf = new Buffer(buf);
}
length += buf.length;
bufsToConcat.push(buf);
}
length += buffer.length;
}
buffer = new Buffer(length);

bufs.forEach(function (buf, i) {
buf = bufs[i];
buf.copy(buffer, index, 0, buf.length);

var concatBuf = new Buffer(length), index = 0;
bufsToConcat.forEach(function (buf) {
buf.copy(concatBuf, index, 0, buf.length);
index += buf.length;
delete bufs[i];
});

return buffer;
return concatBuf;
}

Buffer.concat = concat;

}());

0 comments on commit 2362e79

Please sign in to comment.