Modified field marshalling code to work on any object rather than being

DObject specific (because it didn't need to be).


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@911 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2002-02-01 23:26:49 +00:00
parent d925c5d1b4
commit ef9e0bc53f
15 changed files with 131 additions and 124 deletions
@@ -1,12 +1,11 @@
// //
// $Id: DObjectFactory.java,v 1.9 2001/10/16 16:44:20 mdb Exp $ // $Id: DObjectFactory.java,v 1.10 2002/02/01 23:26:49 mdb Exp $
package com.threerings.presents.dobj.io; package com.threerings.presents.dobj.io;
import java.io.IOException; import java.io.IOException;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.util.HashMap;
import com.threerings.presents.Log; import com.threerings.presents.Log;
import com.threerings.presents.dobj.DObject; import com.threerings.presents.dobj.DObject;
@@ -27,15 +26,13 @@ public class DObjectFactory
throws IOException throws IOException
{ {
// Log.info("Marshalling object: " + dobj); // Log.info("Marshalling object: " + dobj);
// look up the marshaller for this object
Class clazz = dobj.getClass(); // write the class of the object to the stream
Marshaller marsh = getMarshaller(clazz); out.writeUTF(dobj.getClass().getName());
// then write the class of the object to the stream
out.writeUTF(clazz.getName());
// write out the oid // write out the oid
out.writeInt(dobj.getOid()); out.writeInt(dobj.getOid());
// then use the marshaller to write the object itself // then use the marshaller to write the object itself
marsh.writeTo(out, dobj); Marshaller.writeObject(out, dobj);
} }
/** /**
@@ -49,12 +46,11 @@ public class DObjectFactory
Class clazz = Class.forName(in.readUTF()); Class clazz = Class.forName(in.readUTF());
DObject dobj = (DObject)clazz.newInstance(); DObject dobj = (DObject)clazz.newInstance();
dobj.setOid(in.readInt()); // read and set the oid dobj.setOid(in.readInt()); // read and set the oid
// Log.info("Unmarshalling object: " + dobj); // Log.info("Unmarshalling object: " + dobj);
// look up the marshaller for that class // use a marshaller to reconstitute the object from the stream
Marshaller marsh = getMarshaller(clazz); Marshaller.readObject(in, dobj);
// use it to reconstitute the object from the stream
marsh.readFrom(in, dobj);
return dobj; return dobj;
@@ -63,17 +59,4 @@ public class DObjectFactory
throw new ObjectStreamException(errmsg, e); throw new ObjectStreamException(errmsg, e);
} }
} }
protected static Marshaller getMarshaller (Class clazz)
throws IOException
{
Marshaller marsh = (Marshaller)_marshallers.get(clazz);
// create a new marshaller if we don't already have one
if (marsh == null) {
_marshallers.put(clazz, marsh = new Marshaller(clazz));
}
return marsh;
}
protected static HashMap _marshallers = new HashMap();
} }
@@ -1,5 +1,5 @@
// //
// $Id: BooleanFieldMarshaller.java,v 1.4 2001/10/11 04:07:52 mdb Exp $ // $Id: BooleanFieldMarshaller.java,v 1.5 2002/02/01 23:26:49 mdb Exp $
package com.threerings.presents.dobj.io; package com.threerings.presents.dobj.io;
@@ -8,22 +8,20 @@ import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import com.threerings.presents.dobj.DObject;
public class BooleanFieldMarshaller implements FieldMarshaller public class BooleanFieldMarshaller implements FieldMarshaller
{ {
/** This is the sort of field that we marshall. */ /** This is the sort of field that we marshall. */
public boolean prototype; public boolean prototype;
public void writeTo (DataOutputStream out, Field field, DObject dobj) public void writeTo (DataOutputStream out, Field field, Object obj)
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
out.writeBoolean(field.getBoolean(dobj)); out.writeBoolean(field.getBoolean(obj));
} }
public void readFrom (DataInputStream in, Field field, DObject dobj) public void readFrom (DataInputStream in, Field field, Object obj)
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
field.setBoolean(dobj, in.readBoolean()); field.setBoolean(obj, in.readBoolean());
} }
} }
@@ -1,5 +1,5 @@
// //
// $Id: DSetFieldMarshaller.java,v 1.2 2001/10/11 04:07:52 mdb Exp $ // $Id: DSetFieldMarshaller.java,v 1.3 2002/02/01 23:26:49 mdb Exp $
package com.threerings.presents.dobj.io; package com.threerings.presents.dobj.io;
@@ -8,7 +8,6 @@ import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import com.threerings.presents.dobj.DObject;
import com.threerings.presents.dobj.DSet; import com.threerings.presents.dobj.DSet;
public class DSetFieldMarshaller implements FieldMarshaller public class DSetFieldMarshaller implements FieldMarshaller
@@ -16,10 +15,10 @@ public class DSetFieldMarshaller implements FieldMarshaller
/** This is the sort of field that we marshall. */ /** This is the sort of field that we marshall. */
public DSet prototype; public DSet prototype;
public void writeTo (DataOutputStream out, Field field, DObject dobj) public void writeTo (DataOutputStream out, Field field, Object obj)
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
DSet value = (DSet)field.get(dobj); DSet value = (DSet)field.get(obj);
// we freak out if our set is null // we freak out if our set is null
if (value == null) { if (value == null) {
throw new IllegalAccessException("No set instance to marshall!"); throw new IllegalAccessException("No set instance to marshall!");
@@ -27,11 +26,11 @@ public class DSetFieldMarshaller implements FieldMarshaller
value.writeTo(out); value.writeTo(out);
} }
public void readFrom (DataInputStream in, Field field, DObject dobj) public void readFrom (DataInputStream in, Field field, Object obj)
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
DSet value = new DSet(); DSet value = new DSet();
value.readFrom(in); value.readFrom(in);
field.set(dobj, value); field.set(obj, value);
} }
} }
@@ -1,5 +1,5 @@
// //
// $Id: DoubleFieldMarshaller.java,v 1.4 2001/10/11 04:07:52 mdb Exp $ // $Id: DoubleFieldMarshaller.java,v 1.5 2002/02/01 23:26:49 mdb Exp $
package com.threerings.presents.dobj.io; package com.threerings.presents.dobj.io;
@@ -8,22 +8,20 @@ import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import com.threerings.presents.dobj.DObject;
public class DoubleFieldMarshaller implements FieldMarshaller public class DoubleFieldMarshaller implements FieldMarshaller
{ {
/** This is the sort of field that we marshall. */ /** This is the sort of field that we marshall. */
public double prototype; public double prototype;
public void writeTo (DataOutputStream out, Field field, DObject dobj) public void writeTo (DataOutputStream out, Field field, Object obj)
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
out.writeDouble(field.getDouble(dobj)); out.writeDouble(field.getDouble(obj));
} }
public void readFrom (DataInputStream in, Field field, DObject dobj) public void readFrom (DataInputStream in, Field field, Object obj)
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
field.setDouble(dobj, in.readDouble()); field.setDouble(obj, in.readDouble());
} }
} }
@@ -1,5 +1,5 @@
// //
// $Id: FieldMarshaller.java,v 1.4 2001/10/11 04:07:52 mdb Exp $ // $Id: FieldMarshaller.java,v 1.5 2002/02/01 23:26:49 mdb Exp $
package com.threerings.presents.dobj.io; package com.threerings.presents.dobj.io;
@@ -8,8 +8,6 @@ import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import com.threerings.presents.dobj.DObject;
/** /**
* A field marshaller knows how to marshall and unmarshall a particular * A field marshaller knows how to marshall and unmarshall a particular
* data type. It is called upon to do the actual on the wire * data type. It is called upon to do the actual on the wire
@@ -17,9 +15,9 @@ import com.threerings.presents.dobj.DObject;
*/ */
public interface FieldMarshaller public interface FieldMarshaller
{ {
public void writeTo (DataOutputStream out, Field field, DObject dobj) public void writeTo (DataOutputStream out, Field field, Object obj)
throws IOException, IllegalAccessException; throws IOException, IllegalAccessException;
public void readFrom (DataInputStream in, Field field, DObject dobj) public void readFrom (DataInputStream in, Field field, Object obj)
throws IOException, IllegalAccessException; throws IOException, IllegalAccessException;
} }
@@ -1,5 +1,5 @@
// //
// $Id: FloatFieldMarshaller.java,v 1.4 2001/10/11 04:07:52 mdb Exp $ // $Id: FloatFieldMarshaller.java,v 1.5 2002/02/01 23:26:49 mdb Exp $
package com.threerings.presents.dobj.io; package com.threerings.presents.dobj.io;
@@ -8,22 +8,20 @@ import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import com.threerings.presents.dobj.DObject;
public class FloatFieldMarshaller implements FieldMarshaller public class FloatFieldMarshaller implements FieldMarshaller
{ {
/** This is the sort of field that we marshall. */ /** This is the sort of field that we marshall. */
public float prototype; public float prototype;
public void writeTo (DataOutputStream out, Field field, DObject dobj) public void writeTo (DataOutputStream out, Field field, Object obj)
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
out.writeFloat(field.getFloat(dobj)); out.writeFloat(field.getFloat(obj));
} }
public void readFrom (DataInputStream in, Field field, DObject dobj) public void readFrom (DataInputStream in, Field field, Object obj)
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
field.setFloat(dobj, in.readFloat()); field.setFloat(obj, in.readFloat());
} }
} }
@@ -1,5 +1,5 @@
// //
// $Id: IntArrayFieldMarshaller.java,v 1.1 2001/10/17 02:56:51 mdb Exp $ // $Id: IntArrayFieldMarshaller.java,v 1.2 2002/02/01 23:26:49 mdb Exp $
package com.threerings.presents.dobj.io; package com.threerings.presents.dobj.io;
@@ -8,17 +8,15 @@ import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import com.threerings.presents.dobj.DObject;
public class IntArrayFieldMarshaller implements FieldMarshaller public class IntArrayFieldMarshaller implements FieldMarshaller
{ {
/** This is the sort of field that we marshall. */ /** This is the sort of field that we marshall. */
public int[] prototype; public int[] prototype;
public void writeTo (DataOutputStream out, Field field, DObject dobj) public void writeTo (DataOutputStream out, Field field, Object obj)
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
int[] value = (int[])field.get(dobj); int[] value = (int[])field.get(obj);
// we convert null arrays to zero length arrays // we convert null arrays to zero length arrays
if (value == null) { if (value == null) {
out.writeInt(0); out.writeInt(0);
@@ -31,13 +29,13 @@ public class IntArrayFieldMarshaller implements FieldMarshaller
} }
} }
public void readFrom (DataInputStream in, Field field, DObject dobj) public void readFrom (DataInputStream in, Field field, Object obj)
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
int[] value = new int[in.readInt()]; int[] value = new int[in.readInt()];
for (int i = 0; i < value.length; i++) { for (int i = 0; i < value.length; i++) {
value[i] = in.readInt(); value[i] = in.readInt();
} }
field.set(dobj, value); field.set(obj, value);
} }
} }
@@ -1,5 +1,5 @@
// //
// $Id: IntFieldMarshaller.java,v 1.4 2001/10/11 04:07:52 mdb Exp $ // $Id: IntFieldMarshaller.java,v 1.5 2002/02/01 23:26:49 mdb Exp $
package com.threerings.presents.dobj.io; package com.threerings.presents.dobj.io;
@@ -8,22 +8,20 @@ import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import com.threerings.presents.dobj.DObject;
public class IntFieldMarshaller implements FieldMarshaller public class IntFieldMarshaller implements FieldMarshaller
{ {
/** This is the sort of field that we marshall. */ /** This is the sort of field that we marshall. */
public int prototype; public int prototype;
public void writeTo (DataOutputStream out, Field field, DObject dobj) public void writeTo (DataOutputStream out, Field field, Object obj)
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
out.writeInt(field.getInt(dobj)); out.writeInt(field.getInt(obj));
} }
public void readFrom (DataInputStream in, Field field, DObject dobj) public void readFrom (DataInputStream in, Field field, Object obj)
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
field.setInt(dobj, in.readInt()); field.setInt(obj, in.readInt());
} }
} }
@@ -1,5 +1,5 @@
// //
// $Id: LongFieldMarshaller.java,v 1.4 2001/10/11 04:07:52 mdb Exp $ // $Id: LongFieldMarshaller.java,v 1.5 2002/02/01 23:26:49 mdb Exp $
package com.threerings.presents.dobj.io; package com.threerings.presents.dobj.io;
@@ -8,22 +8,20 @@ import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import com.threerings.presents.dobj.DObject;
public class LongFieldMarshaller implements FieldMarshaller public class LongFieldMarshaller implements FieldMarshaller
{ {
/** This is the sort of field that we marshall. */ /** This is the sort of field that we marshall. */
public long prototype; public long prototype;
public void writeTo (DataOutputStream out, Field field, DObject dobj) public void writeTo (DataOutputStream out, Field field, Object obj)
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
out.writeLong(field.getLong(dobj)); out.writeLong(field.getLong(obj));
} }
public void readFrom (DataInputStream in, Field field, DObject dobj) public void readFrom (DataInputStream in, Field field, Object obj)
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
field.setLong(dobj, in.readLong()); field.setLong(obj, in.readLong());
} }
} }
@@ -1,5 +1,5 @@
// //
// $Id: Marshaller.java,v 1.6 2002/02/01 22:35:06 mdb Exp $ // $Id: Marshaller.java,v 1.7 2002/02/01 23:26:49 mdb Exp $
package com.threerings.presents.dobj.io; package com.threerings.presents.dobj.io;
@@ -9,12 +9,12 @@ import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap;
import com.threerings.presents.Log; import com.threerings.presents.Log;
import com.threerings.presents.dobj.DObject;
import com.threerings.presents.io.ObjectStreamException; import com.threerings.presents.io.ObjectStreamException;
/** /**
@@ -24,7 +24,47 @@ import com.threerings.presents.io.ObjectStreamException;
*/ */
public class Marshaller public class Marshaller
{ {
public Marshaller (Class clazz) /**
* Writes the supplied object instance out to the supplied data output
* stream, using a {@link Marshaller} for field marshalling.
*/
public static void writeObject (DataOutputStream out, Object object)
throws IOException
{
getMarshaller(object).writeTo(out, object);
}
/**
* Populates the fields of the supplied object from the supplied data
* input stream, using a {@link Marshaller} for field unmarshalling.
*/
public static void readObject (DataInputStream in, Object object)
throws IOException
{
getMarshaller(object).readFrom(in, object);
}
/**
* Returns the marshaller for objects of this object's class, creating
* one if necessary.
*/
protected static synchronized Marshaller getMarshaller (Object object)
{
Class clazz = object.getClass();
Marshaller marsh = (Marshaller)_marshcache.get(clazz);
if (marsh == null) {
marsh = new Marshaller(clazz);
_marshcache.put(clazz, marsh);
}
return marsh;
}
/**
* Instantiate a marshaller for the specified class. Introspection is
* done during construct time to speed up subsequent marshalling and
* unmarshalling.
*/
protected Marshaller (Class clazz)
{ {
// we introspect on the class and cache the public data members // we introspect on the class and cache the public data members
Field[] fields = clazz.getFields(); Field[] fields = clazz.getFields();
@@ -65,21 +105,21 @@ public class Marshaller
* Writes out all of the fields of the specified distributed object to * Writes out all of the fields of the specified distributed object to
* the supplied data output stream. * the supplied data output stream.
*/ */
public void writeTo (DataOutputStream out, DObject dobj) public void writeTo (DataOutputStream out, Object obj)
throws IOException throws IOException
{ {
// we simply iterate over our marshallers, writing each field // we simply iterate over our marshallers, writing each field
// out in succession // out in succession
for (int i = 0; i < _fields.length; i++) { for (int i = 0; i < _fields.length; i++) {
try { try {
_marshallers[i].writeTo(out, _fields[i], dobj); _marshallers[i].writeTo(out, _fields[i], obj);
} catch (IllegalAccessException iae) { } catch (IllegalAccessException iae) {
// this shouldn't happen because we only attempt to marshall // this shouldn't happen because we only attempt to marshall
// public fields, but we'll pass it on none the less // public fields, but we'll pass it on none the less
String errmsg = "Unable to marshall dobj field " + String errmsg = "Unable to marshall obj field " +
"[field=" + _fields[i].getName() + "[field=" + _fields[i].getName() +
", dobj=" + dobj + "]."; ", obj=" + obj + "].";
throw new ObjectStreamException(errmsg); throw new ObjectStreamException(errmsg);
} }
} }
@@ -89,21 +129,21 @@ public class Marshaller
* Reads in all of the fields of the specified distributed object from * Reads in all of the fields of the specified distributed object from
* the supplied data input stream. * the supplied data input stream.
*/ */
public void readFrom (DataInputStream in, DObject dobj) public void readFrom (DataInputStream in, Object obj)
throws IOException throws IOException
{ {
// we simply iterate over our marshallers, reading each field in // we simply iterate over our marshallers, reading each field in
// in succession // in succession
for (int i = 0; i < _fields.length; i++) { for (int i = 0; i < _fields.length; i++) {
try { try {
_marshallers[i].readFrom(in, _fields[i], dobj); _marshallers[i].readFrom(in, _fields[i], obj);
} catch (IllegalAccessException iae) { } catch (IllegalAccessException iae) {
// this shouldn't happen because we only attempt to // this shouldn't happen because we only attempt to
// unmarshall public fields, but we'll pass it on anyway // unmarshall public fields, but we'll pass it on anyway
String errmsg = "Unable to unmarshall dobj field " + String errmsg = "Unable to unmarshall obj field " +
"[field=" + _fields[i].getName() + "[field=" + _fields[i].getName() +
", dobj=" + dobj + "]."; ", obj=" + obj + "].";
Log.logStackTrace(iae); Log.logStackTrace(iae);
throw new ObjectStreamException(errmsg); throw new ObjectStreamException(errmsg);
} }
@@ -127,9 +167,16 @@ public class Marshaller
} }
} }
/** The fields that are marshalled by this marshaller. */
protected Field[] _fields; protected Field[] _fields;
/** The field marshallers that are used to marshall the fields. */
protected FieldMarshaller[] _marshallers; protected FieldMarshaller[] _marshallers;
/** A table of instantiated marshallers for our various marshalled
* classes. */
protected static HashMap _marshcache;
/** We use this to sort the fields into a predictable order. */ /** We use this to sort the fields into a predictable order. */
protected static final FieldComparator FIELD_COMP = new FieldComparator(); protected static final FieldComparator FIELD_COMP = new FieldComparator();
} }
@@ -1,5 +1,5 @@
// //
// $Id: OidListFieldMarshaller.java,v 1.2 2001/10/11 04:07:52 mdb Exp $ // $Id: OidListFieldMarshaller.java,v 1.3 2002/02/01 23:26:49 mdb Exp $
package com.threerings.presents.dobj.io; package com.threerings.presents.dobj.io;
@@ -8,7 +8,6 @@ import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import com.threerings.presents.dobj.DObject;
import com.threerings.presents.dobj.OidList; import com.threerings.presents.dobj.OidList;
public class OidListFieldMarshaller implements FieldMarshaller public class OidListFieldMarshaller implements FieldMarshaller
@@ -16,10 +15,10 @@ public class OidListFieldMarshaller implements FieldMarshaller
/** This is the sort of field that we marshall. */ /** This is the sort of field that we marshall. */
public OidList prototype; public OidList prototype;
public void writeTo (DataOutputStream out, Field field, DObject dobj) public void writeTo (DataOutputStream out, Field field, Object obj)
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
OidList value = (OidList)field.get(dobj); OidList value = (OidList)field.get(obj);
// we convert null oid lists to empty oid lists // we convert null oid lists to empty oid lists
if (value == null) { if (value == null) {
value = new OidList(); value = new OidList();
@@ -27,11 +26,11 @@ public class OidListFieldMarshaller implements FieldMarshaller
value.writeTo(out); value.writeTo(out);
} }
public void readFrom (DataInputStream in, Field field, DObject dobj) public void readFrom (DataInputStream in, Field field, Object obj)
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
OidList value = new OidList(); OidList value = new OidList();
value.readFrom(in); value.readFrom(in);
field.set(dobj, value); field.set(obj, value);
} }
} }
@@ -1,5 +1,5 @@
// //
// $Id: ShortFieldMarshaller.java,v 1.4 2001/10/11 04:07:52 mdb Exp $ // $Id: ShortFieldMarshaller.java,v 1.5 2002/02/01 23:26:49 mdb Exp $
package com.threerings.presents.dobj.io; package com.threerings.presents.dobj.io;
@@ -8,22 +8,20 @@ import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import com.threerings.presents.dobj.DObject;
public class ShortFieldMarshaller implements FieldMarshaller public class ShortFieldMarshaller implements FieldMarshaller
{ {
/** This is the sort of field that we marshall. */ /** This is the sort of field that we marshall. */
public short prototype; public short prototype;
public void writeTo (DataOutputStream out, Field field, DObject dobj) public void writeTo (DataOutputStream out, Field field, Object obj)
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
out.writeShort(field.getShort(dobj)); out.writeShort(field.getShort(obj));
} }
public void readFrom (DataInputStream in, Field field, DObject dobj) public void readFrom (DataInputStream in, Field field, Object obj)
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
field.setShort(dobj, in.readShort()); field.setShort(obj, in.readShort());
} }
} }
@@ -1,5 +1,5 @@
// //
// $Id: StreamableFieldMarshaller.java,v 1.1 2001/10/12 19:28:43 mdb Exp $ // $Id: StreamableFieldMarshaller.java,v 1.2 2002/02/01 23:26:49 mdb Exp $
package com.threerings.presents.dobj.io; package com.threerings.presents.dobj.io;
@@ -8,15 +8,14 @@ import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import com.threerings.presents.dobj.DObject;
import com.threerings.presents.io.Streamable; import com.threerings.presents.io.Streamable;
public class StreamableFieldMarshaller implements FieldMarshaller public class StreamableFieldMarshaller implements FieldMarshaller
{ {
public void writeTo (DataOutputStream out, Field field, DObject dobj) public void writeTo (DataOutputStream out, Field field, Object obj)
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
Streamable value = (Streamable)field.get(dobj); Streamable value = (Streamable)field.get(obj);
// we freak out if our streamable is null // we freak out if our streamable is null
if (value == null) { if (value == null) {
throw new IllegalAccessException( throw new IllegalAccessException(
@@ -25,7 +24,7 @@ public class StreamableFieldMarshaller implements FieldMarshaller
value.writeTo(out); value.writeTo(out);
} }
public void readFrom (DataInputStream in, Field field, DObject dobj) public void readFrom (DataInputStream in, Field field, Object obj)
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
try { try {
@@ -34,11 +33,11 @@ public class StreamableFieldMarshaller implements FieldMarshaller
// unserialize it // unserialize it
value.readFrom(in); value.readFrom(in);
// and set the value in the object // and set the value in the object
field.set(dobj, value); field.set(obj, value);
} catch (InstantiationException ie) { } catch (InstantiationException ie) {
throw new IOException("Unable to instantiate streamable " + throw new IOException("Unable to instantiate streamable " +
"[field=" + field + ", object=" + dobj + "[field=" + field + ", object=" + obj +
", error=" + ie + "]"); ", error=" + ie + "]");
} }
} }
@@ -1,5 +1,5 @@
// //
// $Id: StringArrayFieldMarshaller.java,v 1.2 2001/10/19 18:03:28 mdb Exp $ // $Id: StringArrayFieldMarshaller.java,v 1.3 2002/02/01 23:26:49 mdb Exp $
package com.threerings.presents.dobj.io; package com.threerings.presents.dobj.io;
@@ -8,17 +8,15 @@ import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import com.threerings.presents.dobj.DObject;
public class StringArrayFieldMarshaller implements FieldMarshaller public class StringArrayFieldMarshaller implements FieldMarshaller
{ {
/** This is the sort of field that we marshall. */ /** This is the sort of field that we marshall. */
public String[] prototype; public String[] prototype;
public void writeTo (DataOutputStream out, Field field, DObject dobj) public void writeTo (DataOutputStream out, Field field, Object obj)
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
String[] value = (String[])field.get(dobj); String[] value = (String[])field.get(obj);
// we convert null string arrays to zero length arrays // we convert null string arrays to zero length arrays
if (value == null) { if (value == null) {
out.writeInt(0); out.writeInt(0);
@@ -31,13 +29,13 @@ public class StringArrayFieldMarshaller implements FieldMarshaller
} }
} }
public void readFrom (DataInputStream in, Field field, DObject dobj) public void readFrom (DataInputStream in, Field field, Object obj)
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
String[] value = new String[in.readInt()]; String[] value = new String[in.readInt()];
for (int i = 0; i < value.length; i++) { for (int i = 0; i < value.length; i++) {
value[i] = in.readUTF(); value[i] = in.readUTF();
} }
field.set(dobj, value); field.set(obj, value);
} }
} }
@@ -1,5 +1,5 @@
// //
// $Id: StringFieldMarshaller.java,v 1.5 2001/10/11 04:07:52 mdb Exp $ // $Id: StringFieldMarshaller.java,v 1.6 2002/02/01 23:26:49 mdb Exp $
package com.threerings.presents.dobj.io; package com.threerings.presents.dobj.io;
@@ -8,17 +8,15 @@ import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import com.threerings.presents.dobj.DObject;
public class StringFieldMarshaller implements FieldMarshaller public class StringFieldMarshaller implements FieldMarshaller
{ {
/** This is the sort of field that we marshall. */ /** This is the sort of field that we marshall. */
public String prototype; public String prototype;
public void writeTo (DataOutputStream out, Field field, DObject dobj) public void writeTo (DataOutputStream out, Field field, Object obj)
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
String value = (String)field.get(dobj); String value = (String)field.get(obj);
// we convert null strings to empty strings // we convert null strings to empty strings
if (value == null) { if (value == null) {
value = ""; value = "";
@@ -26,9 +24,9 @@ public class StringFieldMarshaller implements FieldMarshaller
out.writeUTF(value); out.writeUTF(value);
} }
public void readFrom (DataInputStream in, Field field, DObject dobj) public void readFrom (DataInputStream in, Field field, Object obj)
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
field.set(dobj, in.readUTF()); field.set(obj, in.readUTF());
} }
} }