Widening, one additional log message added in STREAM_DEBUG mode.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4558 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2007-02-12 03:03:51 +00:00
parent a617b9322e
commit d9089e10d3
4 changed files with 189 additions and 298 deletions
+45 -86
View File
@@ -93,16 +93,14 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void writeObject ( public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
Object object, ObjectOutputStream out, boolean useWriter)
throws IOException throws IOException
{ {
out.writeBoolean(((Boolean)object).booleanValue()); out.writeBoolean(((Boolean)object).booleanValue());
} }
// documentation inherited // documentation inherited
public void readObject ( public void readObject (Object object, ObjectInputStream in, boolean useReader)
Object object, ObjectInputStream in, boolean useReader)
throws IOException, ClassNotFoundException throws IOException, ClassNotFoundException
{ {
// nothing to do here // nothing to do here
@@ -120,16 +118,14 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void writeObject ( public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
Object object, ObjectOutputStream out, boolean useWriter)
throws IOException throws IOException
{ {
out.writeByte(((Byte)object).byteValue()); out.writeByte(((Byte)object).byteValue());
} }
// documentation inherited // documentation inherited
public void readObject ( public void readObject (Object object, ObjectInputStream in, boolean useReader)
Object object, ObjectInputStream in, boolean useReader)
throws IOException, ClassNotFoundException throws IOException, ClassNotFoundException
{ {
// nothing to do here // nothing to do here
@@ -147,16 +143,14 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void writeObject ( public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
Object object, ObjectOutputStream out, boolean useWriter)
throws IOException throws IOException
{ {
out.writeShort(((Short)object).shortValue()); out.writeShort(((Short)object).shortValue());
} }
// documentation inherited // documentation inherited
public void readObject ( public void readObject (Object object, ObjectInputStream in, boolean useReader)
Object object, ObjectInputStream in, boolean useReader)
throws IOException, ClassNotFoundException throws IOException, ClassNotFoundException
{ {
// nothing to do here // nothing to do here
@@ -167,23 +161,20 @@ public class BasicStreamers
public static class CharacterStreamer extends Streamer public static class CharacterStreamer extends Streamer
{ {
// documentation inherited // documentation inherited
public Object createObject (ObjectInputStream in) public Object createObject (ObjectInputStream in)throws IOException
throws IOException
{ {
return Character.valueOf(in.readChar()); return Character.valueOf(in.readChar());
} }
// documentation inherited // documentation inherited
public void writeObject ( public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
Object object, ObjectOutputStream out, boolean useWriter)
throws IOException throws IOException
{ {
out.writeChar(((Character)object).charValue()); out.writeChar(((Character)object).charValue());
} }
// documentation inherited // documentation inherited
public void readObject ( public void readObject (Object object, ObjectInputStream in, boolean useReader)
Object object, ObjectInputStream in, boolean useReader)
throws IOException, ClassNotFoundException throws IOException, ClassNotFoundException
{ {
// nothing to do here // nothing to do here
@@ -201,16 +192,14 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void writeObject ( public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
Object object, ObjectOutputStream out, boolean useWriter)
throws IOException throws IOException
{ {
out.writeInt(((Integer)object).intValue()); out.writeInt(((Integer)object).intValue());
} }
// documentation inherited // documentation inherited
public void readObject ( public void readObject (Object object, ObjectInputStream in, boolean useReader)
Object object, ObjectInputStream in, boolean useReader)
throws IOException, ClassNotFoundException throws IOException, ClassNotFoundException
{ {
// nothing to do here // nothing to do here
@@ -228,16 +217,14 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void writeObject ( public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
Object object, ObjectOutputStream out, boolean useWriter)
throws IOException throws IOException
{ {
out.writeLong(((Long)object).longValue()); out.writeLong(((Long)object).longValue());
} }
// documentation inherited // documentation inherited
public void readObject ( public void readObject (Object object, ObjectInputStream in, boolean useReader)
Object object, ObjectInputStream in, boolean useReader)
throws IOException, ClassNotFoundException throws IOException, ClassNotFoundException
{ {
// nothing to do here // nothing to do here
@@ -255,16 +242,14 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void writeObject ( public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
Object object, ObjectOutputStream out, boolean useWriter)
throws IOException throws IOException
{ {
out.writeFloat(((Float)object).floatValue()); out.writeFloat(((Float)object).floatValue());
} }
// documentation inherited // documentation inherited
public void readObject ( public void readObject (Object object, ObjectInputStream in, boolean useReader)
Object object, ObjectInputStream in, boolean useReader)
throws IOException, ClassNotFoundException throws IOException, ClassNotFoundException
{ {
// nothing to do here // nothing to do here
@@ -282,16 +267,14 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void writeObject ( public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
Object object, ObjectOutputStream out, boolean useWriter)
throws IOException throws IOException
{ {
out.writeDouble(((Double)object).doubleValue()); out.writeDouble(((Double)object).doubleValue());
} }
// documentation inherited // documentation inherited
public void readObject ( public void readObject (Object object, ObjectInputStream in, boolean useReader)
Object object, ObjectInputStream in, boolean useReader)
throws IOException, ClassNotFoundException throws IOException, ClassNotFoundException
{ {
// nothing to do here // nothing to do here
@@ -309,16 +292,14 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void writeObject ( public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
Object object, ObjectOutputStream out, boolean useWriter)
throws IOException throws IOException
{ {
out.writeUTF((String)object); out.writeUTF((String)object);
} }
// documentation inherited // documentation inherited
public void readObject ( public void readObject (Object object, ObjectInputStream in, boolean useReader)
Object object, ObjectInputStream in, boolean useReader)
throws IOException, ClassNotFoundException throws IOException, ClassNotFoundException
{ {
// nothing to do here // nothing to do here
@@ -336,8 +317,7 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void writeObject ( public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
Object object, ObjectOutputStream out, boolean useWriter)
throws IOException throws IOException
{ {
boolean[] value = (boolean[])object; boolean[] value = (boolean[])object;
@@ -349,8 +329,7 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void readObject ( public void readObject (Object object, ObjectInputStream in, boolean useReader)
Object object, ObjectInputStream in, boolean useReader)
throws IOException, ClassNotFoundException throws IOException, ClassNotFoundException
{ {
boolean[] value = (boolean[])object; boolean[] value = (boolean[])object;
@@ -372,8 +351,7 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void writeObject ( public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
Object object, ObjectOutputStream out, boolean useWriter)
throws IOException throws IOException
{ {
byte[] value = (byte[])object; byte[] value = (byte[])object;
@@ -383,8 +361,7 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void readObject ( public void readObject (Object object, ObjectInputStream in, boolean useReader)
Object object, ObjectInputStream in, boolean useReader)
throws IOException, ClassNotFoundException throws IOException, ClassNotFoundException
{ {
byte[] value = (byte[])object; byte[] value = (byte[])object;
@@ -411,8 +388,7 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void writeObject ( public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
Object object, ObjectOutputStream out, boolean useWriter)
throws IOException throws IOException
{ {
short[] value = (short[])object; short[] value = (short[])object;
@@ -424,8 +400,7 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void readObject ( public void readObject (Object object, ObjectInputStream in, boolean useReader)
Object object, ObjectInputStream in, boolean useReader)
throws IOException, ClassNotFoundException throws IOException, ClassNotFoundException
{ {
short[] value = (short[])object; short[] value = (short[])object;
@@ -447,8 +422,7 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void writeObject ( public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
Object object, ObjectOutputStream out, boolean useWriter)
throws IOException throws IOException
{ {
char[] value = (char[])object; char[] value = (char[])object;
@@ -460,8 +434,7 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void readObject ( public void readObject (Object object, ObjectInputStream in, boolean useReader)
Object object, ObjectInputStream in, boolean useReader)
throws IOException, ClassNotFoundException throws IOException, ClassNotFoundException
{ {
char[] value = (char[])object; char[] value = (char[])object;
@@ -483,8 +456,7 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void writeObject ( public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
Object object, ObjectOutputStream out, boolean useWriter)
throws IOException throws IOException
{ {
int[] value = (int[])object; int[] value = (int[])object;
@@ -496,8 +468,7 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void readObject ( public void readObject (Object object, ObjectInputStream in, boolean useReader)
Object object, ObjectInputStream in, boolean useReader)
throws IOException, ClassNotFoundException throws IOException, ClassNotFoundException
{ {
int[] value = (int[])object; int[] value = (int[])object;
@@ -519,8 +490,7 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void writeObject ( public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
Object object, ObjectOutputStream out, boolean useWriter)
throws IOException throws IOException
{ {
long[] value = (long[])object; long[] value = (long[])object;
@@ -532,8 +502,7 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void readObject ( public void readObject (Object object, ObjectInputStream in, boolean useReader)
Object object, ObjectInputStream in, boolean useReader)
throws IOException, ClassNotFoundException throws IOException, ClassNotFoundException
{ {
long[] value = (long[])object; long[] value = (long[])object;
@@ -555,8 +524,7 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void writeObject ( public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
Object object, ObjectOutputStream out, boolean useWriter)
throws IOException throws IOException
{ {
float[] value = (float[])object; float[] value = (float[])object;
@@ -568,8 +536,7 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void readObject ( public void readObject (Object object, ObjectInputStream in, boolean useReader)
Object object, ObjectInputStream in, boolean useReader)
throws IOException, ClassNotFoundException throws IOException, ClassNotFoundException
{ {
float[] value = (float[])object; float[] value = (float[])object;
@@ -591,8 +558,7 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void writeObject ( public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
Object object, ObjectOutputStream out, boolean useWriter)
throws IOException throws IOException
{ {
double[] value = (double[])object; double[] value = (double[])object;
@@ -604,8 +570,7 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void readObject ( public void readObject (Object object, ObjectInputStream in, boolean useReader)
Object object, ObjectInputStream in, boolean useReader)
throws IOException, ClassNotFoundException throws IOException, ClassNotFoundException
{ {
double[] value = (double[])object; double[] value = (double[])object;
@@ -627,8 +592,7 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void writeObject ( public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
Object object, ObjectOutputStream out, boolean useWriter)
throws IOException throws IOException
{ {
Object[] value = (Object[])object; Object[] value = (Object[])object;
@@ -640,8 +604,7 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void readObject ( public void readObject (Object object, ObjectInputStream in, boolean useReader)
Object object, ObjectInputStream in, boolean useReader)
throws IOException, ClassNotFoundException throws IOException, ClassNotFoundException
{ {
Object[] value = (Object[])object; Object[] value = (Object[])object;
@@ -660,17 +623,15 @@ public class BasicStreamers
throws IOException throws IOException
{ {
return new ArrayList(0); // minimally sized return new ArrayList(0); // minimally sized
// (We thought about using a ThreadLocal to assist here... // (We thought about using a ThreadLocal to assist here... we could read the length of
// we could read the length of the list here and construct // the list here and construct the ArrayList with the right size, and then read the
// the ArrayList with the right size, and then read the // length out of the ThreadLocal down in readObject(). Instead, we simply create a
// length out of the ThreadLocal down in readObject(). Instead, // 0-length list here, which generates minimal garbage when we ensureCapacity() down in
// we simply create a 0-length list here, which generates // readObject.)
// minimal garbage when we ensureCapacity() down in readObject.)
} }
// documentation inherited // documentation inherited
public void writeObject ( public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
Object object, ObjectOutputStream out, boolean useWriter)
throws IOException throws IOException
{ {
List list = (List)object; List list = (List)object;
@@ -690,12 +651,10 @@ public class BasicStreamers
} }
// documentation inherited // documentation inherited
public void readObject ( public void readObject (Object object, ObjectInputStream in, boolean useReader)
Object object, ObjectInputStream in, boolean useReader)
throws IOException, ClassNotFoundException throws IOException, ClassNotFoundException
{ {
@SuppressWarnings("unchecked") ArrayList<Object> value = @SuppressWarnings("unchecked") ArrayList<Object> value = (ArrayList<Object>)object;
(ArrayList<Object>)object;
int ecount = in.readInt(); int ecount = in.readInt();
value.ensureCapacity(ecount); // resize the array once value.ensureCapacity(ecount); // resize the array once
for (int ii = 0; ii < ecount; ii++) { for (int ii = 0; ii < ecount; ii++) {
+41 -71
View File
@@ -31,23 +31,21 @@ import java.util.HashMap;
public abstract class FieldMarshaller public abstract class FieldMarshaller
{ {
/** /**
* Reads the contents of the supplied field from the supplied stream * Reads the contents of the supplied field from the supplied stream and sets it in the
* and sets it in the supplied object. * supplied object.
*/ */
public abstract void readField ( public abstract void readField (Field field, Object target, ObjectInputStream in)
Field field, Object target, ObjectInputStream in) throws Exception; throws Exception;
/** /**
* Writes the contents of the supplied field in the supplied object to * Writes the contents of the supplied field in the supplied object to the supplied stream.
* the supplied stream.
*/ */
public abstract void writeField ( public abstract void writeField (Field field, Object source, ObjectOutputStream out)
Field field, Object source, ObjectOutputStream out) throws Exception; throws Exception;
/** /**
* Returns a field marshaller appropriate for the supplied field or * Returns a field marshaller appropriate for the supplied field or null if no marshaller
* null if no marshaller exists for the type contained by the field in * exists for the type contained by the field in question.
* question.
*/ */
public static FieldMarshaller getFieldMarshaller (Field field) public static FieldMarshaller getFieldMarshaller (Field field)
{ {
@@ -64,8 +62,7 @@ public abstract class FieldMarshaller
// if we have an exact match, use that // if we have an exact match, use that
FieldMarshaller fm = _marshallers.get(ftype); FieldMarshaller fm = _marshallers.get(ftype);
// otherwise if the class is a streamable, use the streamable // otherwise if the class is a streamable, use the streamable marshaller
// marshaller
if (fm == null && Streamer.isStreamable(ftype)) { if (fm == null && Streamer.isStreamable(ftype)) {
fm = _marshallers.get(Streamable.class); fm = _marshallers.get(Streamable.class);
} }
@@ -74,8 +71,7 @@ public abstract class FieldMarshaller
} }
/** /**
* Used to marshall and unmarshall classes for which we have a basic * Used to marshall and unmarshall classes for which we have a basic {@link Streamer}.
* {@link Streamer}.
*/ */
protected static class StreamerMarshaller extends FieldMarshaller protected static class StreamerMarshaller extends FieldMarshaller
{ {
@@ -85,8 +81,7 @@ public abstract class FieldMarshaller
} }
// documentation inherited // documentation inherited
public void readField ( public void readField (Field field, Object target, ObjectInputStream in)
Field field, Object target, ObjectInputStream in)
throws Exception throws Exception
{ {
if (in.readBoolean()) { if (in.readBoolean()) {
@@ -99,8 +94,7 @@ public abstract class FieldMarshaller
} }
// documentation inherited // documentation inherited
public void writeField ( public void writeField (Field field, Object source, ObjectOutputStream out)
Field field, Object source, ObjectOutputStream out)
throws Exception throws Exception
{ {
Object value = field.get(source); Object value = field.get(source);
@@ -117,9 +111,8 @@ public abstract class FieldMarshaller
} }
/** /**
* Creates instances of all known field marshaller types and populates * Creates instances of all known field marshaller types and populates the {@link
* the {@link #_marshallers} table with them. This is called the first * #_marshallers} table with them. This is called the first time a marshaller is requested.
* time a marshaller is requested.
*/ */
protected static void createMarshallers () protected static void createMarshallers ()
{ {
@@ -128,132 +121,111 @@ public abstract class FieldMarshaller
// create a generic marshaller for streamable instances // create a generic marshaller for streamable instances
FieldMarshaller gmarsh = new FieldMarshaller() { FieldMarshaller gmarsh = new FieldMarshaller() {
public void readField ( public void readField (Field field, Object target, ObjectInputStream in)
Field field, Object target, ObjectInputStream in)
throws Exception { throws Exception {
System.err.println("Reading generic");
field.set(target, in.readObject()); field.set(target, in.readObject());
} }
public void writeField ( public void writeField (Field field, Object source, ObjectOutputStream out)
Field field, Object source, ObjectOutputStream out)
throws Exception { throws Exception {
out.writeObject(field.get(source)); out.writeObject(field.get(source));
} }
}; };
_marshallers.put(Streamable.class, gmarsh); _marshallers.put(Streamable.class, gmarsh);
// use the same generic marshaller for fields declared to by type // use the same generic marshaller for fields declared to by type Object or Object[] with
// Object or Object[] with the expectation that they will contain // the expectation that they will contain only primitive types or Streamables; the runtime
// only primitive types or Streamables; the runtime will fail // will fail informatively if we attempt to store non-Streamable objects in that field
// informatively if the user attempts to store non-Streamable
// objects in that field
_marshallers.put(Object.class, gmarsh); _marshallers.put(Object.class, gmarsh);
_marshallers.put(Object[].class, gmarsh); _marshallers.put(Object[].class, gmarsh);
// create marshallers for the primitive types // create marshallers for the primitive types
_marshallers.put(Boolean.TYPE, new FieldMarshaller() { _marshallers.put(Boolean.TYPE, new FieldMarshaller() {
public void readField ( public void readField (Field field, Object target, ObjectInputStream in)
Field field, Object target, ObjectInputStream in)
throws Exception { throws Exception {
field.setBoolean(target, in.readBoolean()); field.setBoolean(target, in.readBoolean());
} }
public void writeField ( public void writeField (Field field, Object source, ObjectOutputStream out)
Field field, Object source, ObjectOutputStream out)
throws Exception { throws Exception {
out.writeBoolean(field.getBoolean(source)); out.writeBoolean(field.getBoolean(source));
} }
}); });
_marshallers.put(Byte.TYPE, new FieldMarshaller() { _marshallers.put(Byte.TYPE, new FieldMarshaller() {
public void readField ( public void readField (Field field, Object target, ObjectInputStream in)
Field field, Object target, ObjectInputStream in)
throws Exception { throws Exception {
field.setByte(target, in.readByte()); field.setByte(target, in.readByte());
} }
public void writeField ( public void writeField (Field field, Object source, ObjectOutputStream out)
Field field, Object source, ObjectOutputStream out)
throws Exception { throws Exception {
out.writeByte(field.getByte(source)); out.writeByte(field.getByte(source));
} }
}); });
_marshallers.put(Character.TYPE, new FieldMarshaller() { _marshallers.put(Character.TYPE, new FieldMarshaller() {
public void readField ( public void readField (Field field, Object target, ObjectInputStream in)
Field field, Object target, ObjectInputStream in)
throws Exception { throws Exception {
field.setChar(target, in.readChar()); field.setChar(target, in.readChar());
} }
public void writeField ( public void writeField (Field field, Object source, ObjectOutputStream out)
Field field, Object source, ObjectOutputStream out)
throws Exception { throws Exception {
out.writeChar(field.getChar(source)); out.writeChar(field.getChar(source));
} }
}); });
_marshallers.put(Short.TYPE, new FieldMarshaller() { _marshallers.put(Short.TYPE, new FieldMarshaller() {
public void readField ( public void readField (Field field, Object target, ObjectInputStream in)
Field field, Object target, ObjectInputStream in)
throws Exception { throws Exception {
field.setShort(target, in.readShort()); field.setShort(target, in.readShort());
} }
public void writeField ( public void writeField (Field field, Object source, ObjectOutputStream out)
Field field, Object source, ObjectOutputStream out)
throws Exception { throws Exception {
out.writeShort(field.getShort(source)); out.writeShort(field.getShort(source));
} }
}); });
_marshallers.put(Integer.TYPE, new FieldMarshaller() { _marshallers.put(Integer.TYPE, new FieldMarshaller() {
public void readField ( public void readField (Field field, Object target, ObjectInputStream in)
Field field, Object target, ObjectInputStream in)
throws Exception { throws Exception {
field.setInt(target, in.readInt()); field.setInt(target, in.readInt());
} }
public void writeField ( public void writeField (Field field, Object source, ObjectOutputStream out)
Field field, Object source, ObjectOutputStream out)
throws Exception { throws Exception {
out.writeInt(field.getInt(source)); out.writeInt(field.getInt(source));
} }
}); });
_marshallers.put(Long.TYPE, new FieldMarshaller() { _marshallers.put(Long.TYPE, new FieldMarshaller() {
public void readField ( public void readField (Field field, Object target, ObjectInputStream in)
Field field, Object target, ObjectInputStream in)
throws Exception { throws Exception {
field.setLong(target, in.readLong()); field.setLong(target, in.readLong());
} }
public void writeField ( public void writeField (Field field, Object source, ObjectOutputStream out)
Field field, Object source, ObjectOutputStream out)
throws Exception { throws Exception {
out.writeLong(field.getLong(source)); out.writeLong(field.getLong(source));
} }
}); });
_marshallers.put(Float.TYPE, new FieldMarshaller() { _marshallers.put(Float.TYPE, new FieldMarshaller() {
public void readField ( public void readField (Field field, Object target, ObjectInputStream in)
Field field, Object target, ObjectInputStream in)
throws Exception { throws Exception {
field.setFloat(target, in.readFloat()); field.setFloat(target, in.readFloat());
} }
public void writeField ( public void writeField (Field field, Object source, ObjectOutputStream out)
Field field, Object source, ObjectOutputStream out)
throws Exception { throws Exception {
out.writeFloat(field.getFloat(source)); out.writeFloat(field.getFloat(source));
} }
}); });
_marshallers.put(Double.TYPE, new FieldMarshaller() { _marshallers.put(Double.TYPE, new FieldMarshaller() {
public void readField ( public void readField (Field field, Object target, ObjectInputStream in)
Field field, Object target, ObjectInputStream in)
throws Exception { throws Exception {
field.setDouble(target, in.readDouble()); field.setDouble(target, in.readDouble());
} }
public void writeField ( public void writeField (Field field, Object source, ObjectOutputStream out)
Field field, Object source, ObjectOutputStream out)
throws Exception { throws Exception {
out.writeDouble(field.getDouble(source)); out.writeDouble(field.getDouble(source));
} }
}); });
_marshallers.put(Date.class, new FieldMarshaller() { _marshallers.put(Date.class, new FieldMarshaller() {
public void readField ( public void readField (Field field, Object target, ObjectInputStream in)
Field field, Object target, ObjectInputStream in)
throws Exception { throws Exception {
field.set(target, new Date(in.readLong())); field.set(target, new Date(in.readLong()));
} }
public void writeField ( public void writeField (Field field, Object source, ObjectOutputStream out)
Field field, Object source, ObjectOutputStream out)
throws Exception { throws Exception {
out.writeLong(((Date)field.get(source)).getTime()); out.writeLong(((Date)field.get(source)).getTime());
} }
@@ -263,12 +235,10 @@ public abstract class FieldMarshaller
int bscount = BasicStreamers.BSTREAMER_TYPES.length; int bscount = BasicStreamers.BSTREAMER_TYPES.length;
for (int ii = 0; ii < bscount; ii++) { for (int ii = 0; ii < bscount; ii++) {
_marshallers.put(BasicStreamers.BSTREAMER_TYPES[ii], _marshallers.put(BasicStreamers.BSTREAMER_TYPES[ii],
new StreamerMarshaller( new StreamerMarshaller(BasicStreamers.BSTREAMER_INSTANCES[ii]));
BasicStreamers.BSTREAMER_INSTANCES[ii]));
} }
} }
/** Contains a mapping from field type to field marshaller instance /** Contains a mapping from field type to field marshaller instance for that type. */
* for that type. */
protected static HashMap<Class,FieldMarshaller> _marshallers; protected static HashMap<Class,FieldMarshaller> _marshallers;
} }
@@ -141,26 +141,28 @@ public class ObjectInputStream extends DataInputStream
// sanity check // sanity check
if (cmap == null) { if (cmap == null) {
// this will help with debugging // this will help with debugging
log.warning("Internal stream error, no class metadata " + log.warning("Internal stream error, no class metadata [code=" + code +
"[code=" + code + ", ois=" + this + "]."); ", ois=" + this + "].");
Thread.dumpStack(); Thread.dumpStack();
log.warning("ObjectInputStream mappings " + log.warning(
StringUtil.toString(_classmap) + "ObjectInputStream mappings " + StringUtil.toString(_classmap) + ".");
".");
String errmsg = "Read object code for which we " + String errmsg = "Read object code for which we " +
"have no registered class metadata [code=" + code + "]"; "have no registered class metadata [code=" + code + "]";
throw new RuntimeException(errmsg); throw new RuntimeException(errmsg);
} }
} }
if (STREAM_DEBUG) {
log.info(hashCode() + ": Reading with " + cmap.streamer + ".");
}
// create an instance of the appropriate object // create an instance of the appropriate object
Object target = cmap.streamer.createObject(this); Object target = cmap.streamer.createObject(this);
readBareObject(target, cmap.streamer, true); readBareObject(target, cmap.streamer, true);
return target; return target;
} catch (OutOfMemoryError oome) { } catch (OutOfMemoryError oome) {
throw (IOException) throw (IOException)new IOException("Malformed object data").initCause(oome);
new IOException("Malformed object data").initCause(oome);
} }
} }
+94 -134
View File
@@ -43,18 +43,16 @@ import com.samskivert.util.StringUtil;
import static com.threerings.NaryaLog.log; import static com.threerings.NaryaLog.log;
/** /**
* Handles the streaming of {@link Streamable} instances as well as a set * Handles the streaming of {@link Streamable} instances as well as a set of basic object types
* of basic object types (see {@link ObjectOutputStream}). An instance of * (see {@link ObjectOutputStream}). An instance of {@link Streamer} is created for each distinct
* {@link Streamer} is created for each distinct class that implements * class that implements {@link Streamable}. The {@link Streamer} reflects on the streamed class
* {@link Streamable}. The {@link Streamer} reflects on the streamed class * and caches the information necessary to efficiently read and write objects of the class in
* and caches the information necessary to efficiently read and write * question.
* objects of the class in question.
*/ */
public class Streamer public class Streamer
{ {
/** /**
* Returns true if the supplied target class can be streamed using a * Returns true if the supplied target class can be streamed using a streamer.
* streamer.
*/ */
public synchronized static boolean isStreamable (Class target) public synchronized static boolean isStreamable (Class target)
{ {
@@ -74,17 +72,15 @@ public class Streamer
return Streamable.class.isAssignableFrom(target); return Streamable.class.isAssignableFrom(target);
} }
// otherwise extract the component type and loop back around for // otherwise extract the component type and loop back around for another check...
// another check...
target = target.getComponentType(); target = target.getComponentType();
} }
} }
/** /**
* Returns the class that should be used when streaming this object. In * Returns the class that should be used when streaming this object. In general that is the
* general that is the object's natural class, but for enum values, that * object's natural class, but for enum values, that might be its declaring class as enums use
* might be its declaring class as enums use classes in a way that would * classes in a way that would otherwise pollute our id to class mapping space.
* otherwise pollute our id to class mapping space.
*/ */
public static Class getStreamerClass (Object object) public static Class getStreamerClass (Object object)
{ {
@@ -93,18 +89,16 @@ public class Streamer
} }
/** /**
* Obtains a {@link Streamer} that can be used to read and write * Obtains a {@link Streamer} that can be used to read and write objects of the specified
* objects of the specified target class. {@link Streamer} instances * target class. {@link Streamer} instances are shared among all {@link ObjectInputStream}s and
* are shared among all {@link ObjectInputStream}s and {@link * {@link ObjectOutputStream}s.
* ObjectOutputStream}s.
* *
* @param target the class that is desired to be streamed. This should be * @param target the class that is desired to be streamed. This should be the result of a call
* the result of a call to {@link #getStreamerClass} if the caller has an * to {@link #getStreamerClass} if the caller has an instance they wish to stream.
* instance they wish to stream.
* *
* @throws IOException when a streamer is requested for an object that * @throws IOException when a streamer is requested for an object that does not implement
* does not implement {@link Streamable} and is not one of the basic * {@link Streamable} and is not one of the basic object types (@see {@link
* object types (@see {@link ObjectOutputStream}). * ObjectOutputStream}).
*/ */
public synchronized static Streamer getStreamer (final Class target) public synchronized static Streamer getStreamer (final Class target)
throws IOException throws IOException
@@ -128,15 +122,14 @@ public class Streamer
log.info("Creating a streamer for '" + target.getName() + "'."); log.info("Creating a streamer for '" + target.getName() + "'.");
} }
// create our streamer in a privileged block so that it can // create our streamer in a privileged block so that it can introspect on the to be
// introspect on the to be streamed class // streamed class
try { try {
stream = AccessController.doPrivileged( stream = AccessController.doPrivileged(new PrivilegedExceptionAction<Streamer>() {
new PrivilegedExceptionAction<Streamer>() { public Streamer run () throws IOException {
public Streamer run () throws IOException { return new Streamer(target);
return new Streamer(target); }
} });
});
} catch (PrivilegedActionException pae) { } catch (PrivilegedActionException pae) {
throw (IOException) pae.getCause(); throw (IOException) pae.getCause();
} }
@@ -150,19 +143,16 @@ public class Streamer
* *
* @param object the instance to be written to the stream. * @param object the instance to be written to the stream.
* @param out the stream to which to write the instance. * @param out the stream to which to write the instance.
* @param useWriter whether or not to use the custom * @param useWriter whether or not to use the custom <code>writeObject</code> if one exists.
* <code>writeObject</code> if one exists.
*/ */
public void writeObject ( public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
Object object, ObjectOutputStream out, boolean useWriter)
throws IOException throws IOException
{ {
// if we're supposed to and one exists, use the writer method // if we're supposed to and one exists, use the writer method
if (useWriter && _writer != null) { if (useWriter && _writer != null) {
try { try {
if (ObjectInputStream.STREAM_DEBUG) { if (ObjectInputStream.STREAM_DEBUG) {
log.info("Writing with writer " + log.info("Writing with writer [class=" + _target.getName() + "].");
"[class=" + _target.getName() + "].");
} }
_writer.invoke(object, new Object[] { out }); _writer.invoke(object, new Object[] { out });
@@ -184,13 +174,12 @@ public class Streamer
if (_target.isArray()) { if (_target.isArray()) {
int length = Array.getLength(object); int length = Array.getLength(object);
out.writeInt(length); out.writeInt(length);
// if the component class is final, we can be sure that all
// instances in the array will be of the same class and thus // if the component class is final, we can be sure that all instances in the array will
// can serialize things more efficiently // be of the same class and thus can serialize things more efficiently
int cmods = _target.getComponentType().getModifiers(); int cmods = _target.getComponentType().getModifiers();
if ((cmods & Modifier.FINAL) != 0) { if (Modifier.isFinal(cmods)) {
// compute a mask indicating which elements are null and // compute a mask indicating which elements are null and which are populated
// which are populated
ArrayMask mask = new ArrayMask(length); ArrayMask mask = new ArrayMask(length);
for (int ii = 0; ii < length; ii++) { for (int ii = 0; ii < length; ii++) {
if (Array.get(object, ii) != null) { if (Array.get(object, ii) != null) {
@@ -199,6 +188,7 @@ public class Streamer
} }
// write that mask out to the stream // write that mask out to the stream
mask.writeTo(out); mask.writeTo(out);
// now write out the populated elements // now write out the populated elements
for (int ii = 0; ii < length; ii++) { for (int ii = 0; ii < length; ii++) {
Object element = Array.get(object, ii); Object element = Array.get(object, ii);
@@ -208,9 +198,8 @@ public class Streamer
} }
} else { } else {
// otherwise we've got to write each array element with // otherwise we've got to write each array element with its own class identifier
// its own class identifier because it could be any // because it could be any derived class of the array element type
// derived class of the array element type
for (int ii = 0; ii < length; ii++) { for (int ii = 0; ii < length; ii++) {
out.writeObject(Array.get(object, ii)); out.writeObject(Array.get(object, ii));
} }
@@ -218,9 +207,9 @@ public class Streamer
return; return;
} }
// if we're writing an enum; write its string value (to avoid future // if we're writing an enum; write its string value (to avoid future compatibility issues
// compatibility issues if someone serializes an enum to a file and // if someone serializes an enum to a file and then adds a value to the enum, changing the
// then adds a value to the enum, changing the ordinal assignments) // ordinal assignments)
if (_target.isEnum()) { if (_target.isEnum()) {
out.writeUTF(((Enum)object).name()); out.writeUTF(((Enum)object).name());
return; return;
@@ -232,10 +221,8 @@ public class Streamer
Field field = _fields[ii]; Field field = _fields[ii];
FieldMarshaller fm = _marshallers[ii]; FieldMarshaller fm = _marshallers[ii];
if (fm == null) { if (fm == null) {
String errmsg = "Unable to marshall field " + String errmsg = "Unable to marshall field [class=" + _target.getName() +
"[class=" + _target.getName() + ", field=" + field.getName() + ", type=" + field.getType().getName() + "]";
", field=" + field.getName() +
", type=" + field.getType().getName() + "]";
throw new IOException(errmsg); throw new IOException(errmsg);
} }
try { try {
@@ -245,8 +232,7 @@ public class Streamer
} }
fm.writeField(field, object, out); fm.writeField(field, object, out);
} catch (Exception e) { } catch (Exception e) {
String errmsg = "Failure writing streamable field " + String errmsg = "Failure writing streamable field [class=" + _target.getName() +
"[class=" + _target.getName() +
", field=" + field.getName() + "]"; ", field=" + field.getName() + "]";
throw (IOException) new IOException(errmsg).initCause(e); throw (IOException) new IOException(errmsg).initCause(e);
} }
@@ -254,75 +240,63 @@ public class Streamer
} }
/** /**
* Creates a blank object that can subsequently be read by this * Creates a blank object that can subsequently be read by this streamer. Data may be read
* streamer. Data may be read from the input stream as a result of * from the input stream as a result of this method (in the case of arrays, the length of the
* this method (in the case of arrays, the length of the array must be * array must be read before creating the array).
* read before creating the array).
*/ */
public Object createObject (ObjectInputStream in) public Object createObject (ObjectInputStream in)
throws IOException throws IOException
{ {
try { try {
// if our target class is an array type, read in the element // if our target class is an array type, read in the element count and create an array
// count and create an array instance of the appropriate type // instance of the appropriate type and size
// and size
if (_target.isArray()) { if (_target.isArray()) {
int length = in.readInt(); int length = in.readInt();
if (ObjectInputStream.STREAM_DEBUG) { if (ObjectInputStream.STREAM_DEBUG) {
log.info(in.hashCode() + ": Creating array '" + log.info(in.hashCode() + ": Creating array '" +
_target.getComponentType().getName() + _target.getComponentType().getName() + "[" + length + "]'.");
"[" + length + "]'.");
} }
return Array.newInstance(_target.getComponentType(), length); return Array.newInstance(_target.getComponentType(), length);
} else if (_target.isEnum()) { } else if (_target.isEnum()) {
if (ObjectInputStream.STREAM_DEBUG) { if (ObjectInputStream.STREAM_DEBUG) {
log.info(in.hashCode() + ": Creating enum '" + log.info(in.hashCode() + ": Creating enum '" + _target.getName() + "'.");
_target.getName() + "'.");
} }
@SuppressWarnings("unchecked") Object value = @SuppressWarnings("unchecked") Object value = Enum.valueOf(_target, in.readUTF());
Enum.valueOf(_target, in.readUTF());
return value; return value;
} else { } else {
if (ObjectInputStream.STREAM_DEBUG) { if (ObjectInputStream.STREAM_DEBUG) {
log.info(in.hashCode() + ": Creating object '" + log.info(in.hashCode() + ": Creating object '" + _target.getName() + "'.");
_target.getName() + "'.");
} }
return _target.newInstance(); return _target.newInstance();
} }
} catch (InstantiationException ie) { } catch (InstantiationException ie) {
String errmsg = "Error instantiating object " + String errmsg = "Error instantiating object [type=" + _target.getName() + "]";
"[type=" + _target.getName() + "]";
throw (IOException) new IOException(errmsg).initCause(ie); throw (IOException) new IOException(errmsg).initCause(ie);
} catch (IllegalAccessException iae) { } catch (IllegalAccessException iae) {
String errmsg = "Error instantiating object " + String errmsg = "Error instantiating object [type=" + _target.getName() + "]";
"[type=" + _target.getName() + "]";
throw (IOException) new IOException(errmsg).initCause(iae); throw (IOException) new IOException(errmsg).initCause(iae);
} }
} }
/** /**
* Reads and populates the fields of the supplied object from the * Reads and populates the fields of the supplied object from the specified stream.
* specified stream.
* *
* @param object the instance to be read from the stream. * @param object the instance to be read from the stream.
* @param in the stream from which to read the instance. * @param in the stream from which to read the instance.
* @param useReader whether or not to use the custom * @param useReader whether or not to use the custom <code>readObject</code> if one exists.
* <code>readObject</code> if one exists.
*/ */
public void readObject ( public void readObject (Object object, ObjectInputStream in, boolean useReader)
Object object, ObjectInputStream in, boolean useReader)
throws IOException, ClassNotFoundException throws IOException, ClassNotFoundException
{ {
// if we're supposed to and one exists, use the reader method // if we're supposed to and one exists, use the reader method
if (useReader && _reader != null) { if (useReader && _reader != null) {
try { try {
if (ObjectInputStream.STREAM_DEBUG) { if (ObjectInputStream.STREAM_DEBUG) {
log.info(in.hashCode() + ": Reading with reader '" + log.info(in.hashCode() + ": Reading with reader '" + _target.getName() + "." +
_target.getName() + "." +
_reader.getName() + "()'."); _reader.getName() + "()'.");
} }
_reader.invoke(object, new Object[] { in }); _reader.invoke(object, new Object[] { in });
@@ -348,28 +322,24 @@ public class Streamer
// if we're reading in an array, do some special business // if we're reading in an array, do some special business
if (_target.isArray()) { if (_target.isArray()) {
int length = Array.getLength(object); int length = Array.getLength(object);
// if the component class is final, we can be sure that all // if the component class is final, we can be sure that all instances in the array will
// instances in the array will be of the same class and thus // be of the same class and thus have serialized things more efficiently
// have serialized things more efficiently
int cmods = _target.getComponentType().getModifiers(); int cmods = _target.getComponentType().getModifiers();
if ((cmods & Modifier.FINAL) != 0) { if (Modifier.isFinal(cmods)) {
// read in the nullness mask // read in the nullness mask
ArrayMask mask = new ArrayMask(); ArrayMask mask = new ArrayMask();
mask.readFrom(in); mask.readFrom(in);
// now read in the array elements given that we know which // now read in the array elements given that we know which elements to read
// elements to read
for (int ii = 0; ii < length; ii++) { for (int ii = 0; ii < length; ii++) {
if (mask.isSet(ii)) { if (mask.isSet(ii)) {
if (ObjectInputStream.STREAM_DEBUG) { if (ObjectInputStream.STREAM_DEBUG) {
log.info(in.hashCode() + log.info(in.hashCode() + ": Reading fixed element '" + ii + "'.");
": Reading fixed element '" + ii + "'.");
} }
Object element = _delegate.createObject(in); Object element = _delegate.createObject(in);
in.readBareObject(element, _delegate, useReader); in.readBareObject(element, _delegate, useReader);
Array.set(object, ii, element); Array.set(object, ii, element);
} else if (ObjectInputStream.STREAM_DEBUG) { } else if (ObjectInputStream.STREAM_DEBUG) {
log.info(in.hashCode() + log.info(in.hashCode() + ": Skipping null element '" + ii + "'.");
": Skipping null element '" + ii + "'.");
} }
} }
@@ -377,8 +347,7 @@ public class Streamer
// otherwise we had to write each object out individually // otherwise we had to write each object out individually
for (int ii = 0; ii < length; ii++) { for (int ii = 0; ii < length; ii++) {
if (ObjectInputStream.STREAM_DEBUG) { if (ObjectInputStream.STREAM_DEBUG) {
log.info(in.hashCode() + log.info(in.hashCode() + ": Reading free element '" + ii + "'.");
": Reading free element '" + ii + "'.");
} }
Array.set(object, ii, in.readObject()); Array.set(object, ii, in.readObject());
} }
@@ -397,31 +366,27 @@ public class Streamer
Field field = _fields[ii]; Field field = _fields[ii];
FieldMarshaller fm = _marshallers[ii]; FieldMarshaller fm = _marshallers[ii];
if (fm == null) { if (fm == null) {
String errmsg = "Unable to marshall field " + String errmsg = "Unable to marshall field [class=" + _target.getName() +
"[class=" + _target.getName() + ", field=" + field.getName() + ", type=" + field.getType().getName() + "]";
", field=" + field.getName() +
", type=" + field.getType().getName() + "]";
throw new IOException(errmsg); throw new IOException(errmsg);
} }
try { try {
if (ObjectInputStream.STREAM_DEBUG) { if (ObjectInputStream.STREAM_DEBUG) {
log.info(in.hashCode() + log.info(in.hashCode() + ": Reading field '" + field.getName() + "'.");
": Reading field '" + field.getName() + "'.");
} }
// gracefully deal with objects that have had new fields // gracefully deal with objects that have had new fields added to their class
// added to their class definition // definition
if (in.available() > 0) { if (in.available() > 0) {
log.info("Using " + fm);
fm.readField(field, object, in); fm.readField(field, object, in);
} else { } else {
log.info("Streamed instance missing field (probably " + log.info("Streamed instance missing field (probably newly added) " +
"newly added) [class=" + _target.getName() + "[class=" + _target.getName() + ", field=" + field.getName() + "].");
", field=" + field.getName() + "].");
} }
} catch (Exception e) { } catch (Exception e) {
String errmsg = "Failure reading streamable field " + e.printStackTrace(System.err);
"[class=" + _target.getName() + String errmsg = "Failure reading streamable field [class=" + _target.getName() +
", field=" + field.getName() + ", field=" + field.getName() + ", error=" + e + "]";
", error=" + e + "]";
throw (IOException) new IOException(errmsg).initCause(e); throw (IOException) new IOException(errmsg).initCause(e);
} }
} }
@@ -436,9 +401,10 @@ public class Streamer
*/ */
public String toString () public String toString ()
{ {
return "[target=" + _target.getName() + ", delegate=" + _delegate + return (_target == null) ? getClass().getName() :
", fcount=" + _fields.length + ", reader=" + _reader + "[target=" + _target.getName() + ", delegate=" + _delegate +
", writer=" + _writer + "]"; ", fcount=" + (_fields == null ? 0 : _fields.length) +
", reader=" + _reader + ", writer=" + _writer + "]";
} }
/** /**
@@ -457,15 +423,14 @@ public class Streamer
// keep a handle on the class // keep a handle on the class
_target = target; _target = target;
// if our target class is an array, we want to get a handle on a // if our target class is an array, we want to get a handle on a streamer delegate that
// streamer delegate that we'll use to stream our elements // we'll use to stream our elements
if (_target.isArray()) { if (_target.isArray()) {
_delegate = Streamer.getStreamer(_target.getComponentType()); _delegate = Streamer.getStreamer(_target.getComponentType());
// sanity check // sanity check
if (_delegate == null) { if (_delegate == null) {
String errmsg = "Aiya! Streamer created for array type " + String errmsg = "Aiya! Streamer created for array type but we have no registered " +
"but we have no registered streamer for the element " + "streamer for the element type [type=" + _target.getName() + "]";
"type [type=" + _target.getName() + "]";
throw new RuntimeException(errmsg); throw new RuntimeException(errmsg);
} }
// and that's all we'll need // and that's all we'll need
@@ -481,8 +446,8 @@ public class Streamer
for (int ii = 0; ii < fcount; ii++) { for (int ii = 0; ii < fcount; ii++) {
_marshallers[ii] = FieldMarshaller.getFieldMarshaller(_fields[ii]); _marshallers[ii] = FieldMarshaller.getFieldMarshaller(_fields[ii]);
if (ObjectInputStream.STREAM_DEBUG) { if (ObjectInputStream.STREAM_DEBUG) {
log.info("Using " + _marshallers[ii] + " for " + log.info("Using " + _marshallers[ii] + " for " + _target.getName() + "." +
_target.getName() + "." + _fields[ii].getName() + "."); _fields[ii].getName() + ".");
} }
} }
@@ -500,8 +465,7 @@ public class Streamer
} }
/** /**
* Creates our streamers table and registers streamers for all of the * Creates our streamers table and registers streamers for all of the basic types.
* basic types.
*/ */
protected static void createStreamers () protected static void createStreamers ()
{ {
@@ -518,28 +482,24 @@ public class Streamer
/** The class for which this streamer instance is configured. */ /** The class for which this streamer instance is configured. */
protected Class _target; protected Class _target;
/** If our target class is an array, this is a reference to a streamer /** If our target class is an array, this is a reference to a streamer that can stream our
* that can stream our array elements, otherwise it is null. */ * array elements, otherwise it is null. */
protected Streamer _delegate; protected Streamer _delegate;
/** The non-transient, non-static public fields that we will stream /** The non-transient, non-static public fields that we will stream when requested. */
* when requested. */
protected Field[] _fields; protected Field[] _fields;
/** Field marshallers for each field that will be read or written in /** Field marshallers for each field that will be read or written in our objects. */
* our objects. */
protected FieldMarshaller[] _marshallers; protected FieldMarshaller[] _marshallers;
/** A reference to the <code>readObject</code> method if one is /** A reference to the <code>readObject</code> method if one is defined by our target class. */
* defined by our target class. */
protected Method _reader; protected Method _reader;
/** A reference to the <code>writeObject</code> method if one is /** A reference to the <code>writeObject</code> method if one is defined by our target
* defined by our target class. */ * class. */
protected Method _writer; protected Method _writer;
/** Contains the mapping from class names to configured streamer /** Contains the mapping from class names to configured streamer instances. */
* instances. */
protected static HashMap<Class,Streamer> _streamers; protected static HashMap<Class,Streamer> _streamers;
/** The name of the custom reader method. */ /** The name of the custom reader method. */