java.io
A node implement of "java.io.InputObjectStream.readObject()" and "java.io.OutputObjectStream.writeObject()".
Protocol
- Object Serialization Stream Protocol (A protocal is not all, still need to refer to the implementation.)
- Object Serialization Stream Protocol Mind Node
Install
$ npm install java.io --save
Usage
1. readObject()
var fs = ;var io = ;var InputObjectStream = ioInputObjectStream;var OutputObjectStream = ioOutputObjectStream; // Read object and return whole infovar buf = fs;var in = buf true;var obj = in; // Read object but return value onlyvar buf2 = fs;var in2 = buf;var obj2 = in;
obj
should be:
'$' : value : 1024 '$class' : name : 'java.lang.Integer' serialVersionUID : '1360826667806852920' flags : 2 fields : type : 'I' name : 'value' superClass : name : 'java.lang.Number' serialVersionUID : '-8742448824652078965' flags : 2 fields : superClass : null
obj2
should be:
1024
if you only care about the first object from input stream, you could write the code briefly:
var buf3 = fs;var obj3 = InputObjectStream;
then obj3
should be:
'true': true 'false': false
2. writeObject(obj)
var outputObjectStream = ; // 1. Passed in argument must contains the whole info// 2. Every time calling the writeObject function// will return the buf had written invar buf = outputObjectStream;
A brief style is also OK:
OutputObjectStream;
3. OutputObjectStream.normalize(obj, type)
A convenient way to convert ordinary JavaScript object to object of standard format with whole info.
- params
- obj: accept all primitive value or primitive array and map
- type: string | boolean | int | short | long | char | byte | float | double |
- return: normalized object
var outputObjectStream = ;var normalizedObj = OutputObjectStream;var buf = outputObjectStream;
// quals to normalize(1, 'int')
4. addObject()
If a class has writeObject/readObject methods, you need to implement the corresponding methods, and add them via addObject() before read or write the object.
var io = ;io;
Builtin classes:
- java.util.ArrayList
- java.util.LinkedList
- java.util.HashMap
- java.util.HashSet
- java.util.TreeMap
- java.util.TreeSet
- java.util.Date
Data structure
// if a object has it's own readObject/writeObject method // save it's special value here '_$': ... // value of object '$': ... // class description '$class': name: 'className' serialVersionUID: 'SVUID' flags: flags fields: type: 'F' name: 'primitiveProperty' type: 'L' name: 'objProperty' classname: 'Ljava/lang/String;' superClass: parentClassDescriptionOrNull
Incompatible between 1.x and 2.x
- decode java
[B
tonew Buffer([1, 2, 3])
not[1, 2, 3]
#10