JBoss Community Archive (Read Only)

JBoss Marshalling

Marshalling API quick start

It is easy to dive right in to using JBoss Marshalling, especially if you are already familiar with standard Java Serialization. The principles are largely the same, though the actual types involved are different. On the write side, you use the Marshaller interface, and on the read side you use the Unmarshaller interface.

Here's an example of writing an object to a file:

import java.io.FileOutputStream;
import java.io.IOException;
import org.jboss.marshalling.Marshaller;
import org.jboss.marshalling.MarshallerFactory;
import org.jboss.marshalling.Marshalling;
import org.jboss.marshalling.MarshallingConfiguration;

public final class WriteExample {

    public static void main(String[] args) {
        // Get the factory for the "river" marshalling protocol
        final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("river");

        // Create a configuration
        final MarshallingConfiguration configuration = new MarshallingConfiguration();
        // Use version 3
        configuration.setVersion(3);
        try {
            final Marshaller marshaller = marshallerFactory.createMarshaller(configuration);
            final FileOutputStream os = new FileOutputStream(args[0]);
            try {
                marshaller.start(Marshalling.createByteOutput(os));
                marshaller.writeObject("This is a trivial example.");
                marshaller.finish();
                os.close();
            } finally {
                // clean up stream resource
                try {
                    os.close();
                } catch (IOException e) {
                    System.err.print("Stream close failed: ");
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            System.err.print("Marshalling failed: ");
            e.printStackTrace();
        }
    }
}

This example creates the marshaller and writes a simple string object. To read the object back, we reverse the process:

import java.io.FileInputStream;
import java.io.IOException;
import org.jboss.marshalling.MarshallerFactory;
import org.jboss.marshalling.Marshalling;
import org.jboss.marshalling.MarshallingConfiguration;
import org.jboss.marshalling.Unmarshaller;

public final class ReadExample {

    public static void main(String[] args) {
        // Get the factory for the "river" marshalling protocol
        final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("river");

        // Create a configuration
        final MarshallingConfiguration configuration = new MarshallingConfiguration();
        try {
            final Unmarshaller unmarshaller = marshallerFactory.createUnmarshaller(configuration);
            final FileInputStream is = new FileInputStream(args[0]);
            try {
                unmarshaller.start(Marshalling.createByteInput(is));
                System.out.println("Read object: " + unmarshaller.readObject());
                unmarshaller.finish();
                is.close();
            } finally {
                // clean up stream resource
                try {
                    is.close();
                } catch (IOException e) {
                    System.err.print("Stream close failed: ");
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            System.err.print("Marshalling failed: ");
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            System.err.print("Marshalling failed: ");
            e.printStackTrace();
        }
    }
}

Note that we don't have to set the version, as it is read from the stream.

This is just a trivial example which should serve as a quick start to understanding the API; it does not exercise the advanced features of JBoss Marshalling which are described in this manual.

JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-11 11:32:15 UTC, last content change 2011-08-01 22:41:58 UTC.