🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

java.io

Package Overview
Dependencies
Maintainers
2
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

java.io

Object Serialization Stream Protocol javascript implement

2.4.0
latest
Source
npm
Version published
Maintainers
2
Created
Source

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

Keywords

ObjectOutputStream

FAQs

Package last updated on 08 Jan 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts