Added code for marshalling arrays of streamables.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@891 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2002-01-23 18:01:19 +00:00
parent 5510400f45
commit 61730287ca
@@ -1,9 +1,10 @@
// //
// $Id: ValueMarshaller.java,v 1.6 2001/10/19 18:03:28 mdb Exp $ // $Id: ValueMarshaller.java,v 1.7 2002/01/23 18:01:19 mdb Exp $
package com.threerings.presents.dobj.io; package com.threerings.presents.dobj.io;
import java.io.*; import java.io.*;
import java.lang.reflect.Array;
import java.util.HashMap; import java.util.HashMap;
import com.samskivert.util.HashIntMap; import com.samskivert.util.HashIntMap;
@@ -43,12 +44,15 @@ public class ValueMarshaller
public static void writeTo (DataOutputStream out, Object value) public static void writeTo (DataOutputStream out, Object value)
throws IOException throws IOException
{ {
// all types except streamable have a one to one mapping from // all types except streamable and arrays of streamable have a one
// class object to marshaller, but streamables have to be looked // to one mapping from class object to marshaller, but streamables
// up specially because we are dealing with a interface // have to be looked up specially because we are dealing with a
// implementation of streamable rather than a direct instance // interface implementation of streamable rather than a direct
Class vclass = (value instanceof Streamable) ? // instance
Streamable.class : value.getClass(); Class vclass = ((value instanceof Streamable) ?
Streamable.class :
((value instanceof Streamable[]) ?
STREAMARRAY_CLASS : value.getClass()));
Marshaller marsh = (Marshaller)_outmap.get(vclass); Marshaller marsh = (Marshaller)_outmap.get(vclass);
if (marsh == null) { if (marsh == null) {
@@ -521,9 +525,57 @@ public class ValueMarshaller
} }
} }
protected static class StreamableArrayMarshaller extends Marshaller
{
public StreamableArrayMarshaller ()
{
super((byte)16);
}
public void writeValue (DataOutputStream out, Object value)
throws IOException
{
out.writeUTF(value.getClass().getComponentType().getName());
Streamable[] values = (Streamable[])value;
int vlength = values.length;
out.writeInt(vlength);
for (int i = 0; i < vlength; i++) {
values[i].writeTo(out);
}
}
public Object readValue (DataInputStream in)
throws IOException
{
String cname = in.readUTF();
int vlength = in.readInt();
try {
Class vclass = Class.forName(cname);
Streamable[] values = (Streamable[])
Array.newInstance(vclass, vlength);
for (int i = 0; i < vlength; i++) {
Streamable value = (Streamable)vclass.newInstance();
value.readFrom(in);
values[i] = value;
}
return values;
} catch (Exception e) {
throw new IOException("Unable to unmarshall streamable " +
"array [cname=" + cname +
", length=" + vlength +
", error=" + e + "]");
}
}
}
protected static HashMap _outmap = new HashMap(); protected static HashMap _outmap = new HashMap();
protected static HashIntMap _inmap = new HashIntMap(); protected static HashIntMap _inmap = new HashIntMap();
protected static final Class STREAMARRAY_CLASS =
(new Streamable[0]).getClass();
protected static Class[] _classes = { protected static Class[] _classes = {
Byte.class, Byte.class,
Short.class, Short.class,
@@ -532,6 +584,7 @@ public class ValueMarshaller
Float.class, Float.class,
Double.class, Double.class,
String.class, String.class,
Streamable.class,
(new byte[0]).getClass(), (new byte[0]).getClass(),
(new short[0]).getClass(), (new short[0]).getClass(),
(new int[0]).getClass(), (new int[0]).getClass(),
@@ -539,7 +592,7 @@ public class ValueMarshaller
(new float[0]).getClass(), (new float[0]).getClass(),
(new double[0]).getClass(), (new double[0]).getClass(),
(new String[0]).getClass(), (new String[0]).getClass(),
Streamable.class STREAMARRAY_CLASS,
}; };
protected static Marshaller[] _marshallers = { protected static Marshaller[] _marshallers = {
@@ -550,6 +603,7 @@ public class ValueMarshaller
new FloatMarshaller(), new FloatMarshaller(),
new DoubleMarshaller(), new DoubleMarshaller(),
new StringMarshaller(), new StringMarshaller(),
new StreamableMarshaller(),
new ByteArrayMarshaller(), new ByteArrayMarshaller(),
new ShortArrayMarshaller(), new ShortArrayMarshaller(),
new IntegerArrayMarshaller(), new IntegerArrayMarshaller(),
@@ -557,7 +611,7 @@ public class ValueMarshaller
new FloatArrayMarshaller(), new FloatArrayMarshaller(),
new DoubleArrayMarshaller(), new DoubleArrayMarshaller(),
new StringArrayMarshaller(), new StringArrayMarshaller(),
new StreamableMarshaller() new StreamableArrayMarshaller(),
}; };
// register our marshallers // register our marshallers