Skip to content

Commit ce5a7f8

Browse files
authored
feat: support convert java.util.Locale to com.caucho.hessian.io.LocaleHandle (#99)
1 parent bc25600 commit ce5a7f8

5 files changed

Lines changed: 155 additions & 0 deletions

File tree

benchmark/string.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
'use strict';
2+
3+
const Benchmark = require('benchmark');
4+
const benchmarks = require('beautify-benchmark');
5+
const assert = require('assert');
6+
7+
const ByteBuffer = require('byte');
8+
const io = ByteBuffer.allocate(1024 * 1024);
9+
10+
const str = '1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234';
11+
io.putRawString(str);
12+
const buf = io.array();
13+
14+
// io.position(0);
15+
// console.log(io.getRawStringByStringLength(1024));
16+
// console.log(buf.toString());
17+
18+
io.position(0);
19+
assert(io.getRawStringByStringLength(1024) === buf.toString());
20+
21+
function getUTF(buf) {
22+
const data = [];
23+
const length = buf.length;
24+
for (let i = 0; i < length; i++) {
25+
const ch = buf[i];
26+
if (ch < 0x80) {
27+
data.push(ch);
28+
} else if ((ch & 0xe0) === 0xc0) {
29+
const ch1 = buf[++i];
30+
const v = ((ch & 0x1f) << 6) + (ch1 & 0x3f);
31+
data.push(v);
32+
} else if ((ch & 0xf0) === 0xe0) {
33+
const ch1 = buf[++i];
34+
const ch2 = buf[++i];
35+
const v = ((ch & 0x0f) << 12) + ((ch1 & 0x3f) << 6) + (ch2 & 0x3f);
36+
data.push(v);
37+
} else {
38+
throw new Error('string is not valid UTF-8 encode');
39+
}
40+
}
41+
return String.fromCharCode.apply(String, data);
42+
}
43+
44+
assert(getUTF(buf) === buf.toString());
45+
46+
47+
function getUTF2(buf) {
48+
const length = buf.length;
49+
const data = [];
50+
let start = 0;
51+
const numInts = length >> 2;
52+
for (let i = 0; i < numInts; i++) {
53+
const num = buf.readInt32BE(i * 4);
54+
if ((num & 0x80808080) !== 0) {
55+
throw new Error();
56+
}
57+
}
58+
const offset = start + length;
59+
return buf.toString('utf8', 0, offset);
60+
61+
// while (i < length) {
62+
// const num = buf.readInt32BE(i);
63+
// if ((num & 0x80808080) === 0) {
64+
// data.push(buf[i]);
65+
// data.push(buf[i + 1]);
66+
// data.push(buf[i + 2]);
67+
// data.push(buf[i + 3]);
68+
// i += 4;
69+
// } else {
70+
// assert(false);
71+
// }
72+
// }
73+
// return String.fromCharCode.apply(String, data);
74+
}
75+
76+
assert(getUTF2(buf) === buf.toString());
77+
io.position(0);
78+
assert(io.getRawStringFast(1024) === buf.toString());
79+
80+
const suite = new Benchmark.Suite();
81+
suite
82+
.add('io.getRawStringByStringLength', function() {
83+
io._offset = 0;
84+
io.getRawStringByStringLength(1024);
85+
})
86+
.add('io.getRawStringFast', function() {
87+
io._offset = 0;
88+
io.getRawStringFast(1024);
89+
})
90+
.add('buf.toString', function() {
91+
buf.toString();
92+
})
93+
.add('getUTF', function() {
94+
getUTF(buf);
95+
})
96+
.add('getUTF2', function() {
97+
getUTF2(buf);
98+
})
99+
.on('cycle', function(event) {
100+
benchmarks.add(event.target);
101+
})
102+
.on('start', function(event) {
103+
console.log('\n Cache Benchmark\n node version: %s, date: %s\n Starting...',
104+
process.version, Date());
105+
})
106+
.on('complete', function done() {
107+
benchmarks.log();
108+
})
109+
.run({ 'async': false });
110+
111+
// Cache Benchmark
112+
// node version: v8.9.0, date: Thu Nov 16 2017 13:26:18 GMT+0800 (CST)
113+
// Starting...
114+
// 2 tests completed.
115+
//
116+
// with cache x 21,724 ops/sec ±2.01% (82 runs sampled)
117+
// without cache x 8,523 ops/sec ±1.17% (89 runs sampled)

lib/convert/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = {
4+
'java.util.Locale': require('./java.util.locale'),
5+
};

lib/convert/java.util.locale.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
module.exports = function(obj) {
4+
return {
5+
$class: 'com.caucho.hessian.io.LocaleHandle',
6+
$: { value: obj.$ },
7+
};
8+
};

lib/v1/encoder.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var debug = require('debug')('hessian:v1:encoder');
1515
var utils = require('../utils');
1616
var javaObject = require('../object');
1717
var is = require('is-type-of');
18+
var converts = require('../convert');
1819
var supportES6Map = require('../utils').supportES6Map;
1920

2021
function Encoder(options) {
@@ -336,6 +337,8 @@ proto.writeObject = function (obj) {
336337
}
337338

338339
debug('writeObject with complex object, className: %s', className);
340+
var convertor = converts[obj.$class];
341+
obj = convertor ? convertor(obj) : obj;
339342
return this._writeObject(obj);
340343
};
341344

test/convert.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
var assert = require('assert');
4+
var hessian = require('../');
5+
var java = require('js-to-java');
6+
7+
describe('test/convert.test.js', function() {
8+
[
9+
'1.0', '2.0'
10+
].forEach(function(version) {
11+
describe(version, function() {
12+
it('should convert java.util.Locale to com.caucho.hessian.io.LocaleHandle', function() {
13+
var buf1 = hessian.encode(java.Locale('zh_CN'), version);
14+
var buf2 = hessian.encode({
15+
$class: 'java.util.Locale',
16+
$: 'zh_CN',
17+
}, version);
18+
assert.deepEqual(buf1, buf2);
19+
});
20+
});
21+
});
22+
});

0 commit comments

Comments
 (0)