Skip to content

Commit

Permalink
Support (U)Int8 8 bit int
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Feb 8, 2014
1 parent ff8dd81 commit 6c353fe
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/byte.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,26 @@ ByteBuffer.prototype.getChar = function (index) {
};

ByteBuffer.prototype._putValue = function (index, value, method) {
method += this._order === BIG_ENDIAN ? 'BE' : 'LE';
if (method.indexOf('Int8') < 0) {
method += this._order === BIG_ENDIAN ? 'BE' : 'LE';
}
this._bytes[method](value, index);
return this;
};

ByteBuffer.prototype._getValue = function (index, method) {
method += this._order === BIG_ENDIAN ? 'BE' : 'LE';
if (method.indexOf('Int8') < 0) {
method += this._order === BIG_ENDIAN ? 'BE' : 'LE';
}
return this._bytes[method](index);
};

// put / get number types
var types = {
Double: {size: 8, bufMethod: 'Double'},
Float: {size: 4, bufMethod: 'Float'},
Int8: {size: 1, bufMethod: 'Int8'},
UInt8: {size: 1, bufMethod: 'UInt8'},
Short: {size: 2, bufMethod: 'Int16'},
Int16: {size: 2, bufMethod: 'Int16'},
UInt16: {size: 2, bufMethod: 'UInt16'},
Expand Down
31 changes: 31 additions & 0 deletions test/byte.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,37 @@ describe('byte.test.js', function () {
});
});

describe('putInt8(), getInt8(), putUInt8(), getUInt8()', function () {
it('should put and get 8 bits int', function () {
var bytes = ByteBuffer.allocate(1);
bytes.putInt8(-128);
bytes.toString().should.equal('<ByteBuffer 80>');
bytes.putInt8(0, -128);
bytes.toString().should.equal('<ByteBuffer 80>');
bytes.position(0);
bytes.getInt8().should.equal(-128);
bytes.putInt8(0, 0);
bytes.toString().should.equal('<ByteBuffer 00>');
bytes.getInt8(0).should.equal(0);

bytes.putInt8(0, -128);
bytes.toString().should.equal('<ByteBuffer 80>');

bytes.putInt8(0, 127);
bytes.toString().should.equal('<ByteBuffer 7f>');

bytes.position(0);
bytes.putUInt8(255);
bytes.toString().should.equal('<ByteBuffer ff>');
bytes.position(0);
bytes.getUInt8().should.equal(255);

bytes.putUInt8(0, 0);
bytes.toString().should.equal('<ByteBuffer 00>');
bytes.getUInt8(0).should.equal(0);
});
});

describe('putUInt(), getUInt()', function () {
it('should put an int', function () {
var cases = [
Expand Down

0 comments on commit 6c353fe

Please sign in to comment.