Module id.kineticstreamer

kineticstreamer - Java module to do (de)serialization of Java objects into streams. It provides default implementations for object conversion to stream of bytes and CSV files. But you can add support to any other format as well.

It parses object tree and allows you to get control over how types are going to be (de)serialized.

Only fields annotated with Streamed annotation will be (de)serialized.

Stream

In terms of kineticstreamer 'stream' represents sequence of any sort (sequence of bytes, words, numbers, etc). The type of elements in such sequence don't need to be the same. Streams have flat schemas opposite to JSON, XML, etc.

Example of streams are flat byte formats, CSV files, etc.

Kinetic stream

Kinetic stream is an abstraction which is defined by two interfaces:

kineticstreamer relies on them for Java objects (de)serialization.

Kinetic streams allow kineticstreamer to operate with any type of streams without even knowing anything about their format.

Every type of stream has its own implementation of kinetic stream ifaces.

kineticstreamer comes with some predefined kinetic streams (see id.kineticstreamer.streams). To add support for custom types of streams you need to implement kinetic stream ifaces for them.

Arrays

kineticstreamer supports (de)serialization of arrays with non-primitive types plus some primitive types as well. If you need to use arrays with primitive type which are not yet supported please use their wrapped version (Short[], etc).

Defining streamed classes

These are the classes which are going to be (de)serialized with their fields being streamed. There are certain requirements to such classes:

  • Need to define default ctor
  • Fields which will be (de)serialized (streamed fields) should be annotated with Streamed annotation
  • Streamed fields should be accessible to the kineticstreamer or be public
  • Streamed fields which point to arrays should be initialized with empty arrays
  • If the type of streamed field is yet another streamed class then such field should be initialized with non null value (i.e. with its default ctor) otherwise the kineticstreamer will ignore such fields during streaming with all its subfields

Examples

Streamed class:


public class StringMessage {

    @Streamed
    public String data;

    // streamed objects require default ctor
    public StringMessage() {
    }

    public StringMessage(String data) {
        this.data = data;
    }

}
 
Streaming:

// write
var tmpFile = Files.createTempFile("", "kineticstream");
var fos = new FileOutputStream(tmpFile.toFile());
var dos = new ByteOutputKineticStream(new DataOutputStream(fos));
var ksw = new KineticStreamWriter(dos);
ksw.write(new StringMessage("hello kineticstreamer"));

// read back
var fis = new FileInputStream(tmpFile.toFile());
var dis = new ByteInputKineticStream(new DataInputStream(fis));
var ksr = new KineticStreamReader(dis);
StringMessage actual = (StringMessage) ksr.read(StringMessage.class);
System.out.println(actual.data);
 
See Also:
Download kineticstreamer, Github