From: Tõnis Tiigi Date: Wed, 27 Feb 2013 19:29:02 +0000 (+0200) Subject: Allow creating buffers from buffers X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=68b2f74d772504c2b52c2a1a61d30ca5a680bdf6;p=buffer.git Allow creating buffers from buffers --- diff --git a/index.js b/index.js index 522dd93..894532c 100644 --- a/index.js +++ b/index.js @@ -359,7 +359,12 @@ function Buffer(subject, encoding, offset) { // Treat array-ish objects as a byte array. if (isArrayIsh(subject)) { for (var i = 0; i < this.length; i++) { - this.parent[i + this.offset] = subject[i]; + if (subject instanceof Buffer) { + this.parent[i + this.offset] = subject.readUInt8(i); + } + else { + this.parent[i + this.offset] = subject[i]; + } } } else if (type == 'string') { // We are a string diff --git a/test/buffer.js b/test/buffer.js index f54c5d2..2cac5d2 100644 --- a/test/buffer.js +++ b/test/buffer.js @@ -119,3 +119,11 @@ test("concat() a varying number of buffers", function (t) { t.equal(flatLongLen.toString(), (new Array(10+1).join('asdf'))); t.end(); }); + +test("buffer from buffer", function (t) { + t.plan(1); + var b1 = new buffer.Buffer('asdf'); + var b2 = new buffer.Buffer(b1); + t.equal(b1.toString('hex'), b2.toString('hex')); + t.end(); +});