# The FreePastry Tutorial.

This tutorial is designed to get you cooking quickly with the FreePastry API and software toolkit.

#### Version @tutorial_version@; @tutorial_date@. For FreePastry version @freepastry_version@. Maintained by @maintainer@.

# Raw Serialization 2

## Override raw serialization with Java serialization.

This tutorial shows you how to extend a raw serialized message from another application but use Java Serialization. In other words, how to override RawMessage to use Java Serialization.

### Download the tutorial files: DistTutorial.java MyApp.java, MyMsg.java into a directory called rice/tutorial/rawserialization2/.

In this tutorial, we modified lesson4's MyMessage to extend past's LookupMessage instead of implementing Message. Since LookupMessage is raw serialized, you will run into errors if you don't override it's serialization/deserialization mechanism.

Because we extended LookupMessage, we have to modify the constructor to call `super()`.

    public class MyMsg extends LookupMessage {

      ...

      public MyMsg(NodeHandle from, Id to) {
        super(0,from.getId(),from,to);
        this.from = from.getId();
        this.to = to;
      }

      ...

    }

The first thing to do is to override the `serialize()` method in MyApp. We implemented this method in the previous tutorial. However this time, it is simpler. Because we are using Java Serialization, this does not need to do anything that is message specific. We use the rice.p2p.util.rawserialization.JavaSerializer's static method `serialize()`.

      public void serialize(OutputBuffer buf) throws IOException {
        JavaSerializer.serialize(this, buf);
      }

For reverse compatibility, the default deserializer in the endpoint is the JavaSerializedDeserializer. All we have to do is to tell it to always use Java(De)Serialization. We will do this in the constructor for MyApp. To prevent run-time errors, it is important to make any modifications to the deserializer before calling `endpoint.register()`. The `setAlwaysUseJavaSerialization(true)` call will suppress error messages if the deserializer encounters messages with a type that is not 0. type 0 is reserved for Java serialized messages.

        this.endpoint = node.buildEndpoint(this, "myinstance");

        ((JavaSerializedDeserializer)endpoint.getDeserializer()).setAlwaysUseJavaSerialization(true);

        this.endpoint.register();

That's all there is to it.

### Congratulations! You have overridden the raw serialization of an existing message to use Java serialization!