Skip to content

Commit

Permalink
Merge pull request #10 from tonistiigi/clone-buffer
Browse files Browse the repository at this point in the history
Allow creating buffers from buffers
  • Loading branch information
toots committed Feb 27, 2013
2 parents df655de + 68b2f74 commit 1b4302b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

0 comments on commit 1b4302b

Please sign in to comment.