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:
@@ -31,23 +31,21 @@ import java.util.HashMap;
|
||||
public abstract class FieldMarshaller
|
||||
{
|
||||
/**
|
||||
* Reads the contents of the supplied field from the supplied stream
|
||||
* and sets it in the supplied object.
|
||||
* Reads the contents of the supplied field from the supplied stream and sets it in the
|
||||
* supplied object.
|
||||
*/
|
||||
public abstract void readField (
|
||||
Field field, Object target, ObjectInputStream in) throws Exception;
|
||||
public abstract void readField (Field field, Object target, ObjectInputStream in)
|
||||
throws Exception;
|
||||
|
||||
/**
|
||||
* Writes the contents of the supplied field in the supplied object to
|
||||
* the supplied stream.
|
||||
* Writes the contents of the supplied field in the supplied object to the supplied stream.
|
||||
*/
|
||||
public abstract void writeField (
|
||||
Field field, Object source, ObjectOutputStream out) throws Exception;
|
||||
public abstract void writeField (Field field, Object source, ObjectOutputStream out)
|
||||
throws Exception;
|
||||
|
||||
/**
|
||||
* Returns a field marshaller appropriate for the supplied field or
|
||||
* null if no marshaller exists for the type contained by the field in
|
||||
* question.
|
||||
* Returns a field marshaller appropriate for the supplied field or null if no marshaller
|
||||
* exists for the type contained by the field in question.
|
||||
*/
|
||||
public static FieldMarshaller getFieldMarshaller (Field field)
|
||||
{
|
||||
@@ -64,8 +62,7 @@ public abstract class FieldMarshaller
|
||||
// if we have an exact match, use that
|
||||
FieldMarshaller fm = _marshallers.get(ftype);
|
||||
|
||||
// otherwise if the class is a streamable, use the streamable
|
||||
// marshaller
|
||||
// otherwise if the class is a streamable, use the streamable marshaller
|
||||
if (fm == null && Streamer.isStreamable(ftype)) {
|
||||
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
|
||||
* {@link Streamer}.
|
||||
* Used to marshall and unmarshall classes for which we have a basic {@link Streamer}.
|
||||
*/
|
||||
protected static class StreamerMarshaller extends FieldMarshaller
|
||||
{
|
||||
@@ -85,8 +81,7 @@ public abstract class FieldMarshaller
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public void readField (
|
||||
Field field, Object target, ObjectInputStream in)
|
||||
public void readField (Field field, Object target, ObjectInputStream in)
|
||||
throws Exception
|
||||
{
|
||||
if (in.readBoolean()) {
|
||||
@@ -99,8 +94,7 @@ public abstract class FieldMarshaller
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public void writeField (
|
||||
Field field, Object source, ObjectOutputStream out)
|
||||
public void writeField (Field field, Object source, ObjectOutputStream out)
|
||||
throws Exception
|
||||
{
|
||||
Object value = field.get(source);
|
||||
@@ -117,9 +111,8 @@ public abstract class FieldMarshaller
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates instances of all known field marshaller types and populates
|
||||
* the {@link #_marshallers} table with them. This is called the first
|
||||
* time a marshaller is requested.
|
||||
* Creates instances of all known field marshaller types and populates the {@link
|
||||
* #_marshallers} table with them. This is called the first time a marshaller is requested.
|
||||
*/
|
||||
protected static void createMarshallers ()
|
||||
{
|
||||
@@ -128,132 +121,111 @@ public abstract class FieldMarshaller
|
||||
|
||||
// create a generic marshaller for streamable instances
|
||||
FieldMarshaller gmarsh = new FieldMarshaller() {
|
||||
public void readField (
|
||||
Field field, Object target, ObjectInputStream in)
|
||||
public void readField (Field field, Object target, ObjectInputStream in)
|
||||
throws Exception {
|
||||
System.err.println("Reading generic");
|
||||
field.set(target, in.readObject());
|
||||
}
|
||||
public void writeField (
|
||||
Field field, Object source, ObjectOutputStream out)
|
||||
public void writeField (Field field, Object source, ObjectOutputStream out)
|
||||
throws Exception {
|
||||
out.writeObject(field.get(source));
|
||||
}
|
||||
};
|
||||
_marshallers.put(Streamable.class, gmarsh);
|
||||
|
||||
// use the same generic marshaller for fields declared to by type
|
||||
// Object or Object[] with the expectation that they will contain
|
||||
// only primitive types or Streamables; the runtime will fail
|
||||
// informatively if the user attempts to store non-Streamable
|
||||
// objects in that field
|
||||
// use the same generic marshaller for fields declared to by type Object or Object[] with
|
||||
// the expectation that they will contain only primitive types or Streamables; the runtime
|
||||
// will fail informatively if we attempt to store non-Streamable objects in that field
|
||||
_marshallers.put(Object.class, gmarsh);
|
||||
_marshallers.put(Object[].class, gmarsh);
|
||||
|
||||
// create marshallers for the primitive types
|
||||
_marshallers.put(Boolean.TYPE, new FieldMarshaller() {
|
||||
public void readField (
|
||||
Field field, Object target, ObjectInputStream in)
|
||||
public void readField (Field field, Object target, ObjectInputStream in)
|
||||
throws Exception {
|
||||
field.setBoolean(target, in.readBoolean());
|
||||
}
|
||||
public void writeField (
|
||||
Field field, Object source, ObjectOutputStream out)
|
||||
public void writeField (Field field, Object source, ObjectOutputStream out)
|
||||
throws Exception {
|
||||
out.writeBoolean(field.getBoolean(source));
|
||||
}
|
||||
});
|
||||
_marshallers.put(Byte.TYPE, new FieldMarshaller() {
|
||||
public void readField (
|
||||
Field field, Object target, ObjectInputStream in)
|
||||
public void readField (Field field, Object target, ObjectInputStream in)
|
||||
throws Exception {
|
||||
field.setByte(target, in.readByte());
|
||||
}
|
||||
public void writeField (
|
||||
Field field, Object source, ObjectOutputStream out)
|
||||
public void writeField (Field field, Object source, ObjectOutputStream out)
|
||||
throws Exception {
|
||||
out.writeByte(field.getByte(source));
|
||||
}
|
||||
});
|
||||
_marshallers.put(Character.TYPE, new FieldMarshaller() {
|
||||
public void readField (
|
||||
Field field, Object target, ObjectInputStream in)
|
||||
public void readField (Field field, Object target, ObjectInputStream in)
|
||||
throws Exception {
|
||||
field.setChar(target, in.readChar());
|
||||
}
|
||||
public void writeField (
|
||||
Field field, Object source, ObjectOutputStream out)
|
||||
public void writeField (Field field, Object source, ObjectOutputStream out)
|
||||
throws Exception {
|
||||
out.writeChar(field.getChar(source));
|
||||
}
|
||||
});
|
||||
_marshallers.put(Short.TYPE, new FieldMarshaller() {
|
||||
public void readField (
|
||||
Field field, Object target, ObjectInputStream in)
|
||||
public void readField (Field field, Object target, ObjectInputStream in)
|
||||
throws Exception {
|
||||
field.setShort(target, in.readShort());
|
||||
}
|
||||
public void writeField (
|
||||
Field field, Object source, ObjectOutputStream out)
|
||||
public void writeField (Field field, Object source, ObjectOutputStream out)
|
||||
throws Exception {
|
||||
out.writeShort(field.getShort(source));
|
||||
}
|
||||
});
|
||||
_marshallers.put(Integer.TYPE, new FieldMarshaller() {
|
||||
public void readField (
|
||||
Field field, Object target, ObjectInputStream in)
|
||||
public void readField (Field field, Object target, ObjectInputStream in)
|
||||
throws Exception {
|
||||
field.setInt(target, in.readInt());
|
||||
}
|
||||
public void writeField (
|
||||
Field field, Object source, ObjectOutputStream out)
|
||||
public void writeField (Field field, Object source, ObjectOutputStream out)
|
||||
throws Exception {
|
||||
out.writeInt(field.getInt(source));
|
||||
}
|
||||
});
|
||||
_marshallers.put(Long.TYPE, new FieldMarshaller() {
|
||||
public void readField (
|
||||
Field field, Object target, ObjectInputStream in)
|
||||
public void readField (Field field, Object target, ObjectInputStream in)
|
||||
throws Exception {
|
||||
field.setLong(target, in.readLong());
|
||||
}
|
||||
public void writeField (
|
||||
Field field, Object source, ObjectOutputStream out)
|
||||
public void writeField (Field field, Object source, ObjectOutputStream out)
|
||||
throws Exception {
|
||||
out.writeLong(field.getLong(source));
|
||||
}
|
||||
});
|
||||
_marshallers.put(Float.TYPE, new FieldMarshaller() {
|
||||
public void readField (
|
||||
Field field, Object target, ObjectInputStream in)
|
||||
public void readField (Field field, Object target, ObjectInputStream in)
|
||||
throws Exception {
|
||||
field.setFloat(target, in.readFloat());
|
||||
}
|
||||
public void writeField (
|
||||
Field field, Object source, ObjectOutputStream out)
|
||||
public void writeField (Field field, Object source, ObjectOutputStream out)
|
||||
throws Exception {
|
||||
out.writeFloat(field.getFloat(source));
|
||||
}
|
||||
});
|
||||
_marshallers.put(Double.TYPE, new FieldMarshaller() {
|
||||
public void readField (
|
||||
Field field, Object target, ObjectInputStream in)
|
||||
public void readField (Field field, Object target, ObjectInputStream in)
|
||||
throws Exception {
|
||||
field.setDouble(target, in.readDouble());
|
||||
}
|
||||
public void writeField (
|
||||
Field field, Object source, ObjectOutputStream out)
|
||||
public void writeField (Field field, Object source, ObjectOutputStream out)
|
||||
throws Exception {
|
||||
out.writeDouble(field.getDouble(source));
|
||||
}
|
||||
});
|
||||
_marshallers.put(Date.class, new FieldMarshaller() {
|
||||
public void readField (
|
||||
Field field, Object target, ObjectInputStream in)
|
||||
public void readField (Field field, Object target, ObjectInputStream in)
|
||||
throws Exception {
|
||||
field.set(target, new Date(in.readLong()));
|
||||
}
|
||||
public void writeField (
|
||||
Field field, Object source, ObjectOutputStream out)
|
||||
public void writeField (Field field, Object source, ObjectOutputStream out)
|
||||
throws Exception {
|
||||
out.writeLong(((Date)field.get(source)).getTime());
|
||||
}
|
||||
@@ -263,12 +235,10 @@ public abstract class FieldMarshaller
|
||||
int bscount = BasicStreamers.BSTREAMER_TYPES.length;
|
||||
for (int ii = 0; ii < bscount; ii++) {
|
||||
_marshallers.put(BasicStreamers.BSTREAMER_TYPES[ii],
|
||||
new StreamerMarshaller(
|
||||
BasicStreamers.BSTREAMER_INSTANCES[ii]));
|
||||
new StreamerMarshaller(BasicStreamers.BSTREAMER_INSTANCES[ii]));
|
||||
}
|
||||
}
|
||||
|
||||
/** Contains a mapping from field type to field marshaller instance
|
||||
* for that type. */
|
||||
/** Contains a mapping from field type to field marshaller instance for that type. */
|
||||
protected static HashMap<Class,FieldMarshaller> _marshallers;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user