Widening.
git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4682 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -34,17 +34,15 @@ import com.samskivert.util.StringUtil;
|
|||||||
import static com.threerings.NaryaLog.log;
|
import static com.threerings.NaryaLog.log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to read {@link Streamable} objects from an {@link InputStream}.
|
* Used to read {@link Streamable} objects from an {@link InputStream}. Other common object types
|
||||||
* Other common object types are supported as well (@see {@link
|
* are supported as well (@see {@link ObjectOutputStream}).
|
||||||
* ObjectOutputStream}).
|
|
||||||
*
|
*
|
||||||
* @see Streamable
|
* @see Streamable
|
||||||
*/
|
*/
|
||||||
public class ObjectInputStream extends DataInputStream
|
public class ObjectInputStream extends DataInputStream
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Constructs an object input stream which will read its data from the
|
* Constructs an object input stream which will read its data from the supplied source stream.
|
||||||
* supplied source stream.
|
|
||||||
*/
|
*/
|
||||||
public ObjectInputStream (InputStream source)
|
public ObjectInputStream (InputStream source)
|
||||||
{
|
{
|
||||||
@@ -52,8 +50,7 @@ public class ObjectInputStream extends DataInputStream
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Customizes the class loader used to instnatiate objects read from
|
* Customizes the class loader used to instnatiate objects read from the input stream.
|
||||||
* the input stream.
|
|
||||||
*/
|
*/
|
||||||
public void setClassLoader (ClassLoader loader)
|
public void setClassLoader (ClassLoader loader)
|
||||||
{
|
{
|
||||||
@@ -61,9 +58,9 @@ public class ObjectInputStream extends DataInputStream
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configures this object input stream with a mapping from an old
|
* Configures this object input stream with a mapping from an old class name to a new
|
||||||
* class name to a new one. Serialized instances of the old class name
|
* one. Serialized instances of the old class name will use the new class name when
|
||||||
* will use the new class name when unserializing.
|
* unserializing.
|
||||||
*/
|
*/
|
||||||
public void addTranslation (String oldname, String newname)
|
public void addTranslation (String oldname, String newname)
|
||||||
{
|
{
|
||||||
@@ -74,8 +71,8 @@ public class ObjectInputStream extends DataInputStream
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads a {@link Streamable} instance or one of the supported object
|
* Reads a {@link Streamable} instance or one of the supported object types from the input
|
||||||
* types from the input stream.
|
* stream.
|
||||||
*/
|
*/
|
||||||
public Object readObject ()
|
public Object readObject ()
|
||||||
throws IOException, ClassNotFoundException
|
throws IOException, ClassNotFoundException
|
||||||
@@ -100,16 +97,16 @@ public class ObjectInputStream extends DataInputStream
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
// if the code is negative, that means that we've never
|
// if the code is negative, that means that we've never seen it before and class
|
||||||
// seen it before and class metadata follows
|
// metadata follows
|
||||||
} else if (code < 0) {
|
} else if (code < 0) {
|
||||||
// first swap the code into positive-land
|
// first swap the code into positive-land
|
||||||
code *= -1;
|
code *= -1;
|
||||||
|
|
||||||
// read in the class metadata
|
// read in the class metadata
|
||||||
String cname = readUTF();
|
String cname = readUTF();
|
||||||
// if we have a translation (used to cope when serialized
|
// if we have a translation (used to cope when serialized classes are renamed) use
|
||||||
// classes are renamed) use it
|
// it
|
||||||
if (_translations != null) {
|
if (_translations != null) {
|
||||||
String tname = _translations.get(cname);
|
String tname = _translations.get(cname);
|
||||||
if (tname != null) {
|
if (tname != null) {
|
||||||
@@ -126,9 +123,8 @@ public class ObjectInputStream extends DataInputStream
|
|||||||
|
|
||||||
// sanity check
|
// sanity check
|
||||||
if (streamer == null) {
|
if (streamer == null) {
|
||||||
String errmsg = "Aiya! Unable to create streamer for " +
|
String errmsg = "Aiya! Unable to create streamer for newly seen class " +
|
||||||
"newly seen class [code=" + code +
|
"[code=" + code + ", class=" + cname + "]";
|
||||||
", class=" + cname + "]";
|
|
||||||
throw new RuntimeException(errmsg);
|
throw new RuntimeException(errmsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,8 +142,8 @@ public class ObjectInputStream extends DataInputStream
|
|||||||
Thread.dumpStack();
|
Thread.dumpStack();
|
||||||
log.warning(
|
log.warning(
|
||||||
"ObjectInputStream mappings " + 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 " +
|
||||||
"have no registered class metadata [code=" + code + "]";
|
"metadata [code=" + code + "]";
|
||||||
throw new RuntimeException(errmsg);
|
throw new RuntimeException(errmsg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -167,11 +163,11 @@ public class ObjectInputStream extends DataInputStream
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads an object from the input stream that was previously written
|
* Reads an object from the input stream that was previously written with {@link
|
||||||
* with {@link ObjectOutputStream#writeBareObject}.
|
* ObjectOutputStream#writeBareObject}.
|
||||||
*
|
*
|
||||||
* @param object the object to be populated from data on the stream.
|
* @param object the object to be populated from data on the stream. It cannot be
|
||||||
* It cannot be <code>null</code>.
|
* <code>null</code>.
|
||||||
*/
|
*/
|
||||||
public void readBareObject (Object object)
|
public void readBareObject (Object object)
|
||||||
throws IOException, ClassNotFoundException
|
throws IOException, ClassNotFoundException
|
||||||
@@ -180,18 +176,16 @@ public class ObjectInputStream extends DataInputStream
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads an object from the input stream that was previously written
|
* Reads an object from the input stream that was previously written with {@link
|
||||||
* with {@link ObjectOutputStream#writeBareObject}.
|
* ObjectOutputStream#writeBareObject}.
|
||||||
*/
|
*/
|
||||||
protected void readBareObject (
|
protected void readBareObject (Object object, Streamer streamer, boolean useReader)
|
||||||
Object object, Streamer streamer, boolean useReader)
|
|
||||||
throws IOException, ClassNotFoundException
|
throws IOException, ClassNotFoundException
|
||||||
{
|
{
|
||||||
_current = object;
|
_current = object;
|
||||||
_streamer = streamer;
|
_streamer = streamer;
|
||||||
try {
|
try {
|
||||||
_streamer.readObject(object, this, useReader);
|
_streamer.readObject(object, this, useReader);
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
// clear out our current object references
|
// clear out our current object references
|
||||||
_current = null;
|
_current = null;
|
||||||
@@ -200,18 +194,16 @@ public class ObjectInputStream extends DataInputStream
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads the fields of the specified {@link Streamable} instance from
|
* Reads the fields of the specified {@link Streamable} instance from the input stream using
|
||||||
* the input stream using the default object streaming mechanisms (a
|
* the default object streaming mechanisms (a call is not made to <code>readObject()</code>,
|
||||||
* call is not made to <code>readObject()</code>, even if such a
|
* even if such a method exists).
|
||||||
* method exists).
|
|
||||||
*/
|
*/
|
||||||
public void defaultReadObject ()
|
public void defaultReadObject ()
|
||||||
throws IOException, ClassNotFoundException
|
throws IOException, ClassNotFoundException
|
||||||
{
|
{
|
||||||
// sanity check
|
// sanity check
|
||||||
if (_current == null) {
|
if (_current == null) {
|
||||||
throw new RuntimeException(
|
throw new RuntimeException("defaultReadObject() called illegally.");
|
||||||
"defaultReadObject() called illegally.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// read the instance data
|
// read the instance data
|
||||||
@@ -224,12 +216,11 @@ public class ObjectInputStream extends DataInputStream
|
|||||||
public String toString ()
|
public String toString ()
|
||||||
{
|
{
|
||||||
return "[hash=" + hashCode() + ", mappings=" + _classmap.size() +
|
return "[hash=" + hashCode() + ", mappings=" + _classmap.size() +
|
||||||
", current=" + StringUtil.safeToString(_current) +
|
", current=" + StringUtil.safeToString(_current) + ", streamer=" + _streamer + "]";
|
||||||
", streamer=" + _streamer + "]";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Used to map classes to numeric codes and the {@link Streamer}
|
/** Used to map classes to numeric codes and the {@link Streamer} instance used to write
|
||||||
* instance used to write them. */
|
* them. */
|
||||||
protected ArrayList<ClassMapping> _classmap;
|
protected ArrayList<ClassMapping> _classmap;
|
||||||
|
|
||||||
/** The object currently being read from the stream. */
|
/** The object currently being read from the stream. */
|
||||||
@@ -241,8 +232,7 @@ public class ObjectInputStream extends DataInputStream
|
|||||||
/** The class loader we use to instantiate objects. */
|
/** The class loader we use to instantiate objects. */
|
||||||
protected ClassLoader _loader = getClass().getClassLoader();
|
protected ClassLoader _loader = getClass().getClassLoader();
|
||||||
|
|
||||||
/** An optional set of class name translations to use when
|
/** An optional set of class name translations to use when unserializing objects. */
|
||||||
* unserializing objects. */
|
|
||||||
protected HashMap<String,String> _translations;
|
protected HashMap<String,String> _translations;
|
||||||
|
|
||||||
/** Used to activate verbose debug logging. */
|
/** Used to activate verbose debug logging. */
|
||||||
|
|||||||
@@ -27,8 +27,8 @@ import java.io.IOException;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to write {@link Streamable} objects to an {@link OutputStream}.
|
* Used to write {@link Streamable} objects to an {@link OutputStream}. Other common object types
|
||||||
* Other common object types are supported as well:
|
* are supported as well:
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* Boolean
|
* Boolean
|
||||||
@@ -55,8 +55,7 @@ import java.util.HashMap;
|
|||||||
public class ObjectOutputStream extends DataOutputStream
|
public class ObjectOutputStream extends DataOutputStream
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Constructs an object output stream which will write its data to the
|
* Constructs an object output stream which will write its data to the supplied target stream.
|
||||||
* supplied target stream.
|
|
||||||
*/
|
*/
|
||||||
public ObjectOutputStream (OutputStream target)
|
public ObjectOutputStream (OutputStream target)
|
||||||
{
|
{
|
||||||
@@ -64,8 +63,7 @@ public class ObjectOutputStream extends DataOutputStream
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configures this object output stream with a mapping from a classname
|
* Configures this object output stream with a mapping from a classname to a streamed name.
|
||||||
* to a streamed name.
|
|
||||||
*/
|
*/
|
||||||
public void addTranslation (String className, String streamedName)
|
public void addTranslation (String className, String streamedName)
|
||||||
{
|
{
|
||||||
@@ -76,8 +74,8 @@ public class ObjectOutputStream extends DataOutputStream
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes a {@link Streamable} instance or one of the support object
|
* Writes a {@link Streamable} instance or one of the support object types to the output
|
||||||
* types to the output stream.
|
* stream.
|
||||||
*/
|
*/
|
||||||
public void writeObject (Object object)
|
public void writeObject (Object object)
|
||||||
throws IOException
|
throws IOException
|
||||||
@@ -101,21 +99,18 @@ public class ObjectOutputStream extends DataOutputStream
|
|||||||
if (cmap == null) {
|
if (cmap == null) {
|
||||||
// create a streamer instance and assign a code to this class
|
// create a streamer instance and assign a code to this class
|
||||||
Streamer streamer = Streamer.getStreamer(sclass);
|
Streamer streamer = Streamer.getStreamer(sclass);
|
||||||
// we specifically do not inline the getStreamer() call
|
// we specifically do not inline the getStreamer() call into the ClassMapping
|
||||||
// into the ClassMapping constructor because we want to be
|
// constructor because we want to be sure not to call _nextCode++ if getStreamer()
|
||||||
// sure not to call _nextCode++ if getStreamer() throws an
|
// throws an exception
|
||||||
// exception
|
|
||||||
cmap = new ClassMapping(_nextCode++, sclass, streamer);
|
cmap = new ClassMapping(_nextCode++, sclass, streamer);
|
||||||
_classmap.put(sclass, cmap);
|
_classmap.put(sclass, cmap);
|
||||||
|
|
||||||
// make sure we didn't blow past our maximum class count
|
// make sure we didn't blow past our maximum class count
|
||||||
if (_nextCode <= 0) {
|
if (_nextCode <= 0) {
|
||||||
throw new RuntimeException("Too many unique classes " +
|
throw new RuntimeException("Too many unique classes written to ObjectOutputStream");
|
||||||
"written to ObjectOutputStream");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// writing a negative class code indicates that the class
|
// writing a negative class code indicates that the class name will follow
|
||||||
// name will follow
|
|
||||||
writeShort(-cmap.code);
|
writeShort(-cmap.code);
|
||||||
String cname = sclass.getName();
|
String cname = sclass.getName();
|
||||||
if (_translations != null) {
|
if (_translations != null) {
|
||||||
@@ -134,34 +129,29 @@ public class ObjectOutputStream extends DataOutputStream
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes a {@link Streamable} instance or one of the support object
|
* Writes a {@link Streamable} instance or one of the support object types <em>without
|
||||||
* types <em>without associated class metadata</em> to the output
|
* associated class metadata</em> to the output stream. The caller is responsible for knowing
|
||||||
* stream. The caller is responsible for knowing the exact class of
|
* the exact class of the written object, creating an instance of such and calling {@link
|
||||||
* the written object, creating an instance of such and calling {@link
|
|
||||||
* ObjectInputStream#readBareObject} to read its data from the stream.
|
* ObjectInputStream#readBareObject} to read its data from the stream.
|
||||||
*
|
*
|
||||||
* @param object the object to be written. It cannot be
|
* @param object the object to be written. It cannot be <code>null</code>.
|
||||||
* <code>null</code>.
|
|
||||||
*/
|
*/
|
||||||
public void writeBareObject (Object object)
|
public void writeBareObject (Object object)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
writeBareObject(object, Streamer.getStreamer(
|
writeBareObject(object, Streamer.getStreamer(Streamer.getStreamerClass(object)), true);
|
||||||
Streamer.getStreamerClass(object)), true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write a {@link Streamable} instance without associated class metadata.
|
* Write a {@link Streamable} instance without associated class metadata.
|
||||||
*/
|
*/
|
||||||
protected void writeBareObject (
|
protected void writeBareObject (Object obj, Streamer streamer, boolean useWriter)
|
||||||
Object obj, Streamer streamer, boolean useWriter)
|
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
_current = obj;
|
_current = obj;
|
||||||
_streamer = streamer;
|
_streamer = streamer;
|
||||||
try {
|
try {
|
||||||
_streamer.writeObject(obj, this, useWriter);
|
_streamer.writeObject(obj, this, useWriter);
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
_current = null;
|
_current = null;
|
||||||
_streamer = null;
|
_streamer = null;
|
||||||
@@ -169,29 +159,26 @@ public class ObjectOutputStream extends DataOutputStream
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uses the default streamable mechanism to write the contents of the
|
* Uses the default streamable mechanism to write the contents of the object currently being
|
||||||
* object currently being streamed. This can only be called from
|
* streamed. This can only be called from within a <code>writeObject</code> implementation in a
|
||||||
* within a <code>writeObject</code> implementation in a {@link
|
* {@link Streamable} object.
|
||||||
* Streamable} object.
|
|
||||||
*/
|
*/
|
||||||
public void defaultWriteObject ()
|
public void defaultWriteObject ()
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
// sanity check
|
// sanity check
|
||||||
if (_current == null) {
|
if (_current == null) {
|
||||||
throw new RuntimeException(
|
throw new RuntimeException("defaultWriteObject() called illegally.");
|
||||||
"defaultWriteObject() called illegally.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// log.info("Writing default [cmap=" + _streamer +
|
// log.info("Writing default [cmap=" + _streamer + ", current=" + _current + "].");
|
||||||
// ", current=" + _current + "].");
|
|
||||||
|
|
||||||
// write the instance data
|
// write the instance data
|
||||||
_streamer.writeObject(_current, this, false);
|
_streamer.writeObject(_current, this, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Used to map classes to numeric codes and the {@link Streamer}
|
/** Used to map classes to numeric codes and the {@link Streamer} instance used to write
|
||||||
* instance used to write them. */
|
* them. */
|
||||||
protected HashMap<Class,ClassMapping> _classmap;
|
protected HashMap<Class,ClassMapping> _classmap;
|
||||||
|
|
||||||
/** A counter used to assign codes to streamed classes. */
|
/** A counter used to assign codes to streamed classes. */
|
||||||
@@ -203,7 +190,6 @@ public class ObjectOutputStream extends DataOutputStream
|
|||||||
/** The streamer being used currently. */
|
/** The streamer being used currently. */
|
||||||
protected Streamer _streamer;
|
protected Streamer _streamer;
|
||||||
|
|
||||||
/** An optional set of class name translations to use when serializing
|
/** An optional set of class name translations to use when serializing objects. */
|
||||||
* objects. */
|
|
||||||
protected HashMap<String,String> _translations;
|
protected HashMap<String,String> _translations;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user