// // $Id$ // // Narya library - tools for developing networked games // Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved // http://www.threerings.net/code/narya/ // // This library is free software; you can redistribute it and/or modify it // under the terms of the GNU Lesser General Public License as published // by the Free Software Foundation; either version 2.1 of the License, or // (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA package com.threerings.io; import java.io.OutputStream; import java.io.DataOutputStream; import java.io.IOException; import java.util.HashMap; /** * Used to write {@link Streamable} objects to an {@link OutputStream}. Other common object types * are supported as well: * *
* Boolean * Byte * Character * Short * Integer * Long * Float * Double * boolean[] * byte[] * char[] * short[] * int[] * long[] * float[] * double[] * Object[] ** * @see Streamable */ public class ObjectOutputStream extends DataOutputStream { /** * Constructs an object output stream which will write its data to the supplied target stream. */ public ObjectOutputStream (OutputStream target) { super(target); } /** * Configures this object output stream with a mapping from a classname to a streamed name. */ public void addTranslation (String className, String streamedName) { if (_translations == null) { _translations = new HashMap
null.
*/
public void writeBareObject (Object object)
throws IOException
{
writeBareObject(object, Streamer.getStreamer(Streamer.getStreamerClass(object)), true);
}
/**
* Write a {@link Streamable} instance without associated class metadata.
*/
protected void writeBareObject (Object obj, Streamer streamer, boolean useWriter)
throws IOException
{
_current = obj;
_streamer = streamer;
try {
_streamer.writeObject(obj, this, useWriter);
} finally {
_current = null;
_streamer = null;
}
}
/**
* Uses the default streamable mechanism to write the contents of the object currently being
* streamed. This can only be called from within a writeObject implementation in a
* {@link Streamable} object.
*/
public void defaultWriteObject ()
throws IOException
{
// sanity check
if (_current == null) {
throw new RuntimeException("defaultWriteObject() called illegally.");
}
// log.info("Writing default [cmap=" + _streamer + ", current=" + _current + "].");
// write the instance data
_streamer.writeObject(_current, this, false);
}
/** Used to map classes to numeric codes and the {@link Streamer} instance used to write
* them. */
protected HashMap