From c6d2529e78ba0d8be972243db2a4fd0badeefd37 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Sat, 2 Feb 2002 08:48:03 +0000 Subject: [PATCH] It doesn't get much simpler than this: objects that desire to be streamable and that don't have funky requirements can simply extend SimpleStreamableObject and magically have their public fields streamed for them as well as have a useful default implementation of toString(). My tired fingers are thanking me already. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@917 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../presents/io/SimpleStreamableObject.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/java/com/threerings/presents/io/SimpleStreamableObject.java diff --git a/src/java/com/threerings/presents/io/SimpleStreamableObject.java b/src/java/com/threerings/presents/io/SimpleStreamableObject.java new file mode 100644 index 000000000..f6dab535e --- /dev/null +++ b/src/java/com/threerings/presents/io/SimpleStreamableObject.java @@ -0,0 +1,45 @@ +// +// $Id: SimpleStreamableObject.java,v 1.1 2002/02/02 08:48:03 mdb Exp $ + +package com.threerings.presents.io; + +import java.io.IOException; +import java.io.DataInputStream; +import java.io.DataOutputStream; + +import com.samskivert.util.StringUtil; + +/** + * A simple streamable object uses the {@link Marshaller} class to stream + * its fields and provides a simple mechanism for objects to make + * themselves streamable without having to write any code. They simply + * declare all fields to be streamed as public data members (marking + * public fields as transient that should not be streamed) + * and ensure that the fields are all valid streamable types (see {@link + * FieldMarshallerRegistry}). + */ +public class SimpleStreamableObject + implements Streamable +{ + // documentation inherited from interface + public void writeTo (DataOutputStream out) + throws IOException + { + Marshaller.writeObject(out, this); + } + + // documentation inherited from interface + public void readFrom (DataInputStream in) + throws IOException + { + Marshaller.readObject(in, this); + } + + /** + * Generates a string representation of this instance. + */ + public String toString () + { + return StringUtil.fieldsToString(this); + } +}