Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
Accept both byte arrays and buffers (closes #15)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjmulder committed Oct 2, 2015
1 parent 39665c8 commit 6b339ba
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
3 changes: 2 additions & 1 deletion nbt.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@
this[nbt.tagTypes.byteArray] = function(value) {
this.int(value.length);
accommodate(value.length);
value.copy(this.buffer, this.offset);
var valueBuffer = 'copy' in value ? value : new Buffer(value);
valueBuffer.copy(this.buffer, this.offset);
this.offset += value.length;
return this;
};
Expand Down
4 changes: 2 additions & 2 deletions sample/bigtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ module.exports = {
},
"byteArrayTest (the first 1000 values of (n*n*255+n*7)%100, starting with n=0 (0, 62, 34, 16, 8, ...))": {
"type": "byteArray",
"value": new Buffer([
"value": [
0,
62,
34,
Expand Down Expand Up @@ -1124,7 +1124,7 @@ module.exports = {
74,
6,
48
])
]
},
"doubleTest": {
"type": "double",
Expand Down
12 changes: 11 additions & 1 deletion test/writer-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('nbt.Writer', function() {
expect(writer.double(1).buffer).to.deep.equal(buffer);
});

it('writes 8-bit byte arrays', function() {
it('writes 8-bit byte arrays from buffers', function() {
var writer = new nbt.Writer();
var buffer = new Buffer([
0,0,0,2, 1,2,
Expand All @@ -71,6 +71,16 @@ describe('nbt.Writer', function() {
expect(writer.byteArray(new Buffer([3, 4, 5, 6])).buffer).to.deep.equal(buffer);
});

it('writes 8-bit byte arrays from plain arrays', function() {
var writer = new nbt.Writer();
var buffer = new Buffer([
0,0,0,2, 1,2,
0,0,0,4, 3,4,5,6
]);
expect(writer.byteArray([1, 2]).buffer).to.deep.equal(buffer.slice(0, 6));
expect(writer.byteArray([3, 4, 5, 6]).buffer).to.deep.equal(buffer);
});

it('writes 32-bit int arrays', function() {
var writer = new nbt.Writer();
var buffer = new Buffer([
Expand Down

0 comments on commit 6b339ba

Please sign in to comment.