Skip to content

Commit 8f982a9

Browse files
author
宗羽
committed
fix(writeLong) hessian2.0 write long bug
- writeLong method to support string val
1 parent be3ad2d commit 8f982a9

3 files changed

Lines changed: 125 additions & 1 deletion

File tree

lib/v2/encoder.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var is = require('is-type-of');
1919
var util = require('util');
2020
var EncoderV1 = require('../v1/encoder');
2121
var javaObject = require('../object');
22+
var utility = require('utility');
2223

2324
var SUPPORT_ES6_MAP = typeof Map === 'function' && typeof Map.prototype.forEach === 'function';
2425

@@ -74,6 +75,7 @@ var INT_SHORT_ZERO = 0xd4; // 212
7475
* ```
7576
*/
7677
proto.writeInt = function (val) {
78+
this._assertType('writeInt', 'int32', val);
7779
if (INT_DIRECT_MIN <= val && val <= INT_DIRECT_MAX) {
7880
this.byteBuffer.put(val + INT_ZERO);
7981
} else if (INT_BYTE_MIN <= val && val <= INT_BYTE_MAX) {
@@ -140,6 +142,10 @@ proto.writeInt = function (val) {
140142
* ```
141143
*/
142144
proto.writeLong = function (val) {
145+
if (typeof val !== 'number' && utility.isSafeNumberString(val)) {
146+
val = Number(val);
147+
}
148+
143149
if (val >= -8 && val <= 15) {
144150
this.byteBuffer.put(val + 0xe0);
145151
} else if (val >= -2048 && val <= 2047) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"dependencies": {
3232
"byte": "~1.1.1",
3333
"debug": "~2.1.3",
34-
"is-type-of": "~0.3.1"
34+
"is-type-of": "~0.3.1",
35+
"utility": "~1.4.0"
3536
},
3637
"devDependencies": {
3738
"autod": "*",

test/long.test.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,123 @@ describe('long.test.js', function () {
233233
buf.should.eql(new Buffer([0x4c, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00]));
234234
});
235235

236+
it('should write { $class: "long": $: "..." } ok', function () {
237+
// -8 ~ 15
238+
var buf = hessian.encode(java.long('0'), '2.0');
239+
buf.should.length(1);
240+
buf[0].should.equal(0xe0);
241+
buf.should.eql(new Buffer([0xe0]));
242+
243+
buf = hessian.encode(java.long('-8'), '2.0');
244+
buf.should.length(1);
245+
buf[0].should.equal(0xd8);
246+
buf.should.eql(new Buffer([0xd8]));
247+
248+
buf = hessian.encode(java.long('-7'), '2.0');
249+
buf.should.length(1);
250+
buf[0].should.equal(0xd9);
251+
buf.should.eql(new Buffer([0xd9]));
252+
253+
buf = hessian.encode(java.long('15'), '2.0');
254+
buf.should.length(1);
255+
buf[0].should.equal(0xef);
256+
buf.should.eql(new Buffer([0xef]));
257+
258+
buf = hessian.encode(java.long('14'), '2.0');
259+
buf.should.length(1);
260+
buf[0].should.equal(0xee);
261+
buf.should.eql(new Buffer([0xee]));
262+
263+
// -2048 ~ 2047
264+
buf = hessian.encode(java.long('-9'), '2.0');
265+
buf.should.length(2);
266+
buf[0].should.equal(0xf7);
267+
buf[1].should.equal(0xf7);
268+
buf.should.eql(new Buffer([0xf7, 0xf7]));
269+
270+
buf = hessian.encode(java.long('16'), '2.0');
271+
buf.should.length(2);
272+
buf[0].should.equal(0xf8);
273+
buf[1].should.equal(0x10);
274+
buf.should.eql(new Buffer([0xf8, 0x10]));
275+
276+
buf = hessian.encode(java.long('255'), '2.0');
277+
buf.should.length(2);
278+
buf[0].should.equal(0xf8);
279+
buf[1].should.equal(0xff);
280+
buf.should.eql(new Buffer([0xf8, 0xff]));
281+
282+
buf = hessian.encode(java.long('-2048'), '2.0');
283+
buf.should.length(2);
284+
buf[0].should.equal(0xf0);
285+
buf[1].should.equal(0);
286+
buf.should.eql(new Buffer([0xf0, 0x00]));
287+
288+
buf = hessian.encode(java.long('2047'), '2.0');
289+
buf.should.length(2);
290+
buf[0].should.equal(0xff);
291+
buf[1].should.equal(0xff);
292+
buf.should.eql(new Buffer([0xff, 0xff]));
293+
294+
// -262144 ~ 262143
295+
buf = hessian.encode(java.long('262143'), '2.0');
296+
buf.should.length(3);
297+
buf[0].should.equal(0x3f);
298+
buf[1].should.equal(0xff);
299+
buf[2].should.equal(0xff);
300+
buf.should.eql(new Buffer([0x3f, 0xff, 0xff]));
301+
302+
buf = hessian.encode(java.long('-262144'), '2.0');
303+
buf.should.length(3);
304+
buf[0].should.equal(0x38);
305+
buf[1].should.equal(0);
306+
buf[2].should.equal(0);
307+
buf.should.eql(new Buffer([0x38, 0x00, 0x00]));
308+
309+
buf = hessian.encode(java.long('2048'), '2.0');
310+
buf.should.length(3);
311+
buf[0].should.equal(0x3c);
312+
buf[1].should.equal(0x08);
313+
buf[2].should.equal(0x00);
314+
buf.should.eql(new Buffer([0x3c, 0x08, 0x00]));
315+
316+
buf = hessian.encode(java.long('-2049'), '2.0');
317+
buf.should.length(3);
318+
buf[0].should.equal(0x3b);
319+
buf[1].should.equal(0xf7);
320+
buf[2].should.equal(0xff);
321+
buf.should.eql(new Buffer([0x3b, 0xf7, 0xff]));
322+
323+
// -2147483648 ~ 2147483647
324+
buf = hessian.encode(java.long('-2147483648'), '2.0');
325+
buf.should.length(5);
326+
buf[0].should.equal(0x59);
327+
buf[1].should.equal(0x80);
328+
buf[2].should.equal(0x00);
329+
buf[3].should.equal(0x00);
330+
buf[4].should.equal(0x00);
331+
buf.should.eql(new Buffer([0x59, 0x80, 0x00, 0x00, 0x00]));
332+
333+
buf = hessian.encode(java.long('2147483647'), '2.0');
334+
buf.should.length(5);
335+
buf[0].should.equal(0x59);
336+
buf[1].should.equal(0x7f);
337+
buf[2].should.equal(0xff);
338+
buf[3].should.equal(0xff);
339+
buf[4].should.equal(0xff);
340+
buf.should.eql(new Buffer([0x59, 0x7f, 0xff, 0xff, 0xff]));
341+
342+
// L
343+
buf = hessian.encode(java.long('2147483648'), '2.0');
344+
buf.should.length(9);
345+
buf.should.eql(new Buffer([0x4c, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00]));
346+
347+
// unsafe long value
348+
buf = hessian.encode(java.long('9007199254740992'), '2.0');
349+
buf.should.length(9);
350+
buf.should.eql(new Buffer([0x4c, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]));
351+
});
352+
236353
it('should write and read equal java impl', function () {
237354
hessian.encode(java.long(0), '2.0').should.eql(utils.bytes('v2/long/0'));
238355
hessian.decode(utils.bytes('v2/long/0'), '2.0').should.equal(0);

0 commit comments

Comments
 (0)