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
+94 -134
View File
@@ -43,18 +43,16 @@ import com.samskivert.util.StringUtil;
import static com.threerings.NaryaLog.log;
/**
* Handles the streaming of {@link Streamable} instances as well as a set
* of basic object types (see {@link ObjectOutputStream}). An instance of
* {@link Streamer} is created for each distinct class that implements
* {@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 question.
* Handles the streaming of {@link Streamable} instances as well as a set of basic object types
* (see {@link ObjectOutputStream}). An instance of {@link Streamer} is created for each distinct
* class that implements {@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
* question.
*/
public class Streamer
{
/**
* Returns true if the supplied target class can be streamed using a
* streamer.
* Returns true if the supplied target class can be streamed using a streamer.
*/
public synchronized static boolean isStreamable (Class target)
{
@@ -74,17 +72,15 @@ public class Streamer
return Streamable.class.isAssignableFrom(target);
}
// otherwise extract the component type and loop back around for
// another check...
// otherwise extract the component type and loop back around for another check...
target = target.getComponentType();
}
}
/**
* Returns the class that should be used when streaming this object. In
* general that is the object's natural class, but for enum values, that
* might be its declaring class as enums use classes in a way that would
* otherwise pollute our id to class mapping space.
* Returns the class that should be used when streaming this object. In general that is the
* object's natural class, but for enum values, that might be its declaring class as enums use
* classes in a way that would otherwise pollute our id to class mapping space.
*/
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
* objects of the specified target class. {@link Streamer} instances
* are shared among all {@link ObjectInputStream}s and {@link
* ObjectOutputStream}s.
* Obtains a {@link Streamer} that can be used to read and write objects of the specified
* target class. {@link Streamer} instances are shared among all {@link ObjectInputStream}s and
* {@link ObjectOutputStream}s.
*
* @param target the class that is desired to be streamed. This should be
* the result of a call to {@link #getStreamerClass} if the caller has an
* instance they wish to stream.
* @param target the class that is desired to be streamed. This should be the result of a call
* to {@link #getStreamerClass} if the caller has an instance they wish to stream.
*
* @throws IOException when a streamer is requested for an object that
* does not implement {@link Streamable} and is not one of the basic
* object types (@see {@link ObjectOutputStream}).
* @throws IOException when a streamer is requested for an object that does not implement
* {@link Streamable} and is not one of the basic object types (@see {@link
* ObjectOutputStream}).
*/
public synchronized static Streamer getStreamer (final Class target)
throws IOException
@@ -128,15 +122,14 @@ public class Streamer
log.info("Creating a streamer for '" + target.getName() + "'.");
}
// create our streamer in a privileged block so that it can
// introspect on the to be streamed class
// create our streamer in a privileged block so that it can introspect on the to be
// streamed class
try {
stream = AccessController.doPrivileged(
new PrivilegedExceptionAction<Streamer>() {
public Streamer run () throws IOException {
return new Streamer(target);
}
});
stream = AccessController.doPrivileged(new PrivilegedExceptionAction<Streamer>() {
public Streamer run () throws IOException {
return new Streamer(target);
}
});
} catch (PrivilegedActionException pae) {
throw (IOException) pae.getCause();
}
@@ -150,19 +143,16 @@ public class Streamer
*
* @param object the instance to be written to the stream.
* @param out the stream to which to write the instance.
* @param useWriter whether or not to use the custom
* <code>writeObject</code> if one exists.
* @param useWriter whether or not to use the custom <code>writeObject</code> if one exists.
*/
public void writeObject (
Object object, ObjectOutputStream out, boolean useWriter)
public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
throws IOException
{
// if we're supposed to and one exists, use the writer method
if (useWriter && _writer != null) {
try {
if (ObjectInputStream.STREAM_DEBUG) {
log.info("Writing with writer " +
"[class=" + _target.getName() + "].");
log.info("Writing with writer [class=" + _target.getName() + "].");
}
_writer.invoke(object, new Object[] { out });
@@ -184,13 +174,12 @@ public class Streamer
if (_target.isArray()) {
int length = Array.getLength(object);
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
// can serialize things more efficiently
// if the component class is final, we can be sure that all instances in the array will
// be of the same class and thus can serialize things more efficiently
int cmods = _target.getComponentType().getModifiers();
if ((cmods & Modifier.FINAL) != 0) {
// compute a mask indicating which elements are null and
// which are populated
if (Modifier.isFinal(cmods)) {
// compute a mask indicating which elements are null and which are populated
ArrayMask mask = new ArrayMask(length);
for (int ii = 0; ii < length; ii++) {
if (Array.get(object, ii) != null) {
@@ -199,6 +188,7 @@ public class Streamer
}
// write that mask out to the stream
mask.writeTo(out);
// now write out the populated elements
for (int ii = 0; ii < length; ii++) {
Object element = Array.get(object, ii);
@@ -208,9 +198,8 @@ public class Streamer
}
} else {
// otherwise we've got to write each array element with
// its own class identifier because it could be any
// derived class of the array element type
// otherwise we've got to write each array element with its own class identifier
// because it could be any derived class of the array element type
for (int ii = 0; ii < length; ii++) {
out.writeObject(Array.get(object, ii));
}
@@ -218,9 +207,9 @@ public class Streamer
return;
}
// if we're writing an enum; write its string value (to avoid future
// compatibility issues if someone serializes an enum to a file and
// then adds a value to the enum, changing the ordinal assignments)
// if we're writing an enum; write its string value (to avoid future compatibility issues
// if someone serializes an enum to a file and then adds a value to the enum, changing the
// ordinal assignments)
if (_target.isEnum()) {
out.writeUTF(((Enum)object).name());
return;
@@ -232,10 +221,8 @@ public class Streamer
Field field = _fields[ii];
FieldMarshaller fm = _marshallers[ii];
if (fm == null) {
String errmsg = "Unable to marshall field " +
"[class=" + _target.getName() +
", field=" + field.getName() +
", type=" + field.getType().getName() + "]";
String errmsg = "Unable to marshall field [class=" + _target.getName() +
", field=" + field.getName() + ", type=" + field.getType().getName() + "]";
throw new IOException(errmsg);
}
try {
@@ -245,8 +232,7 @@ public class Streamer
}
fm.writeField(field, object, out);
} catch (Exception e) {
String errmsg = "Failure writing streamable field " +
"[class=" + _target.getName() +
String errmsg = "Failure writing streamable field [class=" + _target.getName() +
", field=" + field.getName() + "]";
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
* streamer. Data may be read from the input stream as a result of
* this method (in the case of arrays, the length of the array must be
* read before creating the array).
* Creates a blank object that can subsequently be read by this streamer. Data may be read
* from the input stream as a result of this method (in the case of arrays, the length of the
* array must be read before creating the array).
*/
public Object createObject (ObjectInputStream in)
throws IOException
{
try {
// if our target class is an array type, read in the element
// count and create an array instance of the appropriate type
// and size
// if our target class is an array type, read in the element count and create an array
// instance of the appropriate type and size
if (_target.isArray()) {
int length = in.readInt();
if (ObjectInputStream.STREAM_DEBUG) {
log.info(in.hashCode() + ": Creating array '" +
_target.getComponentType().getName() +
"[" + length + "]'.");
_target.getComponentType().getName() + "[" + length + "]'.");
}
return Array.newInstance(_target.getComponentType(), length);
} else if (_target.isEnum()) {
if (ObjectInputStream.STREAM_DEBUG) {
log.info(in.hashCode() + ": Creating enum '" +
_target.getName() + "'.");
log.info(in.hashCode() + ": Creating enum '" + _target.getName() + "'.");
}
@SuppressWarnings("unchecked") Object value =
Enum.valueOf(_target, in.readUTF());
@SuppressWarnings("unchecked") Object value = Enum.valueOf(_target, in.readUTF());
return value;
} else {
if (ObjectInputStream.STREAM_DEBUG) {
log.info(in.hashCode() + ": Creating object '" +
_target.getName() + "'.");
log.info(in.hashCode() + ": Creating object '" + _target.getName() + "'.");
}
return _target.newInstance();
}
} catch (InstantiationException ie) {
String errmsg = "Error instantiating object " +
"[type=" + _target.getName() + "]";
String errmsg = "Error instantiating object [type=" + _target.getName() + "]";
throw (IOException) new IOException(errmsg).initCause(ie);
} catch (IllegalAccessException iae) {
String errmsg = "Error instantiating object " +
"[type=" + _target.getName() + "]";
String errmsg = "Error instantiating object [type=" + _target.getName() + "]";
throw (IOException) new IOException(errmsg).initCause(iae);
}
}
/**
* Reads and populates the fields of the supplied object from the
* specified stream.
* Reads and populates the fields of the supplied object from the specified stream.
*
* @param object the instance to be read from the stream.
* @param in the stream from which to read the instance.
* @param useReader whether or not to use the custom
* <code>readObject</code> if one exists.
* @param useReader whether or not to use the custom <code>readObject</code> if one exists.
*/
public void readObject (
Object object, ObjectInputStream in, boolean useReader)
public void readObject (Object object, ObjectInputStream in, boolean useReader)
throws IOException, ClassNotFoundException
{
// if we're supposed to and one exists, use the reader method
if (useReader && _reader != null) {
try {
if (ObjectInputStream.STREAM_DEBUG) {
log.info(in.hashCode() + ": Reading with reader '" +
_target.getName() + "." +
log.info(in.hashCode() + ": Reading with reader '" + _target.getName() + "." +
_reader.getName() + "()'.");
}
_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 (_target.isArray()) {
int length = Array.getLength(object);
// if the component class is final, we can be sure that all
// instances in the array will be of the same class and thus
// have serialized things more efficiently
// if the component class is final, we can be sure that all instances in the array will
// be of the same class and thus have serialized things more efficiently
int cmods = _target.getComponentType().getModifiers();
if ((cmods & Modifier.FINAL) != 0) {
if (Modifier.isFinal(cmods)) {
// read in the nullness mask
ArrayMask mask = new ArrayMask();
mask.readFrom(in);
// now read in the array elements given that we know which
// elements to read
// now read in the array elements given that we know which elements to read
for (int ii = 0; ii < length; ii++) {
if (mask.isSet(ii)) {
if (ObjectInputStream.STREAM_DEBUG) {
log.info(in.hashCode() +
": Reading fixed element '" + ii + "'.");
log.info(in.hashCode() + ": Reading fixed element '" + ii + "'.");
}
Object element = _delegate.createObject(in);
in.readBareObject(element, _delegate, useReader);
Array.set(object, ii, element);
} else if (ObjectInputStream.STREAM_DEBUG) {
log.info(in.hashCode() +
": Skipping null element '" + ii + "'.");
log.info(in.hashCode() + ": Skipping null element '" + ii + "'.");
}
}
@@ -377,8 +347,7 @@ public class Streamer
// otherwise we had to write each object out individually
for (int ii = 0; ii < length; ii++) {
if (ObjectInputStream.STREAM_DEBUG) {
log.info(in.hashCode() +
": Reading free element '" + ii + "'.");
log.info(in.hashCode() + ": Reading free element '" + ii + "'.");
}
Array.set(object, ii, in.readObject());
}
@@ -397,31 +366,27 @@ public class Streamer
Field field = _fields[ii];
FieldMarshaller fm = _marshallers[ii];
if (fm == null) {
String errmsg = "Unable to marshall field " +
"[class=" + _target.getName() +
", field=" + field.getName() +
", type=" + field.getType().getName() + "]";
String errmsg = "Unable to marshall field [class=" + _target.getName() +
", field=" + field.getName() + ", type=" + field.getType().getName() + "]";
throw new IOException(errmsg);
}
try {
if (ObjectInputStream.STREAM_DEBUG) {
log.info(in.hashCode() +
": Reading field '" + field.getName() + "'.");
log.info(in.hashCode() + ": Reading field '" + field.getName() + "'.");
}
// gracefully deal with objects that have had new fields
// added to their class definition
// gracefully deal with objects that have had new fields added to their class
// definition
if (in.available() > 0) {
log.info("Using " + fm);
fm.readField(field, object, in);
} else {
log.info("Streamed instance missing field (probably " +
"newly added) [class=" + _target.getName() +
", field=" + field.getName() + "].");
log.info("Streamed instance missing field (probably newly added) " +
"[class=" + _target.getName() + ", field=" + field.getName() + "].");
}
} catch (Exception e) {
String errmsg = "Failure reading streamable field " +
"[class=" + _target.getName() +
", field=" + field.getName() +
", error=" + e + "]";
e.printStackTrace(System.err);
String errmsg = "Failure reading streamable field [class=" + _target.getName() +
", field=" + field.getName() + ", error=" + e + "]";
throw (IOException) new IOException(errmsg).initCause(e);
}
}
@@ -436,9 +401,10 @@ public class Streamer
*/
public String toString ()
{
return "[target=" + _target.getName() + ", delegate=" + _delegate +
", fcount=" + _fields.length + ", reader=" + _reader +
", writer=" + _writer + "]";
return (_target == null) ? getClass().getName() :
"[target=" + _target.getName() + ", delegate=" + _delegate +
", fcount=" + (_fields == null ? 0 : _fields.length) +
", reader=" + _reader + ", writer=" + _writer + "]";
}
/**
@@ -457,15 +423,14 @@ public class Streamer
// keep a handle on the class
_target = target;
// if our target class is an array, we want to get a handle on a
// streamer delegate that we'll use to stream our elements
// if our target class is an array, we want to get a handle on a streamer delegate that
// we'll use to stream our elements
if (_target.isArray()) {
_delegate = Streamer.getStreamer(_target.getComponentType());
// sanity check
if (_delegate == null) {
String errmsg = "Aiya! Streamer created for array type " +
"but we have no registered streamer for the element " +
"type [type=" + _target.getName() + "]";
String errmsg = "Aiya! Streamer created for array type but we have no registered " +
"streamer for the element type [type=" + _target.getName() + "]";
throw new RuntimeException(errmsg);
}
// and that's all we'll need
@@ -481,8 +446,8 @@ public class Streamer
for (int ii = 0; ii < fcount; ii++) {
_marshallers[ii] = FieldMarshaller.getFieldMarshaller(_fields[ii]);
if (ObjectInputStream.STREAM_DEBUG) {
log.info("Using " + _marshallers[ii] + " for " +
_target.getName() + "." + _fields[ii].getName() + ".");
log.info("Using " + _marshallers[ii] + " for " + _target.getName() + "." +
_fields[ii].getName() + ".");
}
}
@@ -500,8 +465,7 @@ public class Streamer
}
/**
* Creates our streamers table and registers streamers for all of the
* basic types.
* Creates our streamers table and registers streamers for all of the basic types.
*/
protected static void createStreamers ()
{
@@ -518,28 +482,24 @@ public class Streamer
/** The class for which this streamer instance is configured. */
protected Class _target;
/** If our target class is an array, this is a reference to a streamer
* that can stream our array elements, otherwise it is null. */
/** If our target class is an array, this is a reference to a streamer that can stream our
* array elements, otherwise it is null. */
protected Streamer _delegate;
/** The non-transient, non-static public fields that we will stream
* when requested. */
/** The non-transient, non-static public fields that we will stream when requested. */
protected Field[] _fields;
/** Field marshallers for each field that will be read or written in
* our objects. */
/** Field marshallers for each field that will be read or written in our objects. */
protected FieldMarshaller[] _marshallers;
/** A reference to the <code>readObject</code> method if one is
* defined by our target class. */
/** A reference to the <code>readObject</code> method if one is defined by our target class. */
protected Method _reader;
/** A reference to the <code>writeObject</code> method if one is
* defined by our target class. */
/** A reference to the <code>writeObject</code> method if one is defined by our target
* class. */
protected Method _writer;
/** Contains the mapping from class names to configured streamer
* instances. */
/** Contains the mapping from class names to configured streamer instances. */
protected static HashMap<Class,Streamer> _streamers;
/** The name of the custom reader method. */