NuGet Package Manager

    java.io

    2.4.0 • Public • Published

    java.io

    NPM version build status codecov David

    A node implement of "java.io.InputObjectStream.readObject()" and "java.io.OutputObjectStream.writeObject()".

    Protocol

    Install

    $ npm install java.io --save

    Usage

    1. readObject()

    var fs = require('fs');
    var io = require('java.io');
    var InputObjectStream = io.InputObjectStream;
    var OutputObjectStream = io.OutputObjectStream;
     
    // Read object and return whole info
    var buf = fs.readFileSync('./test/fixtures/out/int/1024.bin');
    var in = new InputObjectStream(buf, true);
    var obj = in.readObject();
     
    // Read object but return value only
    var buf2 = fs.readFileSync('./test/fixtures/out/int/1024.bin');
    var in2 = new InputObjectStream(buf);
    var obj2 = in.readObject();

    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.readFileSync('./test/fixtures/out/map/boolean.bin');
    var obj3 = InputObjectStream.readObject(buf);

    then obj3 should be:

    { 'true': true, 'false': false }

    2. writeObject(obj)

    var outputObjectStream = new OutputObjectStream();
     
    // 1. Passed in argument must contains the whole info
    // 2. Every time calling the writeObject function
    //    will return the buf had written in
    var buf = outputObjectStream.writeObject(obj);

    A brief style is also OK:

    OutputObjectStream.writeObject(obj);

    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 = new OutputObjectStream();
    var normalizedObj = OutputObjectStream.normalize(true);
    var buf = outputObjectStream.writeObject(normalizedObj);
    normalize(null)
     
    normalize('string')
     
    normalize(true)
     
    normalize(1) // quals to normalize(1, 'int')
     
    normalize(-123456, long)
     
    normalize([ true, false, false, false ], 'boolean')
     
    normalize( {'true': true, 'false': false}, 'boolean')

    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 = require('java.io');
    io.addObject({{className}}, {{class}});
     

    Builtin classes:

    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
      }
    }

    Some more examples

    Incompatible between 1.x and 2.x

    • decode java [B to new Buffer([1, 2, 3]) not [1, 2, 3] #10

    License

    MIT

    Install

    npm i java.io

    DownloadsWeekly Downloads

    351

    Version

    2.4.0

    License

    MIT

    Unpacked Size

    57.5 kB

    Total Files

    17

    Last publish

    Collaborators

    • fool2fish
    • gxcsoccer
    • fengmk2