wiki:uka.transport/Unmarshal

Implementing unmarshaling methods

Due to the fact that the uka.transport serialization is written in pure Java, it can not allocate uninitialized objects. When declaring a class to be transportable, a special constructor has to be provided that allocates a new object out of an UnmarshalStream.

Similar to marshaling, unmarshaling is also done in two phases:

  • A special constructor is called that is responsible for initializing all basic typed fields of the object with values unmarshaled from the stream. This constructor must have the signature <init>(UnmarshalStream).

There is no way in Java to enforce a class implementing a certain interface to also provide a particular constructor. This rule can only be established as programming convention and is not enforced by the compiler.

  • Method unmarshalReferences(UnmarshalStream) is called after initialization via the constructor described above. It is responsible for unmarshaling the values of reference type from the stream and storing them to the corresponding fields.

Unmarshaling references has to be done after construction of the object is complete. This will enable cyclic references to be resolved and stored into reference fields of the object currently being unmarshaled.

For easy reuse of unmarshaling code in subclass, it is useful to spit the unmarshaling of basic types into two different constructors one calling the other. Please refer to the example below:

//
// Unmarshaling: Methods and declarations for class TParam
//
public TParam(uka.transport.UnmarshalStream _stream)
    throws java.io.IOException, ClassNotFoundException
{
    this(_stream, _SIZE);
    _stream.accept(_SIZE);
}

protected TParam(uka.transport.UnmarshalStream  _stream, int _size)
    throws java.io.IOException, ClassNotFoundException
{
    _stream.request(_size); 
    byte[] _buffer = _stream.getBuffer();
    int    _pos    = _stream.getPosition();
    floatValue = uka.transport.BasicIO.extractFloat(_buffer, _pos);
    _pos += uka.transport.BasicIO.SIZEOF_float;
    intValue = uka.transport.BasicIO.extractInt(_buffer, _pos);
    _pos += uka.transport.BasicIO.SIZEOF_int;
}

public void unmarshalReferences(uka.transport.UnmarshalStream _stream)
    throws java.io.IOException, ClassNotFoundException
{
    objectReference = (java.lang.Object) _stream.readObject();
}
Last modified 12 years ago Last modified on Aug 22, 2005 9:10:25 AM