org.simantics.databoard.serialization
Class Serializer

java.lang.Object
  extended by org.simantics.databoard.serialization.Serializer
Direct Known Subclasses:
Serializer.CompositeSerializer, Serializer.NonRecursiveSerializer, Serializer.RecursiveSerializer

public abstract class Serializer
extends java.lang.Object


Nested Class Summary
static class Serializer.CompositeSerializer
          Serializer for composite data types
static class Serializer.NonRecursiveSerializer
          Serializer for non-recursive data types
static class Serializer.RecursiveSerializer
          Serializer for recursive data types
 
Constructor Summary
Serializer()
           
 
Method Summary
 java.lang.Object deserialize(byte[] data)
          Deserialize an object in byte[] format.
 void deserialize(byte[] data, java.lang.Object obj)
          Deserialize byte[] into a valid object.
abstract  java.lang.Object deserialize(java.io.DataInput in)
           
abstract  java.lang.Object deserialize(java.io.DataInput in, java.util.List<java.lang.Object> identities)
          Deserialize an object from a readable.
abstract  void deserialize(java.io.DataInput in, java.util.List<java.lang.Object> identities, java.lang.Object obj)
          Deserialize into an valid object.
abstract  void deserialize(java.io.DataInput in, java.lang.Object obj)
           
 java.lang.Object deserialize(java.io.File file)
          Deserialize object from a file.
 void deserialize(java.io.File file, java.lang.Object obj)
          Deserialize a file into a valid object.
 java.lang.Object deserialize(java.io.InputStream in)
          Deserialize the next object in an input stream.
 void deserialize(java.io.InputStream in, java.lang.Object obj)
          Deserialize from an input stream into an object.
abstract  java.lang.Integer getConstantSize()
          Get constant size of the data type in its binary serialized format
 java.io.InputStream getInputStream(java.lang.Object obj)
          Get object as readable Input Stream.
abstract  int getMinSize()
           
abstract  int getSize(java.lang.Object obj)
           
abstract  int getSize(java.lang.Object obj, gnu.trove.TObjectIntHashMap<java.lang.Object> identities)
          Get the number of bytes required to serialize an object
abstract  void serialize(java.io.DataOutput out, java.lang.Object obj)
           
abstract  void serialize(java.io.DataOutput out, gnu.trove.TObjectIntHashMap<java.lang.Object> identities, java.lang.Object obj)
          Serialize obj to out.
 byte[] serialize(java.lang.Object obj)
          Serializes an object to a byte[].
 void serialize(java.lang.Object obj, java.io.File file)
          Serialize an object to a file
 void serialize(java.lang.Object obj, java.io.OutputStream out)
          Serializes an object to an output stream.
abstract  void skip(java.io.DataInput in)
           
abstract  void skip(java.io.DataInput in, java.util.List<java.lang.Object> identities)
          Skip over an object in a stream.
 void skip(java.io.InputStream in)
          Skip over an object in a stream.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Serializer

public Serializer()
Method Detail

serialize

public abstract void serialize(java.io.DataOutput out,
                               gnu.trove.TObjectIntHashMap<java.lang.Object> identities,
                               java.lang.Object obj)
                        throws java.io.IOException
Serialize obj to out. The identities argument is a map of identities (in the binary block) and objects that have already been serialized. Once serialized, an object is added to the map. Typically an empty map is provided. If the type has no recursion, a null value can be provided.

Parameters:
out -
identities - Thread local empty map or null if there is no recursion
obj -
Throws:
java.io.IOException

serialize

public abstract void serialize(java.io.DataOutput out,
                               java.lang.Object obj)
                        throws java.io.IOException
Throws:
java.io.IOException

deserialize

public abstract java.lang.Object deserialize(java.io.DataInput in,
                                             java.util.List<java.lang.Object> identities)
                                      throws java.io.IOException
Deserialize an object from a readable. The identities argument is a list of identities (in the binary block) of objects that have already been deserialized. Once deserialized they are added to the list. Typically an empty list is provided. If the type has no recursion, a null value can be provided.

Note, if in argument is instanceof BinaryReadable or RandomAccessBinary, the serializer performs extra protection against malformed data when deserializing arrays and maps. This prevents the serializer from instanting potentially out-of-memory-invoking huge arrays. For example, if data data says array size is 0xffffffff (-1), 4GB is allocated -> out of memory exception -> unhandled runtime error. BinaryReadable has length limit which allowes serializer to estimate whether future data is readable.

Parameters:
in - DataInput, BinaryReadable or RandomAccessBinary
identities - empty identities array or null if there is no recursion
Returns:
the instance
Throws:
java.io.IOException

deserialize

public abstract java.lang.Object deserialize(java.io.DataInput in)
                                      throws java.io.IOException
Throws:
java.io.IOException

deserialize

public abstract void deserialize(java.io.DataInput in,
                                 java.util.List<java.lang.Object> identities,
                                 java.lang.Object obj)
                          throws java.io.IOException
Deserialize into an valid object. This method writes over previous values.

Parameters:
in -
identities -
obj - valid object
Throws:
java.io.IOException

deserialize

public abstract void deserialize(java.io.DataInput in,
                                 java.lang.Object obj)
                          throws java.io.IOException
Throws:
java.io.IOException

deserialize

public java.lang.Object deserialize(java.io.InputStream in)
                             throws java.io.IOException
Deserialize the next object in an input stream. Note, if multiple objects are deserialized from the same stream, it is more efficient to instantiate InputStreamReadable and identities only once, and use deserialize(DataInput, List).

Parameters:
in -
Returns:
The object deserialized into a Java Object
Throws:
java.io.IOException

deserialize

public void deserialize(java.io.InputStream in,
                        java.lang.Object obj)
                 throws java.io.IOException
Deserialize from an input stream into an object. Note, if multiple objects are deserialized from the same stream, it is more efficient to instantiate InputStreamReadable and identities only once, and use deserialize(DataInput, List).

Parameters:
in -
obj - a valid object
Throws:
java.io.IOException

deserialize

public java.lang.Object deserialize(java.io.File file)
                             throws java.io.IOException
Deserialize object from a file.

Parameters:
file - source file
Returns:
the object
Throws:
java.io.IOException

deserialize

public void deserialize(java.io.File file,
                        java.lang.Object obj)
                 throws java.io.IOException
Deserialize a file into a valid object. This method writes over previous values.

Parameters:
file - source file
obj - a dst valid object
Throws:
java.io.IOException

deserialize

public java.lang.Object deserialize(byte[] data)
                             throws java.io.IOException
Deserialize an object in byte[] format.

Parameters:
data -
Returns:
the instance
Throws:
java.io.IOException

deserialize

public void deserialize(byte[] data,
                        java.lang.Object obj)
                 throws java.io.IOException
Deserialize byte[] into a valid object.

Parameters:
data -
obj - dst valid object
Throws:
java.io.IOException

skip

public abstract void skip(java.io.DataInput in,
                          java.util.List<java.lang.Object> identities)
                   throws java.io.IOException
Skip over an object in a stream. This method deserializes the object without producing a result or reading thru all bytes.

Parameters:
in -
identities -
Throws:
java.io.IOException

skip

public abstract void skip(java.io.DataInput in)
                   throws java.io.IOException
Throws:
java.io.IOException

skip

public void skip(java.io.InputStream in)
          throws java.io.IOException
Skip over an object in a stream. This method deserializes the object without producing a result or reading thru all bytes.

Parameters:
in -
Throws:
java.io.IOException

getConstantSize

public abstract java.lang.Integer getConstantSize()
Get constant size of the data type in its binary serialized format

Returns:
size in bytes or null if not fixed

getSize

public abstract int getSize(java.lang.Object obj,
                            gnu.trove.TObjectIntHashMap<java.lang.Object> identities)
                     throws java.io.IOException
Get the number of bytes required to serialize an object

Parameters:
obj -
identities - thread local empty hash map
Returns:
number of bytes required to serialize obj
Throws:
java.io.IOException

getSize

public abstract int getSize(java.lang.Object obj)
                     throws java.io.IOException
Throws:
java.io.IOException

getMinSize

public abstract int getMinSize()

serialize

public byte[] serialize(java.lang.Object obj)
                 throws java.io.IOException
Serializes an object to a byte[].

Parameters:
obj -
Returns:
byte array containing the obj in its serialized format.
Throws:
java.io.IOException

serialize

public void serialize(java.lang.Object obj,
                      java.io.OutputStream out)
               throws java.io.IOException
Serializes an object to an output stream. Note, if multiple objects are serialized to the same stream, it is more efficient to instantiate OutputStreamWriteable and identities only once.

Parameters:
obj -
out -
Throws:
java.io.IOException

serialize

public void serialize(java.lang.Object obj,
                      java.io.File file)
               throws java.io.IOException
Serialize an object to a file

Parameters:
obj -
file -
Throws:
java.io.IOException

getInputStream

public java.io.InputStream getInputStream(java.lang.Object obj)
                                   throws java.io.IOException
Get object as readable Input Stream.

Parameters:
obj -
Returns:
input stream
Throws:
java.io.IOException