Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
sudo: false
language: node_js
node_js:
- 'iojs-1'
- '4'
- '3'
- '2'
- '1'
- '0.12'
- '0.11'
- '0.10'
Expand Down
68 changes: 32 additions & 36 deletions lib/v2/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,38 +164,36 @@ utils.addByteCodes(BYTE_CODES, [
* @return {Number}
* @api public
*/
proto.readLong = function () {
proto.readLong = function (withType) {
var result;
var code = this.byteBuffer.get();
// Compact long
if (code >= 0xd8 && code <= 0xef) {
// Longs between -8 and 15 are represented by a single octet in the range xd8 to xef.
// value = (code - 0xe0)
return code - 0xe0;
}
if (code >= 0xf0 && code <= 0xff) {
result = code - 0xe0;
} else if (code >= 0xf0 && code <= 0xff) {
// Longs between -2048 and 2047 are encoded in two octets with the leading byte in the range xf0 to xff.
// value = ((code - 0xf8) << 8) + b0
return ((code - 0xf8) << 8) + this.byteBuffer.get();
}
if (code >= 0x38 && code <= 0x3f) {
result = ((code - 0xf8) << 8) + this.byteBuffer.get();
} else if (code >= 0x38 && code <= 0x3f) {
// Longs between -262144 and 262143 are encoded in three octets with the leading byte in the range x38 to x3f.
// value = ((code - 0x3c) << 16) + (b1 << 8) + b0
var b1 = this.byteBuffer.get();
var b0 = this.byteBuffer.get();
return ((code - 0x3c) << 16) + (b1 << 8) + b0;
}
result = ((code - 0x3c) << 16) + (b1 << 8) + b0;
// ::= x77 b3 b2 b1 b0 # 32-bit integer cast to long
if (code === 0x77) {
} else if (code === 0x77) {
// Longs between which fit into 32-bits are encoded in five octets with the leading byte x59.
// value = (b3 << 24) + (b2 << 16) + (b1 << 8) + b0
return this.byteBuffer.getInt32();
}

if (code === 0x4c) {
return utils.handleLong(this.byteBuffer.getLong());
result = this.byteBuffer.getInt32();
} else if (code === 0x4c) {
result = utils.handleLong(this.byteBuffer.getLong());
} else {
this.throwError('readLong', code);
}

this.throwError('readLong', code);
return this.handleType('long', result, withType);
};

utils.addByteCodes(BYTE_CODES, [
Expand All @@ -214,29 +212,27 @@ utils.addByteCodes(BYTE_CODES, [
* @return {Number}
* @api public
*/
proto.readDouble = function () {
proto.readDouble = function (withType) {
var result;
var code = this.byteBuffer.get();
if (code === 0x44) {
return this.byteBuffer.getDouble();
}

result = this.byteBuffer.getDouble();
// Compact double
if (code === 0x67) {
return 0.0;
}
if (code === 0x68) {
return 1.0;
}
if (code === 0x69) {
return this.byteBuffer.getInt8();
}
if (code === 0x6a) {
return this.byteBuffer.getInt16();
}
if (code === 0x6b) {
return this.byteBuffer.getFloat();
} else if (code === 0x67) {
result = 0.0;
} else if (code === 0x68) {
result = 1.0;
} else if (code === 0x69) {
result = this.byteBuffer.getInt8();
} else if (code === 0x6a) {
result = this.byteBuffer.getInt16();
} else if (code === 0x6b) {
result = this.byteBuffer.getFloat();
} else {
this.throwError('readDouble', code);
}
this.throwError('readDouble', code);

return this.handleType('double', result, withType);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

测试用例里面没有对应的?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我补一下。

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不过最关键的是上面我说的那个问题,有哪些信息 encode(decode(buffer)) 之后可能无法完全一致

};

utils.addByteCodes(BYTE_CODES, [
Expand Down Expand Up @@ -756,7 +752,7 @@ proto.readMap = function (withType) {
if (!type) {
var map = {};
this._addRef(map);
this._readMap(map);
this._readMap(map, withType);
return map;
}

Expand Down
34 changes: 34 additions & 0 deletions test/double.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ describe('double.test.js', function () {
hessian.decode(utils.bytes('v1/double/2147483646.456'), '1.0').should.equal(2147483646.456);
});

it('should decode with type', function () {
hessian.decode(utils.bytes('v1/double/0'), '1.0', true).should.eql({
$class: 'double',
$: 0,
});

hessian.decode(utils.bytes('v1/double/-127.9999'), '1.0', true).should.eql({
$class: 'double',
$: -127.9999,
});

hessian.decode(utils.bytes('v1/double/-2147483647'), '1.0', true).should.eql({
$class: 'double',
$: -2147483647,
});
});

describe('v2.0', function () {
it('should read 0.0 and 1.0', function () {
hessian.decode(new Buffer([0x67]), '2.0').should.equal(0.0);
Expand Down Expand Up @@ -133,6 +150,23 @@ describe('double.test.js', function () {
hessian.decode(new Buffer([0x44, 0x40, 0x24, 0, 0, 0, 0, 0, 0]), '2.0').should.equal(10.0);
});

it('should decode with type', function () {
hessian.decode(new Buffer([0x69, 0x01]), '2.0', true).should.eql({
$class: 'double',
$: 1.0,
});

hessian.decode(new Buffer([0x44, 0x40, 0x24, 0, 0, 0, 0, 0, 0]), '2.0', true).should.eql({
$class: 'double',
$: 10.0,
});

hessian.decode(new Buffer([0x6a, 0x00, 0x80]), '2.0', true).should.eql({
$class: 'double',
$: 128.0,
});
});

it('should write 0.0 and 1.0', function () {
hessian.encode(java.double(0), '2.0').should.eql(new Buffer([0x67]));
hessian.encode(java.double(0.0), '2.0').should.eql(new Buffer([0x67]));
Expand Down
35 changes: 35 additions & 0 deletions test/long.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,23 @@ describe('long.test.js', function () {
hessian.decode(utils.bytes('v1/long/2147483648')).should.equal(2147483648);
});

it('should decode with type', function () {
hessian.decode(utils.bytes('v1/long/-7'), true).should.eql({
$class: 'long',
$: -7,
});

hessian.decode(utils.bytes('v1/long/262143'), true).should.eql({
$class: 'long',
$: 262143,
});

hessian.decode(utils.bytes('v1/long/2147483648'), true).should.eql({
$class: 'long',
$: 2147483648,
});
});

describe('v2.0', function () {
it('should read compact long', function () {
hessian.decode(new Buffer([0xe0]), '2.0').should.equal(0);
Expand Down Expand Up @@ -295,4 +312,22 @@ describe('long.test.js', function () {
hessian.decode(utils.bytes('v1/long/2147483648'), '2.0').should.equal(2147483648);
});
});

it('should decode with type', function () {
hessian.decode(new Buffer([0x38, 0x00, 0x00]), '2.0', true).should.eql({
$class: 'long',
$: -262144,
});

hessian.decode(utils.bytes('v2/long/-2048'), '2.0', true).should.eql({
$class: 'long',
$: -2048,
});

hessian.decode(utils.bytes('v2/long/2147483646'), '2.0', true).should.eql({
$class: 'long',
$: 2147483646,
});
});

});
10 changes: 8 additions & 2 deletions test/object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,10 @@ describe('object.test.js', function () {
a0.should.eql({
$class: 'java.util.concurrent.atomic.AtomicLong',
$: {
value: 0
value: {
$class: 'long',
$: 0,
},
}
});
hessian.encode({
Expand All @@ -830,7 +833,10 @@ describe('object.test.js', function () {
a1.should.eql({
$class: 'java.util.concurrent.atomic.AtomicLong',
$: {
value: 1
value: {
$class: 'long',
$: 1,
},
}
});
hessian.encode({
Expand Down
2 changes: 2 additions & 0 deletions test/string.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ var hessian = require('../');
var utils = require('./utils');

describe('string.test.js', function () {
this.timeout(0);

var helloBuffer = Buffer.concat([new Buffer(['S'.charCodeAt(0), 0x00, 0x05]),
new Buffer('hello')]);

Expand Down