Skip to content

Commit 77f4fe4

Browse files
author
余化
committed
decode long with type
1 parent eae6bd1 commit 77f4fe4

File tree

3 files changed

+55
-17
lines changed

3 files changed

+55
-17
lines changed

lib/v2/decoder.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -170,35 +170,34 @@ utils.addByteCodes(BYTE_CODES, [
170170
* @return {Number}
171171
* @api public
172172
*/
173-
proto.readLong = function () {
173+
proto.readLong = function (withType) {
174174
var code = this.byteBuffer.get();
175+
var val;
175176
// Compact long
176177
if (code >= 0xd8 && code <= 0xef) {
177178
// Longs between -8 and 15 are represented by a single octet in the range xd8 to xef.
178179
// value = (code - 0xe0)
179-
return code - 0xe0;
180-
}
181-
if (code >= 0xf0 && code <= 0xff) {
180+
val = code - 0xe0;
181+
} else if (code >= 0xf0 && code <= 0xff) {
182182
// Longs between -2048 and 2047 are encoded in two octets with the leading byte in the range xf0 to xff.
183183
// value = ((code - 0xf8) << 8) + b0
184-
return ((code - 0xf8) << 8) + this.byteBuffer.get();
185-
}
186-
if (code >= 0x38 && code <= 0x3f) {
184+
val = ((code - 0xf8) << 8) + this.byteBuffer.get();
185+
} else if (code >= 0x38 && code <= 0x3f) {
187186
// Longs between -262144 and 262143 are encoded in three octets with the leading byte in the range x38 to x3f.
188187
// value = ((code - 0x3c) << 16) + (b1 << 8) + b0
189188
var b1 = this.byteBuffer.get();
190189
var b0 = this.byteBuffer.get();
191-
return ((code - 0x3c) << 16) + (b1 << 8) + b0;
192-
}
193-
if (code === 0x59) {
190+
val = ((code - 0x3c) << 16) + (b1 << 8) + b0;
191+
} else if (code === 0x59) {
194192
// 32-bit integer cast to long
195-
return this.byteBuffer.getInt32();
196-
}
197-
if (code === 0x4c) {
198-
return utils.handleLong(this.byteBuffer.getLong());
193+
val = this.byteBuffer.getInt32();
194+
} else if (code === 0x4c) {
195+
val = utils.handleLong(this.byteBuffer.getLong());
196+
} else {
197+
this.throwError('readLong', code);
199198
}
200199

201-
this.throwError('readLong', code);
200+
return this.handleType('long', val, withType)
202201
};
203202

204203
utils.addByteCodes(BYTE_CODES, [

test/long.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ describe('long.test.js', function () {
3232

3333
it('should read long 300', function () {
3434
hessian.decode(longBuffer).should.equal(300);
35+
hessian.decode(longBuffer, true).should.eql({$class: 'long', $: 300});
3536
});
3637

3738
it('should write long 300', function () {
@@ -53,14 +54,19 @@ describe('long.test.js', function () {
5354
it('should write and read equal java impl', function () {
5455
hessian.encode(java.long(0), '1.0').should.eql(utils.bytes('v1/long/0'));
5556
hessian.decode(utils.bytes('v1/long/0')).should.equal(0);
57+
hessian.decode(utils.bytes('v1/long/0'), true).should.eql({$class: 'long', $: 0});
5658
hessian.encode(java.long(-8), '1.0').should.eql(utils.bytes('v1/long/-8'));
5759
hessian.decode(utils.bytes('v1/long/-8')).should.equal(-8);
60+
hessian.decode(utils.bytes('v1/long/-8'), true).should.eql({$class: 'long', $: -8});
5861
hessian.encode(java.long(-7), '1.0').should.eql(utils.bytes('v1/long/-7'));
5962
hessian.decode(utils.bytes('v1/long/-7')).should.equal(-7);
63+
hessian.decode(utils.bytes('v1/long/-7'), true).should.eql({$class: 'long', $: -7});
6064
hessian.encode(java.long(15), '1.0').should.eql(utils.bytes('v1/long/15'));
6165
hessian.decode(utils.bytes('v1/long/15')).should.equal(15);
66+
hessian.decode(utils.bytes('v1/long/15'), true).should.eql({$class: 'long', $: 15});
6267
hessian.encode(java.long(14), '1.0').should.eql(utils.bytes('v1/long/14'));
6368
hessian.decode(utils.bytes('v1/long/14')).should.equal(14);
69+
hessian.decode(utils.bytes('v1/long/14'), true).should.eql({$class: 'long', $: 14});
6470
hessian.encode(java.long(-9), '1.0').should.eql(utils.bytes('v1/long/-9'));
6571
hessian.decode(utils.bytes('v1/long/-9')).should.equal(-9);
6672
hessian.encode(java.long(16), '1.0').should.eql(utils.bytes('v1/long/16'));
@@ -81,6 +87,7 @@ describe('long.test.js', function () {
8187
hessian.decode(utils.bytes('v1/long/-2049')).should.equal(-2049);
8288
hessian.encode(java.long(-2147483648), '1.0').should.eql(utils.bytes('v1/long/-2147483648'));
8389
hessian.decode(utils.bytes('v1/long/-2147483648')).should.equal(-2147483648);
90+
hessian.decode(utils.bytes('v1/long/-2147483648'), true).should.eql({$class: 'long', $: -2147483648});
8491
hessian.encode(java.long(-2147483647), '1.0').should.eql(utils.bytes('v1/long/-2147483647'));
8592
hessian.decode(utils.bytes('v1/long/-2147483647')).should.equal(-2147483647);
8693
hessian.encode(java.long(2147483647), '1.0').should.eql(utils.bytes('v1/long/2147483647'));
@@ -89,36 +96,43 @@ describe('long.test.js', function () {
8996
hessian.decode(utils.bytes('v1/long/2147483646')).should.equal(2147483646);
9097
hessian.encode(java.long(2147483648), '1.0').should.eql(utils.bytes('v1/long/2147483648'));
9198
hessian.decode(utils.bytes('v1/long/2147483648')).should.equal(2147483648);
99+
hessian.decode(utils.bytes('v1/long/2147483648'), true).should.eql({$class: 'long', $: 2147483648});
92100
});
93101

94102
describe('v2.0', function () {
95103
it('should read compact long', function () {
96104
hessian.decode(new Buffer([0xe0]), '2.0').should.equal(0);
97105
hessian.decode(new Buffer([0xd8]), '2.0').should.equal(-8);
98106
hessian.decode(new Buffer([0xef]), '2.0').should.equal(15);
107+
hessian.decode(new Buffer([0xef]), '2.0', true).should.eql({$class: 'long', $: 15});
99108

100109
hessian.decode(new Buffer([0xf8, 0x00]), '2.0').should.equal(0);
101110
hessian.decode(new Buffer([0xf0, 0x00]), '2.0').should.equal(-2048);
111+
hessian.decode(new Buffer([0xf0, 0x00]), '2.0', true).should.eql({$class: 'long', $: -2048});
102112
hessian.decode(new Buffer([0xf7, 0x00]), '2.0').should.equal(-256);
103113
hessian.decode(new Buffer([0xff, 0xff]), '2.0').should.equal(2047);
104114

105115
hessian.decode(new Buffer([0x3c, 0x00, 0x00]), '2.0').should.equal(0);
106116
hessian.decode(new Buffer([0x38, 0x00, 0x00]), '2.0').should.equal(-262144);
107117
hessian.decode(new Buffer([0x3f, 0xff, 0xff]), '2.0').should.equal(262143);
118+
hessian.decode(new Buffer([0x3f, 0xff, 0xff]), '2.0', true).should.eql({$class: 'long', $: 262143});
108119

109120
// four octet longs
110121
hessian.decode(new Buffer([0x59, 0x00, 0x00, 0x00, 0x00]), '2.0').should.equal(0);
111122
hessian.decode(new Buffer([0x59, 0x00, 0x00, 0x01, 0x2c]), '2.0').should.equal(300);
112123
hessian.decode(new Buffer([0x59, 0x7f, 0xff, 0xff, 0xff]), '2.0').should.equal(2147483647);
113124
hessian.decode(new Buffer([0x59, 0x80, 0x00, 0x00, 0x00]), '2.0').should.equal(-2147483648);
125+
hessian.decode(new Buffer([0x59, 0x80, 0x00, 0x00, 0x00]), '2.0', true).should.eql({$class: 'long', $: -2147483648});
114126

115127
hessian.decode(new Buffer([0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), '2.0').should.equal(0);
116128
hessian.decode(new Buffer([0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x2c]), '2.0').should.equal(300);
117129
hessian.decode(new Buffer([0x4c, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff]), '2.0').should.equal(2147483647);
130+
hessian.decode(new Buffer([0x4c, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff]), '2.0', true).should.eql({$class: 'long', $: 2147483647});
118131
});
119132

120133
it('should read normal long', function () {
121134
hessian.decode(new Buffer([0x4c, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00]), '2.0').should.equal(2147483648);
135+
hessian.decode(new Buffer([0x4c, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00]), '2.0', true).should.eql({$class: 'long', $: 2147483648});
122136
});
123137

124138
it('should write compact long', function () {
@@ -353,45 +367,64 @@ describe('long.test.js', function () {
353367
it('should write and read equal java impl', function () {
354368
hessian.encode(java.long(0), '2.0').should.eql(utils.bytes('v2/long/0'));
355369
hessian.decode(utils.bytes('v2/long/0'), '2.0').should.equal(0);
370+
hessian.decode(utils.bytes('v2/long/0'), '2.0', true).should.eql({ $: 0, $class: 'long' });
356371
hessian.encode(java.long(-8), '2.0').should.eql(utils.bytes('v2/long/-8'));
357372
hessian.decode(utils.bytes('v2/long/-8'), '2.0').should.equal(-8);
373+
hessian.decode(utils.bytes('v2/long/-8'), '2.0', true).should.eql({ $: -8, $class: 'long' });
358374
hessian.encode(java.long(-7), '2.0').should.eql(utils.bytes('v2/long/-7'));
359375
hessian.decode(utils.bytes('v2/long/-7'), '2.0').should.equal(-7);
376+
hessian.decode(utils.bytes('v2/long/-7'), '2.0', true).should.eql({ $: -7, $class: 'long' });
360377
hessian.encode(java.long(15), '2.0').should.eql(utils.bytes('v2/long/15'));
361378
hessian.decode(utils.bytes('v2/long/15'), '2.0').should.equal(15);
379+
hessian.decode(utils.bytes('v2/long/15'), '2.0', true).should.eql({ $: 15, $class: 'long' });
362380
hessian.encode(java.long(14), '2.0').should.eql(utils.bytes('v2/long/14'));
363381
hessian.decode(utils.bytes('v2/long/14'), '2.0').should.equal(14);
382+
hessian.decode(utils.bytes('v2/long/14'), '2.0', true).should.eql({ $: 14, $class: 'long' });
364383
hessian.encode(java.long(-9), '2.0').should.eql(utils.bytes('v2/long/-9'));
365384
hessian.decode(utils.bytes('v2/long/-9'), '2.0').should.equal(-9);
385+
hessian.decode(utils.bytes('v2/long/-9'), '2.0', true).should.eql({ $: -9, $class: 'long' });
366386
hessian.encode(java.long(16), '2.0').should.eql(utils.bytes('v2/long/16'));
367387
hessian.decode(utils.bytes('v2/long/16'), '2.0').should.equal(16);
388+
hessian.decode(utils.bytes('v2/long/16'), '2.0', true).should.eql({ $: 16, $class: 'long' });
368389
hessian.encode(java.long(255), '2.0').should.eql(utils.bytes('v2/long/255'));
369390
hessian.encode(java.long(Long.fromNumber(255)), '2.0').should.eql(utils.bytes('v2/long/255'));
370391
hessian.encode(Long.fromNumber(255), '2.0').should.eql(utils.bytes('v2/long/255'));
371392

372393
hessian.decode(utils.bytes('v2/long/255'), '2.0').should.equal(255);
394+
hessian.decode(utils.bytes('v2/long/255'), '2.0', true).should.eql({ $: 255, $class: 'long' });
373395
hessian.encode(java.long(-2048), '2.0').should.eql(utils.bytes('v2/long/-2048'));
374396
hessian.decode(utils.bytes('v2/long/-2048'), '2.0').should.equal(-2048);
397+
hessian.decode(utils.bytes('v2/long/-2048'), '2.0', true).should.eql({ $: -2048, $class: 'long' });
375398
hessian.encode(java.long(2047), '2.0').should.eql(utils.bytes('v2/long/2047'));
376399
hessian.decode(utils.bytes('v2/long/2047'), '2.0').should.equal(2047);
400+
hessian.decode(utils.bytes('v2/long/2047'), '2.0', true).should.eql({ $: 2047, $class: 'long' });
377401
hessian.encode(java.long(262143), '2.0').should.eql(utils.bytes('v2/long/262143'));
378402
hessian.decode(utils.bytes('v2/long/262143'), '2.0').should.equal(262143);
403+
hessian.decode(utils.bytes('v2/long/262143'), '2.0', true).should.eql({ $: 262143, $class: 'long' });
379404
hessian.encode(java.long(-262144), '2.0').should.eql(utils.bytes('v2/long/-262144'));
380405
hessian.decode(utils.bytes('v2/long/-262144'), '2.0').should.equal(-262144);
406+
hessian.decode(utils.bytes('v2/long/-262144'), '2.0', true).should.eql({ $: -262144, $class: 'long' });
381407
hessian.encode(java.long(2048), '2.0').should.eql(utils.bytes('v2/long/2048'));
382408
hessian.decode(utils.bytes('v2/long/2048'), '2.0').should.equal(2048);
409+
hessian.decode(utils.bytes('v2/long/2048'), '2.0', true).should.eql({ $: 2048, $class: 'long' });
383410
hessian.encode(java.long(-2049), '2.0').should.eql(utils.bytes('v2/long/-2049'));
384411
hessian.decode(utils.bytes('v2/long/-2049'), '2.0').should.equal(-2049);
412+
hessian.decode(utils.bytes('v2/long/-2049'), '2.0', true).should.eql({ $: -2049, $class: 'long' });
385413
hessian.encode(java.long(-2147483648), '2.0').should.eql(utils.bytes('v2/long/-2147483648'));
386414
hessian.decode(utils.bytes('v2/long/-2147483648'), '2.0').should.equal(-2147483648);
415+
hessian.decode(utils.bytes('v2/long/-2147483648'), '2.0', true).should.eql({ $: -2147483648, $class: 'long' });
387416
hessian.encode(java.long(-2147483647), '2.0').should.eql(utils.bytes('v2/long/-2147483647'));
388417
hessian.decode(utils.bytes('v2/long/-2147483647'), '2.0').should.equal(-2147483647);
418+
hessian.decode(utils.bytes('v2/long/-2147483647'), '2.0', true).should.eql({ $: -2147483647, $class: 'long' });
389419
hessian.encode(java.long(2147483647), '2.0').should.eql(utils.bytes('v2/long/2147483647'));
390420
hessian.decode(utils.bytes('v2/long/2147483647'), '2.0').should.equal(2147483647);
421+
hessian.decode(utils.bytes('v2/long/2147483647'), '2.0', true).should.eql({ $: 2147483647, $class: 'long' });
391422
hessian.encode(java.long(2147483646), '2.0').should.eql(utils.bytes('v2/long/2147483646'));
392423
hessian.decode(utils.bytes('v2/long/2147483646'), '2.0').should.equal(2147483646);
424+
hessian.decode(utils.bytes('v2/long/2147483646'), '2.0', true).should.eql({ $: 2147483646, $class: 'long' });
393425
hessian.encode(java.long(2147483648), '2.0').should.eql(utils.bytes('v2/long/2147483648'));
394426
hessian.decode(utils.bytes('v2/long/2147483648'), '2.0').should.equal(2147483648);
427+
hessian.decode(utils.bytes('v2/long/2147483648'), '2.0', true).should.eql({ $: 2147483648, $class: 'long' });
395428
});
396429

397430
it('should read 1.0 bin as well', function () {

test/object.test.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,10 @@ describe('object.test.js', function () {
813813
a0.should.eql({
814814
$class: 'java.util.concurrent.atomic.AtomicLong',
815815
$: {
816-
value: 0
816+
value: {
817+
$class: 'long',
818+
$: 0
819+
}
817820
}
818821
});
819822
hessian.encode({
@@ -833,7 +836,10 @@ describe('object.test.js', function () {
833836
a1.should.eql({
834837
$class: 'java.util.concurrent.atomic.AtomicLong',
835838
$: {
836-
value: 1
839+
value: {
840+
$class: 'long',
841+
$: 1
842+
}
837843
}
838844
});
839845
hessian.encode({

0 commit comments

Comments
 (0)