Skip to content
Open
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
108 changes: 54 additions & 54 deletions lib/v2/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,31 +93,31 @@ utils.addByteCodes(BYTE_CODES, [
* @return {Number}
* @api public
*/
proto.readInt = function () {
proto.readInt = function (withType) {
var code = this.byteBuffer.get();
var val;
// Compact int
if (code >= 0x80 && code <= 0xbf) {
// Integers between -16 and 47 can be encoded by a single octet in the range x80 to xbf.
// value = code - 0x90
return code - 0x90;
}
if (code >= 0xc0 && code <= 0xcf) {
val = code - 0x90;
} else if (code >= 0xc0 && code <= 0xcf) {
// Integers between -2048 and 2047 can be encoded in two octets with the leading byte in the range xc0 to xcf.
// value = ((code - 0xc8) << 8) + b0;
return ((code - 0xc8) << 8) + this.byteBuffer.get();
}
if (code >= 0xd0 && code <= 0xd7) {
val = ((code - 0xc8) << 8) + this.byteBuffer.get();
} else if (code >= 0xd0 && code <= 0xd7) {
// Integers between -262144 and 262143 can be encoded in three bytes with the leading byte in the range xd0 to xd7.
// value = ((code - 0xd4) << 16) + (b1 << 8) + b0;
var b1 = this.byteBuffer.get();
var b0 = this.byteBuffer.get();
return ((code - 0xd4) << 16) + (b1 << 8) + b0;
}
if (code === 0x49) {
return this.byteBuffer.getInt();
val = ((code - 0xd4) << 16) + (b1 << 8) + b0;
} else if (code === 0x49) {
val = this.byteBuffer.getInt();
} else {
this.throwError('readInt', code);
}

this.throwError('readInt', code);
return this.handleType('int', val, withType);
};

utils.addByteCodes(BYTE_CODES, [
Expand Down Expand Up @@ -170,35 +170,34 @@ utils.addByteCodes(BYTE_CODES, [
* @return {Number}
* @api public
*/
proto.readLong = function () {
proto.readLong = function (withType) {
var code = this.byteBuffer.get();
var val;
// 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) {
val = 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) {
val = ((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;
}
if (code === 0x59) {
val = ((code - 0x3c) << 16) + (b1 << 8) + b0;
} else if (code === 0x59) {
// 32-bit integer cast to long
return this.byteBuffer.getInt32();
}
if (code === 0x4c) {
return utils.handleLong(this.byteBuffer.getLong());
val = this.byteBuffer.getInt32();
} else if (code === 0x4c) {
val = utils.handleLong(this.byteBuffer.getLong());
} else {
this.throwError('readLong', code);
}

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

utils.addByteCodes(BYTE_CODES, [
Expand Down Expand Up @@ -247,29 +246,27 @@ utils.addByteCodes(BYTE_CODES, [
* @return {Number}
* @api public
*/
proto.readDouble = function () {
proto.readDouble = function (withType) {
var code = this.byteBuffer.get();
var val;
if (code === 0x44) {
return this.byteBuffer.getDouble();
val = this.byteBuffer.getDouble();
} else if (code === 0x5b) {
// Compact double
val = 0.0;
} else if (code === 0x5c) {
val = 1.0;
} else if (code === 0x5d) {
val = this.byteBuffer.getInt8();
} else if (code === 0x5e) {
val = this.byteBuffer.getInt16();
} else if (code === 0x5f) {
val = this.byteBuffer.getInt32() * 0.001;
} else {
this.throwError('readDouble', code);
}

// Compact double
if (code === 0x5b) {
return 0.0;
}
if (code === 0x5c) {
return 1.0;
}
if (code === 0x5d) {
return this.byteBuffer.getInt8();
}
if (code === 0x5e) {
return this.byteBuffer.getInt16();
}
if (code === 0x5f) {
return this.byteBuffer.getInt32() * 0.001;
}
this.throwError('readDouble', code);
return this.handleType('double', val, withType);
};

utils.addByteCodes(BYTE_CODES, [
Expand All @@ -295,16 +292,18 @@ utils.addByteCodes(BYTE_CODES, [
* @return {Date}
* @api public
*/
proto.readDate = function () {
proto.readDate = function (withType) {
var code = this.byteBuffer.get();
var val;
if (code === 0x4a) {
return new Date(utils.handleLong(this.byteBuffer.getLong()));
}
if (code === 0x4b) {
return new Date(this.byteBuffer.getUInt32() * 60000);
val = new Date(utils.handleLong(this.byteBuffer.getLong()));
} else if (code === 0x4b) {
val = new Date(this.byteBuffer.getUInt32() * 60000);
} else {
this.throwError('readDate', code);
}

this.throwError('readDate', code);
return this.handleType('date', val, withType);
};

utils.addByteCodes(BYTE_CODES, [
Expand Down Expand Up @@ -407,7 +406,7 @@ utils.addByteCodes(BYTE_CODES, [
* @return {String}
* @api public
*/
proto.readString = function () {
proto.readString = function (withType) {
var str = '';
var code = this.byteBuffer.get();
debug('readString() code: %s', code);
Expand Down Expand Up @@ -457,7 +456,8 @@ proto.readString = function () {
this.throwError('readString', code);
break;
}
return str;

return this.handleType('string', str, withType);
};

utils.addByteCodes(BYTE_CODES, [
Expand Down
18 changes: 18 additions & 0 deletions test/date.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,20 @@ describe('date.test.js', function () {
d.getTime().should.equal(894621091000);
d.toUTCString().should.equal('Fri, 08 May 1998 09:51:31 GMT');
d.toISOString().should.equal('1998-05-08T09:51:31.000Z');
hessian.decode(dateBuffer, true).should.eql({
$class: 'java.util.Date',
$: new Date(894621091000)
});
});

it('should write date 2:51:31 May 8, 1998', function () {
hessian.encode(new Date(894621091000)).should.eql(dateBuffer);
hessian.encode({$class: 'java.util.Date', $:new Date(894621091000)}).should.eql(dateBuffer);
});

it('should write date 0 and read', function () {
hessian.encode(new Date(0)).should.eql(new Buffer(['d'.charCodeAt(0), 0, 0, 0, 0, 0, 0, 0, 0]));
hessian.encode({$class: 'java.util.Date', $: new Date(0)}).should.eql(new Buffer(['d'.charCodeAt(0), 0, 0, 0, 0, 0, 0, 0, 0]));
});

it('should read date 09:51:31 May 8, 1998 UTC', function () {
Expand All @@ -45,6 +51,8 @@ describe('date.test.js', function () {
d.getTime().should.equal(894621091000);
d.toUTCString().should.equal('Fri, 08 May 1998 09:51:31 GMT');
d.toISOString().should.equal('1998-05-08T09:51:31.000Z');

hessian.decode(utils.bytes('v1/date/894621091000'), '1.0', true).should.eql({$class: 'java.util.Date', $: new Date(894621091000)});
});

it('should read date 09:51:00 May 8, 1998 UTC', function () {
Expand All @@ -54,13 +62,17 @@ describe('date.test.js', function () {
d.getTime().should.equal(894621060000);
d.toUTCString().should.equal('Fri, 08 May 1998 09:51:00 GMT');
d.toISOString().should.equal('1998-05-08T09:51:00.000Z');

hessian.decode(utils.bytes('v1/date/894621060000'), '1.0', true).should.eql({$class: 'java.util.Date', $: new Date(894621060000)});
});

it('should write date', function () {
var now = new Date(1398280514000);
hessian.encode(now, '1.0').should.eql(utils.bytes('v1/date/now'));
hessian.encode({$class: 'java.util.Date', $: now}, '1.0').should.eql(utils.bytes('v1/date/now'));
// read it
hessian.decode(utils.bytes('v1/date/now'), '1.0').should.eql(now);
hessian.decode(utils.bytes('v1/date/now'), '1.0', true).should.eql({$class: 'java.util.Date', $: now});
});

describe('hessian 2.0', function () {
Expand All @@ -71,6 +83,8 @@ describe('date.test.js', function () {
d.getTime().should.equal(894621091000);
d.toUTCString().should.equal('Fri, 08 May 1998 09:51:31 GMT');
d.toISOString().should.equal('1998-05-08T09:51:31.000Z');

hessian.decode(utils.bytes('v2/date/894621091000'), '2.0', true).should.eql({$class: 'java.util.Date', $: new Date(894621091000)});
});

it('should read Compact: date in minutes, 09:51:00 May 8, 1998 UTC', function () {
Expand All @@ -80,13 +94,17 @@ describe('date.test.js', function () {
d.getTime().should.equal(894621060000);
d.toUTCString().should.equal('Fri, 08 May 1998 09:51:00 GMT');
d.toISOString().should.equal('1998-05-08T09:51:00.000Z');

hessian.decode(utils.bytes('v2/date/894621060000'), '2.0', true).should.eql({$class: 'java.util.Date', $: new Date(894621060000)});
});

it('should write and read date', function () {
var now = new Date(1398280514000);
hessian.encode(now, '2.0').should.eql(utils.bytes('v2/date/now'));
hessian.encode({$class: 'java.util.Date', $: now}, '2.0').should.eql(utils.bytes('v2/date/now'));
// read it
hessian.decode(utils.bytes('v2/date/now'), '2.0').should.eql(now);
hessian.decode(utils.bytes('v2/date/now'), '2.0', true).should.eql({$class: 'java.util.Date', $: now});
});

// it('should read 1.0 format', function () {
Expand Down
10 changes: 10 additions & 0 deletions test/double.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,17 @@ describe('double.test.js', function () {
describe('v2.0', function () {
it('should read 0.0 and 1.0', function () {
hessian.decode(new Buffer([0x5b]), '2.0').should.equal(0.0);
hessian.decode(new Buffer([0x5b]), '2.0', true).should.eql({$class: 'double', $: 0.0});
hessian.decode(new Buffer([0x5c]), '2.0').should.equal(1.0);
hessian.decode(new Buffer([0x5c]), '2.0', true).should.eql({$class: 'double', $: 1.0});
});

it('should read 8 bits double', function () {
hessian.decode(new Buffer([0x5d, 0x00]), '2.0').should.equal(0.0);
hessian.decode(new Buffer([0x5d, 0x01]), '2.0').should.equal(1.0);
hessian.decode(new Buffer([0x5d, 0x80]), '2.0').should.equal(-128.0);
hessian.decode(new Buffer([0x5d, 0x7f]), '2.0').should.equal(127.0);
hessian.decode(new Buffer([0x5d, 0x7f]), '2.0', true).should.eql({$class: 'double', $: 127.0});
});

it('should read 16 bits double', function () {
Expand All @@ -121,16 +124,20 @@ describe('double.test.js', function () {
hessian.decode(new Buffer([0x5e, 0x00, 0x80]), '2.0').should.equal(128.0);
hessian.decode(new Buffer([0x5e, 0x00, 0x7f]), '2.0').should.equal(127.0);
hessian.decode(new Buffer([0x5e, 0x80, 0x00]), '2.0').should.equal(-32768.0);
hessian.decode(new Buffer([0x5e, 0x80, 0x00]), '2.0', true).should.eql({$class: 'double', $: -32768.0});
hessian.decode(new Buffer([0x5e, 0x7f, 0xff]), '2.0').should.equal(32767.0);
});

it('should read 32 bits float double', function () {
hessian.decode(new Buffer([0x5f, 0x00, 0x00, 0x00, 0x00]), '2.0').should.equal(0.0);
hessian.decode(new Buffer([0x5f, 0x00, 0x00, 0x00, 0x00]), '2.0', true).should.eql({$class: 'double', $: 0.0});
hessian.decode(new Buffer([0x5f, 0x00, 0x00, 0x2f, 0xda]), '2.0').should.equal(12.25);
hessian.decode(new Buffer([0x5f, 0x00, 0x00, 0x2f, 0xda]), '2.0', true).should.eql({$class: 'double', $: 12.25});
});

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

it('should write 0.0 and 1.0', function () {
Expand Down Expand Up @@ -183,6 +190,7 @@ describe('double.test.js', function () {
hessian.decode(utils.bytes('v2/double/10.1'), '2.0').should.equal(10.1);
hessian.decode(utils.bytes('v2/double/-128'), '2.0').should.equal(-128);
hessian.decode(utils.bytes('v2/double/-127.9999'), '2.0').should.equal(-127.9999);
hessian.decode(utils.bytes('v2/double/-127.9999'), '2.0', true).should.eql({$class: 'double', $: -127.9999});
hessian.decode(utils.bytes('v2/double/127'), '2.0').should.equal(127);
hessian.decode(utils.bytes('v2/double/126.9989'), '2.0').should.equal(126.9989);
hessian.decode(utils.bytes('v2/double/-32768'), '2.0').should.equal(-32768);
Expand All @@ -203,6 +211,7 @@ describe('double.test.js', function () {
hessian.decode(utils.bytes('v2/double/2147483647'), '2.0').should.equal(2147483647);
hessian.decode(utils.bytes('v2/double/2147483646'), '2.0').should.equal(2147483646);
hessian.decode(utils.bytes('v2/double/2147483646.456'), '2.0').should.equal(2147483646.456);
hessian.decode(utils.bytes('v2/double/2147483646.456'), '2.0', true).should.eql({$class: 'double', $: 2147483646.456});
});

it('should read java hessian 1.0 bin format', function () {
Expand All @@ -229,6 +238,7 @@ describe('double.test.js', function () {
hessian.decode(utils.bytes('v1/double/2147483647'), '2.0').should.equal(2147483647);
hessian.decode(utils.bytes('v1/double/2147483646'), '2.0').should.equal(2147483646);
hessian.decode(utils.bytes('v1/double/2147483646.456'), '2.0').should.equal(2147483646.456);
hessian.decode(utils.bytes('v1/double/2147483646.456'), '2.0', true).should.eql({$class: 'double', $: 2147483646.456});
});
});
});
Loading