java.io
java.io copied to clipboard
"java.sql.Timestamp" dose not be added in or not implement readObject()
Uncaught Error: Class "java.sql.Timestamp" dose not be added in or not implement readObject()
Custom class should add by yourself. see https://github.com/node-modules/java.io#4-addobject
see unittest for a demo https://github.com/node-modules/java.io/blob/5f130aef94f05c19684630fa5809a09cb9a581bd/test/addobjects.js#L7
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Date.java#Date.writeObject%28java.io.ObjectOutputStream%29
impl writeObject
and readObject
https://github.com/node-modules/java.io/blob/master/lib/objects/date.js#L5
I decode the timestamp class, the binary is not end with 0x78
// if(!(tag == cons.TC_OBJECT)){
// assert.equal(tag, cons.TC_ENDBLOCKDATA,
// 'SC_WRITE_METHOD object should end with TC_ENDBLOCKDATA');
// }
this assert alway wrong, therefore i fork it and comment it.
Not sure adding Custom class can handle it
@Tsuki I try to handle readObject and use your raw.bin
const io = require('java.io');
io.addObject('java.sql.Timestamp', {
readObject(io, obj) {
console.log('>> java.sql.Timestamp readObject');
io.readBlockHeader();
var fastTime = io.readLong().toNumber();
console.log('<< readObject | fastTime = %s', fastTime);
obj._$ = new Date(fastTime);
return obj;
},
writeObject(io, obj) {
io.writeBlockHeader(8);
io.writeLong(obj._$.getTime());
},
});
var ret = io.ObjectInputStream.read(fs.readFileSync('raw.bin'), true);
console.log(ret);
{
'$': {},
'$class':
{ name: 'java.sql.Timestamp',
serialVersionUID: '2745179027874758501',
flags: 2,
fields: [ { type: 'I', name: 'nanos' } ],
superClass:
{ name: 'java.util.Date',
serialVersionUID: '7523967970034938905',
flags: 3,
fields: [],
superClass: null } },
'_$': new Date('2017-02-03T09:33:02.000Z'),
}
It shoud be have nanos,
when you add io.defaultReadObject(obj);
you will get AssertionError: SC_WRITE_METHOD object should end with TC_ENDBLOCKDATA
{ '$': { nanos: 2015070607 },
'$class':
{ name: 'java.sql.Timestamp',
serialVersionUID: '2745179027874758501',
flags: 2,
fields: [ [Object] ],
superClass:
{ name: 'java.util.Date',
serialVersionUID: '7523967970034938905',
flags: 3,
fields: [],
superClass: null } },
'_$': 2017-02-03T09:44:05.000Z }
io.addObject('java.sql.Timestamp', {
readObject(io, obj) {
console.log('>> java.sql.Timestamp readObject');
io.readBlockHeader();
var fastTime = io.readLong().toNumber();
console.log('<< readObject | fastTime = %s', fastTime);
io.defaultReadObject(obj);
obj._$ = new Date(fastTime);
io.in.putInt8(120);
io.in._offset--;
return obj;
},
writeObject(io, obj) {
io.writeBlockHeader(8);
io.writeLong(obj._$.getTime());
},
});
this is my workaround
{ '$':
{ time:
{ '$': [Object],
'$class': [Object],
'_$': 2017-02-06T01:45:15.000Z },
time2:
{ '$': [Object],
'$class': [Object],
'_$': 2017-02-06T01:45:15.000Z },
time3:
{ '$': [Object],
'$class': [Object],
'_$': 2017-02-06T01:45:15.000Z } },
'$class':
{ name: 'JavaObjectSerialiser$Test',
serialVersionUID: '-7238052131403966026',
flags: 2,
fields: [ [Object], [Object], [Object] ],
superClass: null } }