Convert Narya (most of the way) over to a Maven Ant task based build. The

ActionScript bits remain belligerent, but the Java stuff is mostly shipshape.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@6222 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2010-10-22 21:12:29 +00:00
parent 555b865bbf
commit 9d2ca42eac
434 changed files with 163 additions and 208 deletions
@@ -0,0 +1,91 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/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.IOException;
/**
* Used to keep track of which entries in an array are null and which are
* not. <em>Note:</em> only arrays up to 262,144 elements in length can be
* handled by this class.
*/
public class ArrayMask
{
/**
* Creates an array mask suitable for unserializing.
*/
public ArrayMask ()
{
}
/**
* Creates an array mask for an array of the specified length.
*/
public ArrayMask (int length)
{
int mlength = length/8;
if (length % 8 != 0) {
mlength++;
}
_mask = new byte[mlength];
}
/**
* Sets the bit indicating that the specified array index is non-null.
*/
public void set (int index)
{
_mask[index/8] |= (1 << (index%8));
}
/**
* Returns true if the specified array index should be non-null.
*/
public boolean isSet (int index)
{
return (_mask[index/8] & (1 << (index%8))) != 0;
}
/**
* Writes this mask to the specified output stream.
*/
public void writeTo (ObjectOutputStream out)
throws IOException
{
out.writeShort(_mask.length);
out.write(_mask);
}
/**
* Reads this mask from the specified input stream.
*/
public void readFrom (ObjectInputStream in)
throws IOException
{
int length = in.readShort();
_mask = new byte[length];
in.read(_mask);
}
/** A byte array with bits for every entry in the source array. */
protected byte[] _mask;
}
@@ -0,0 +1,748 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/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.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.io.EOFException;
import java.io.IOException;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
/**
* Code to read and write basic object types (like arrays of primitives, {@link Integer} instances,
* {@link Double} instances, etc.).
*/
public class BasicStreamers
{
public static final Map<Class<?>, Streamer> BSTREAMERS =
ImmutableMap.<Class<?>, Streamer>builder()
.put(Boolean.class, new BooleanStreamer())
.put(Byte.class, new ByteStreamer())
.put(Short.class, new ShortStreamer())
.put(Character.class, new CharacterStreamer())
.put(Integer.class, new IntegerStreamer())
.put(Long.class, new LongStreamer())
.put(Float.class, new FloatStreamer())
.put(Double.class, new DoubleStreamer())
.put(Class.class, new ClassStreamer())
.put(String.class, Boolean.getBoolean("com.threerings.io.unmodifiedUTFStreaming")
? new UnmodifiedUTFStringStreamer()
: new StringStreamer())
.put(boolean[].class, new BooleanArrayStreamer())
.put(byte[].class, new ByteArrayStreamer())
.put(short[].class, new ShortArrayStreamer())
.put(char[].class, new CharArrayStreamer())
.put(int[].class, new IntArrayStreamer())
.put(long[].class, new LongArrayStreamer())
.put(float[].class, new FloatArrayStreamer())
.put(double[].class, new DoubleArrayStreamer())
.put(Object[].class, new ObjectArrayStreamer())
.put(List.class, ListStreamer.INSTANCE)
.put(ArrayList.class, ListStreamer.INSTANCE)
.put(Collection.class, ListStreamer.INSTANCE)
.put(Set.class, SetStreamer.INSTANCE)
.put(HashSet.class, SetStreamer.INSTANCE)
.put(Map.class, MapStreamer.INSTANCE)
.put(HashMap.class, MapStreamer.INSTANCE)
.build();
/** Streams {@link Boolean} instances. */
public static class BooleanStreamer extends BasicStreamer
{
@Override
public Object createObject (ObjectInputStream in)
throws IOException
{
return Boolean.valueOf(in.readBoolean());
}
@Override
public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
throws IOException
{
out.writeBoolean(((Boolean)object).booleanValue());
}
}
/** Streams {@link Byte} instances. */
public static class ByteStreamer extends BasicStreamer
{
@Override
public Object createObject (ObjectInputStream in)
throws IOException
{
return Byte.valueOf(in.readByte());
}
@Override
public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
throws IOException
{
out.writeByte(((Byte)object).byteValue());
}
}
/** Streams {@link Short} instances. */
public static class ShortStreamer extends BasicStreamer
{
@Override
public Object createObject (ObjectInputStream in)
throws IOException
{
return Short.valueOf(in.readShort());
}
@Override
public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
throws IOException
{
out.writeShort(((Short)object).shortValue());
}
}
/** Streams {@link Character} instances. */
public static class CharacterStreamer extends BasicStreamer
{
@Override
public Object createObject (ObjectInputStream in)
throws IOException
{
return Character.valueOf(in.readChar());
}
@Override
public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
throws IOException
{
out.writeChar(((Character)object).charValue());
}
}
/** Streams {@link Integer} instances. */
public static class IntegerStreamer extends BasicStreamer
{
@Override
public Object createObject (ObjectInputStream in)
throws IOException
{
return Integer.valueOf(in.readInt());
}
@Override
public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
throws IOException
{
out.writeInt(((Integer)object).intValue());
}
}
/** Streams {@link Long} instances. */
public static class LongStreamer extends BasicStreamer
{
@Override
public Object createObject (ObjectInputStream in)
throws IOException
{
return Long.valueOf(in.readLong());
}
@Override
public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
throws IOException
{
out.writeLong(((Long)object).longValue());
}
}
/** Streams {@link Float} instances. */
public static class FloatStreamer extends BasicStreamer
{
@Override
public Object createObject (ObjectInputStream in)
throws IOException
{
return Float.valueOf(in.readFloat());
}
@Override
public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
throws IOException
{
out.writeFloat(((Float)object).floatValue());
}
}
/** Streams {@link Double} instances. */
public static class DoubleStreamer extends BasicStreamer
{
@Override
public Object createObject (ObjectInputStream in)
throws IOException
{
return Double.valueOf(in.readDouble());
}
@Override
public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
throws IOException
{
out.writeDouble(((Double)object).doubleValue());
}
}
/** Streams {@link Class} instances (but only those that represent streamable classes). */
public static class ClassStreamer extends BasicStreamer
{
@Override
public Object createObject (ObjectInputStream in)
throws IOException, ClassNotFoundException
{
return in.readClassMapping().sclass;
}
@Override
public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
throws IOException
{
out.writeClassMapping((Class<?>)object);
}
}
/** Streams {@link String} instances, using modifiedUTF. */
public static class StringStreamer extends BasicStreamer
{
@Override
public Object createObject (ObjectInputStream in)
throws IOException
{
return in.readUTF();
}
@Override
public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
throws IOException
{
out.writeUTF((String)object);
}
}
/** Streams {@link String} instances, without using modifiedUTF. */
public static class UnmodifiedUTFStringStreamer extends BasicStreamer
{
@Override
public Object createObject (ObjectInputStream in)
throws IOException
{
return in.readUnmodifiedUTF();
}
@Override
public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
throws IOException
{
out.writeUnmodifiedUTF((String)object);
}
}
/** Streams arrays of booleans. */
public static class BooleanArrayStreamer extends BasicStreamer
{
@Override
public Object createObject (ObjectInputStream in)
throws IOException
{
return readBooleanArray(in);
}
@Override
public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
throws IOException
{
writeBooleanArray(out, (boolean[])object);
}
}
/** Streams arrays of bytes. */
public static class ByteArrayStreamer extends BasicStreamer
{
@Override
public Object createObject (ObjectInputStream in)
throws IOException
{
return readByteArray(in);
}
@Override
public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
throws IOException
{
writeByteArray(out, (byte[])object);
}
}
/** Streams arrays of shorts. */
public static class ShortArrayStreamer extends BasicStreamer
{
@Override
public Object createObject (ObjectInputStream in)
throws IOException
{
return readShortArray(in);
}
@Override
public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
throws IOException
{
writeShortArray(out, (short[])object);
}
}
/** Streams arrays of chars. */
public static class CharArrayStreamer extends BasicStreamer
{
@Override
public Object createObject (ObjectInputStream in)
throws IOException
{
return readCharArray(in);
}
@Override
public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
throws IOException
{
writeCharArray(out, (char[])object);
}
}
/** Streams arrays of ints. */
public static class IntArrayStreamer extends BasicStreamer
{
@Override
public Object createObject (ObjectInputStream in)
throws IOException
{
return readIntArray(in);
}
@Override
public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
throws IOException
{
writeIntArray(out, (int[])object);
}
}
/** Streams arrays of longs. */
public static class LongArrayStreamer extends BasicStreamer
{
@Override
public Object createObject (ObjectInputStream in)
throws IOException
{
return readLongArray(in);
}
@Override
public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
throws IOException
{
writeLongArray(out, (long[])object);
}
}
/** Streams arrays of floats. */
public static class FloatArrayStreamer extends BasicStreamer
{
@Override
public Object createObject (ObjectInputStream in)
throws IOException
{
return readFloatArray(in);
}
@Override
public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
throws IOException
{
writeFloatArray(out, (float[])object);
}
}
/** Streams arrays of doubles. */
public static class DoubleArrayStreamer extends BasicStreamer
{
@Override
public Object createObject (ObjectInputStream in)
throws IOException
{
return readDoubleArray(in);
}
@Override
public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
throws IOException
{
writeDoubleArray(out, (double[])object);
}
}
/** Streams arrays of Object instances. */
public static class ObjectArrayStreamer extends BasicStreamer
{
@Override
public Object createObject (ObjectInputStream in)
throws IOException, ClassNotFoundException
{
return readObjectArray(in);
}
@Override
public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
throws IOException
{
writeObjectArray(out, (Object[])object);
}
}
/** A building-block class for streaming Collections. */
protected abstract static class CollectionStreamer extends BasicStreamer
{
@Override
public Object createObject (ObjectInputStream in)
throws IOException, ClassNotFoundException
{
int size = in.readInt();
Collection<Object> coll = createCollection(size);
for (int ii = 0; ii < size; ii++) {
coll.add(in.readObject());
}
return coll;
}
@Override
public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
throws IOException
{
Collection<?> coll = (Collection<?>)object;
out.writeInt(coll.size());
for (Object o : coll) {
out.writeObject(o);
}
}
/**
* Called to create the collection being read.
*
* @param size the exact size of the collection.
*/
protected abstract Collection<Object> createCollection (int size);
}
/** Streams {@link List} instances. */
public static class ListStreamer extends CollectionStreamer
{
/** A singleton instance. */
public static final ListStreamer INSTANCE = new ListStreamer();
@Override
protected Collection<Object> createCollection (int size)
{
return Lists.newArrayListWithCapacity(size);
}
}
/** Streams {@link Set} instances. */
public static class SetStreamer extends CollectionStreamer
{
/** A singleton instance. */
public static final SetStreamer INSTANCE = new SetStreamer();
@Override
protected Collection<Object> createCollection (int size)
{
return Sets.newHashSetWithExpectedSize(size);
}
}
/** Streams {@link Map} instances. */
public static class MapStreamer extends BasicStreamer
{
/** A singleton instance. */
public static final MapStreamer INSTANCE = new MapStreamer();
@Override
public Object createObject (ObjectInputStream in)
throws IOException, ClassNotFoundException
{
int size = in.readInt();
Map<Object, Object> map = createMap(size);
for (int ii = 0; ii < size; ii++) {
map.put(in.readObject(), in.readObject());
}
return map;
}
@Override
public void writeObject (Object object, ObjectOutputStream out, boolean useWriter)
throws IOException
{
Map<?, ?> map = (Map<?, ?>)object;
out.writeInt(map.size());
for (Map.Entry<?, ?> entry : map.entrySet()) {
out.writeObject(entry.getKey());
out.writeObject(entry.getValue());
}
}
/**
* Overrideable if a subclass is desired to stream a different kind of map.
*/
protected Map<Object, Object> createMap (int size)
{
return Maps.newHashMapWithExpectedSize(size);
}
}
// // TODO : We can't create an EnumMap of zero size- we need to know the key class
// public static class EnumMapStreamer extends MapStreamer
// {
// }
// // TODO : We can't create an EnumSet of zero size- we need to know the key class
// public static class EnumSetStreamer extends CollectionStreamer
// {
// }
/** Streams {@link String} instances. */
public static class BasicStreamer extends Streamer
{
@Override
public void readObject (Object object, ObjectInputStream in, boolean useReader)
throws IOException, ClassNotFoundException
{
// nothing to do here
}
}
public static boolean[] readBooleanArray (ObjectInputStream ins)
throws IOException
{
boolean[] value = new boolean[ins.readInt()];
int ecount = value.length;
for (int ii = 0; ii < ecount; ii++) {
value[ii] = ins.readBoolean();
}
return value;
}
public static byte[] readByteArray (ObjectInputStream ins)
throws IOException
{
byte[] value = new byte[ins.readInt()];
int remain = value.length, offset = 0, read;
while (remain > 0) {
if ((read = ins.read(value, offset, remain)) > 0) {
remain -= read;
offset += read;
} else {
throw new EOFException();
}
}
return value;
}
public static short[] readShortArray (ObjectInputStream ins)
throws IOException
{
short[] value = new short[ins.readInt()];
int ecount = value.length;
for (int ii = 0; ii < ecount; ii++) {
value[ii] = ins.readShort();
}
return value;
}
public static char[] readCharArray (ObjectInputStream ins)
throws IOException
{
char[] value = new char[ins.readInt()];
int ecount = value.length;
for (int ii = 0; ii < ecount; ii++) {
value[ii] = ins.readChar();
}
return value;
}
public static int[] readIntArray (ObjectInputStream ins)
throws IOException
{
int[] value = new int[ins.readInt()];
int ecount = value.length;
for (int ii = 0; ii < ecount; ii++) {
value[ii] = ins.readInt();
}
return value;
}
public static long[] readLongArray (ObjectInputStream ins)
throws IOException
{
long[] value = new long[ins.readInt()];
int ecount = value.length;
for (int ii = 0; ii < ecount; ii++) {
value[ii] = ins.readLong();
}
return value;
}
public static float[] readFloatArray (ObjectInputStream ins)
throws IOException
{
float[] value = new float[ins.readInt()];
int ecount = value.length;
for (int ii = 0; ii < ecount; ii++) {
value[ii] = ins.readFloat();
}
return value;
}
public static double[] readDoubleArray (ObjectInputStream ins)
throws IOException
{
double[] value = new double[ins.readInt()];
int ecount = value.length;
for (int ii = 0; ii < ecount; ii++) {
value[ii] = ins.readDouble();
}
return value;
}
public static Object[] readObjectArray (ObjectInputStream ins)
throws IOException, ClassNotFoundException
{
Object[] value = new Object[ins.readInt()];
int ecount = value.length;
for (int ii = 0; ii < ecount; ii++) {
value[ii] = ins.readObject();
}
return value;
}
public static void writeBooleanArray (ObjectOutputStream out, boolean[] value)
throws IOException
{
int ecount = value.length;
out.writeInt(ecount);
for (int ii = 0; ii < ecount; ii++) {
out.writeBoolean(value[ii]);
}
}
public static void writeByteArray (ObjectOutputStream out, byte[] value)
throws IOException
{
int ecount = value.length;
out.writeInt(ecount);
out.write(value);
}
public static void writeCharArray (ObjectOutputStream out, char[] value)
throws IOException
{
int ecount = value.length;
out.writeInt(ecount);
for (int ii = 0; ii < ecount; ii++) {
out.writeChar(value[ii]);
}
}
public static void writeShortArray (ObjectOutputStream out, short[] value)
throws IOException
{
int ecount = value.length;
out.writeInt(ecount);
for (int ii = 0; ii < ecount; ii++) {
out.writeShort(value[ii]);
}
}
public static void writeIntArray (ObjectOutputStream out, int[] value)
throws IOException
{
int ecount = value.length;
out.writeInt(ecount);
for (int ii = 0; ii < ecount; ii++) {
out.writeInt(value[ii]);
}
}
public static void writeLongArray (ObjectOutputStream out, long[] value)
throws IOException
{
int ecount = value.length;
out.writeInt(ecount);
for (int ii = 0; ii < ecount; ii++) {
out.writeLong(value[ii]);
}
}
public static void writeFloatArray (ObjectOutputStream out, float[] value)
throws IOException
{
int ecount = value.length;
out.writeInt(ecount);
for (int ii = 0; ii < ecount; ii++) {
out.writeFloat(value[ii]);
}
}
public static void writeDoubleArray (ObjectOutputStream out, double[] value)
throws IOException
{
int ecount = value.length;
out.writeInt(ecount);
for (int ii = 0; ii < ecount; ii++) {
out.writeDouble(value[ii]);
}
}
public static void writeObjectArray (ObjectOutputStream out, Object[] value)
throws IOException
{
int ecount = value.length;
out.writeInt(ecount);
for (int ii = 0; ii < ecount; ii++) {
out.writeObject(value[ii]);
}
}
}
@@ -0,0 +1,115 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/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.IOException;
import java.io.InputStream;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.InvalidMarkException;
/**
* Reads input from a {@link ByteBuffer}.
*/
public class ByteBufferInputStream extends InputStream
{
/**
* Creates a new input stream to read from the specified buffer.
*/
public ByteBufferInputStream (ByteBuffer buffer)
{
_buffer = buffer;
}
/**
* Returns a reference to the underlying buffer.
*/
public ByteBuffer getBuffer ()
{
return _buffer;
}
@Override
public int read ()
{
try {
return (_buffer.get() & 0xFF);
} catch (BufferUnderflowException e) {
return -1;
}
}
@Override
public int read (byte[] b, int offset, int length)
throws IOException
{
length = Math.min(length, _buffer.remaining());
if (length <= 0) {
return -1;
}
_buffer.get(b, offset, length);
return length;
}
@Override
public long skip (long n)
throws IOException
{
n = Math.min(n, _buffer.remaining());
_buffer.position((int)(_buffer.position() + n));
return n;
}
@Override
public int available ()
{
return _buffer.remaining();
}
@Override
public boolean markSupported ()
{
return true;
}
@Override
public void mark (int readLimit)
{
_buffer.mark();
}
@Override
public void reset ()
throws IOException
{
try {
_buffer.reset();
} catch (InvalidMarkException e) {
throw new IOException("No mark set.");
}
}
/** The buffer from which we read. */
protected ByteBuffer _buffer;
}
@@ -0,0 +1,120 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/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.nio.BufferOverflowException;
import java.nio.ByteBuffer;
/**
* Stores output in an {@link ByteBuffer} that grows automatically to accommodate the data.
*/
public class ByteBufferOutputStream extends OutputStream
{
/**
* Creates a new byte buffer output stream.
*/
public ByteBufferOutputStream ()
{
_buffer = ByteBuffer.allocate(INITIAL_BUFFER_SIZE);
}
/**
* Returns a reference to the underlying buffer.
*/
public ByteBuffer getBuffer ()
{
return _buffer;
}
/**
* Flips and returns the buffer. The returned buffer will have a position of zero and a limit
* equal to the number of bytes written. Call {@link #reset} to reset the buffer before
* writing again.
*/
public ByteBuffer flip ()
{
_buffer.flip();
return _buffer;
}
/**
* Resets our internal buffer.
*/
public void reset ()
{
_buffer.clear();
}
@Override
public void write (int b)
{
try {
_buffer.put((byte)b);
} catch (BufferOverflowException boe) {
expand(1);
_buffer.put((byte)b);
}
}
@Override
public void write (byte[] b, int off, int len)
{
// sanity check the arguments
if ((off < 0) || (off > b.length) || (len < 0) ||
((off + len) > b.length) || ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return;
}
try {
_buffer.put(b, off, len);
} catch (BufferOverflowException boe) {
expand(len);
_buffer.put(b, off, len);
}
}
/**
* Expands our buffer to accomodate the specified capacity.
*/
protected final void expand (int needed)
{
int ocapacity = _buffer.capacity();
int ncapacity = _buffer.position() + needed;
if (ncapacity > ocapacity) {
// increase the buffer size in large increments
ncapacity = Math.max(ocapacity << 1, ncapacity);
ByteBuffer newbuf = ByteBuffer.allocate(ncapacity);
newbuf.put((ByteBuffer)_buffer.flip());
_buffer = newbuf;
}
}
/** The buffer in which we store our frame data. */
protected ByteBuffer _buffer;
/** The default initial size of the internal buffer. */
protected static final int INITIAL_BUFFER_SIZE = 32;
}
@@ -0,0 +1,46 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/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;
/**
* Used by the {@link ObjectOutputStream} and {@link ObjectInputStream} to
* map classes to codes during a session.
*/
class ClassMapping
{
public short code;
public Class<?> sclass;
public Streamer streamer;
public ClassMapping (short code, Class<?> sclass, Streamer streamer)
{
this.code = code;
this.sclass = sclass;
this.streamer = streamer;
}
@Override
public String toString ()
{
return "[code=" + code + ", class=" + sclass.getName() + "]";
}
}
@@ -0,0 +1,408 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/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.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ReflectPermission;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.google.common.collect.Maps;
import static com.threerings.NaryaLog.log;
/**
* Used to read and write a single field of a {@link Streamable} instance.
*/
public abstract class FieldMarshaller
{
public FieldMarshaller ()
{
this(null);
}
public FieldMarshaller (String type)
{
_type = type;
}
/**
* 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;
/**
* 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;
@Override
public String toString ()
{
if (_type != null) {
return "FieldMarshaller " + _type;
}
return super.toString();
}
protected final String _type;
/**
* 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)
{
if (_marshallers == null) {
createMarshallers();
}
// if necessary (we're running in a sandbox), look for custom field accessors
if (useFieldAccessors()) {
Method reader = null, writer = null;
try {
reader = field.getDeclaringClass().getMethod(
getReaderMethodName(field.getName()), READER_ARGS);
} catch (NoSuchMethodException nsme) {
// no problem
}
try {
writer = field.getDeclaringClass().getMethod(
getWriterMethodName(field.getName()), WRITER_ARGS);
} catch (NoSuchMethodException nsme) {
// no problem
}
if (reader != null && writer != null) {
return new MethodFieldMarshaller(reader, writer);
}
if ((reader == null && writer != null) || (writer == null && reader != null)) {
log.warning("Class contains one but not both custom field reader and writer",
"class", field.getDeclaringClass().getName(), "field", field.getName(),
"reader", reader, "writer", writer);
// fall through to using reflection on the fields...
}
}
Class<?> ftype = field.getType();
// use the intern marshaller for pooled strings
if (ftype == String.class && field.isAnnotationPresent(Intern.class)) {
return _internMarshaller;
}
// if we have an exact match, use that
FieldMarshaller fm = _marshallers.get(ftype);
// otherwise if the class is a pure interface or streamable, use the streamable marshaller
if (fm == null && (ftype.isInterface() || Streamer.isStreamable(ftype))) {
fm = _marshallers.get(Streamable.class);
}
return fm;
}
/**
* Returns the name of the custom reader method which will be used if it exists to stream a
* field with the supplied name.
*/
public static final String getReaderMethodName (String field)
{
return "readField_" + field;
}
/**
* Returns the name of the custom writer method which will be used if it exists to stream a
* field with the supplied name.
*/
public static final String getWriterMethodName (String field)
{
return "writeField_" + field;
}
/**
* Returns true if we should use the generated field marshaller methods that allow us to work
* around our inability to read and write protected and private fields of a {@link Streamable}.
*/
protected static boolean useFieldAccessors ()
{
try {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPermission(new ReflectPermission("suppressAccessChecks"));
}
return false;
} catch (SecurityException se) {
return true;
}
}
/**
* Used to marshall and unmarshall classes for which we have a basic {@link Streamer}.
*/
protected static class StreamerMarshaller extends FieldMarshaller
{
public StreamerMarshaller (Streamer streamer)
{
_streamer = streamer;
}
@Override
public void readField (Field field, Object target, ObjectInputStream in)
throws Exception
{
if (in.readBoolean()) {
Object value = _streamer.createObject(in);
_streamer.readObject(value, in, true);
field.set(target, value);
} else {
field.set(target, null);
}
}
@Override
public void writeField (Field field, Object source, ObjectOutputStream out)
throws Exception
{
Object value = field.get(source);
if (value == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
_streamer.writeObject(value, out, true);
}
}
@Override
public String toString ()
{
return "StreamerMarshaller:" + _streamer.toString();
}
/** The streamer we use to read and write our field. */
protected Streamer _streamer;
}
/**
* Uses custom accessor methods to read and write a field.
*/
protected static class MethodFieldMarshaller extends FieldMarshaller
{
public MethodFieldMarshaller (Method reader, Method writer)
{
_reader = reader;
_writer = writer;
}
@Override
public void readField (Field field, Object target, ObjectInputStream in)
throws Exception
{
_reader.invoke(target, in);
}
@Override
public void writeField (Field field, Object source, ObjectOutputStream out)
throws Exception
{
_writer.invoke(source, out);
}
protected Method _reader, _writer;
}
/**
* 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 ()
{
// create our table
_marshallers = Maps.newHashMap();
// create a generic marshaller for streamable instances
FieldMarshaller gmarsh = new FieldMarshaller("Generic") {
@Override
public void readField (Field field, Object target, ObjectInputStream in)
throws Exception {
field.set(target, in.readObject());
}
@Override
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 as 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);
// create marshallers for the primitive types
_marshallers.put(Boolean.TYPE, new FieldMarshaller("boolean") {
@Override
public void readField (Field field, Object target, ObjectInputStream in)
throws Exception {
field.setBoolean(target, in.readBoolean());
}
@Override
public void writeField (Field field, Object source, ObjectOutputStream out)
throws Exception {
out.writeBoolean(field.getBoolean(source));
}
});
_marshallers.put(Byte.TYPE, new FieldMarshaller("byte") {
@Override
public void readField (Field field, Object target, ObjectInputStream in)
throws Exception {
field.setByte(target, in.readByte());
}
@Override
public void writeField (Field field, Object source, ObjectOutputStream out)
throws Exception {
out.writeByte(field.getByte(source));
}
});
_marshallers.put(Character.TYPE, new FieldMarshaller("char") {
@Override
public void readField (Field field, Object target, ObjectInputStream in)
throws Exception {
field.setChar(target, in.readChar());
}
@Override
public void writeField (Field field, Object source, ObjectOutputStream out)
throws Exception {
out.writeChar(field.getChar(source));
}
});
_marshallers.put(Short.TYPE, new FieldMarshaller("short") {
@Override
public void readField (Field field, Object target, ObjectInputStream in)
throws Exception {
field.setShort(target, in.readShort());
}
@Override
public void writeField (Field field, Object source, ObjectOutputStream out)
throws Exception {
out.writeShort(field.getShort(source));
}
});
_marshallers.put(Integer.TYPE, new FieldMarshaller("int") {
@Override
public void readField (Field field, Object target, ObjectInputStream in)
throws Exception {
field.setInt(target, in.readInt());
}
@Override
public void writeField (Field field, Object source, ObjectOutputStream out)
throws Exception {
out.writeInt(field.getInt(source));
}
});
_marshallers.put(Long.TYPE, new FieldMarshaller("long") {
@Override
public void readField (Field field, Object target, ObjectInputStream in)
throws Exception {
field.setLong(target, in.readLong());
}
@Override
public void writeField (Field field, Object source, ObjectOutputStream out)
throws Exception {
out.writeLong(field.getLong(source));
}
});
_marshallers.put(Float.TYPE, new FieldMarshaller("float") {
@Override
public void readField (Field field, Object target, ObjectInputStream in)
throws Exception {
field.setFloat(target, in.readFloat());
}
@Override
public void writeField (Field field, Object source, ObjectOutputStream out)
throws Exception {
out.writeFloat(field.getFloat(source));
}
});
_marshallers.put(Double.TYPE, new FieldMarshaller("double") {
@Override
public void readField (Field field, Object target, ObjectInputStream in)
throws Exception {
field.setDouble(target, in.readDouble());
}
@Override
public void writeField (Field field, Object source, ObjectOutputStream out)
throws Exception {
out.writeDouble(field.getDouble(source));
}
});
_marshallers.put(Date.class, new FieldMarshaller("Date") {
@Override
public void readField (Field field, Object target, ObjectInputStream in)
throws Exception {
field.set(target, new Date(in.readLong()));
}
@Override
public void writeField (Field field, Object source, ObjectOutputStream out)
throws Exception {
out.writeLong(((Date)field.get(source)).getTime());
}
});
// create field marshallers for all of the basic types
for (Map.Entry<Class<?>,Streamer> entry : BasicStreamers.BSTREAMERS.entrySet()) {
_marshallers.put(entry.getKey(), new StreamerMarshaller(entry.getValue()));
}
// create the field marshaller for pooled strings
_internMarshaller = new FieldMarshaller("intern") {
@Override
public void readField (Field field, Object target, ObjectInputStream in)
throws Exception {
field.set(target, in.readIntern());
}
@Override
public void writeField (Field field, Object source, ObjectOutputStream out)
throws Exception {
out.writeIntern((String)field.get(source));
}
};
}
/** Contains a mapping from field type to field marshaller instance for that type. */
protected static HashMap<Class<?>, FieldMarshaller> _marshallers;
/** The field marshaller for pooled strings. */
protected static FieldMarshaller _internMarshaller;
/** Defines the signature to a custom field reader method. */
protected static final Class<?>[] READER_ARGS = { ObjectInputStream.class };
/** Defines the signature to a custom field writer method. */
protected static final Class<?>[] WRITER_ARGS = { ObjectOutputStream.class };
}
@@ -0,0 +1,323 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/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.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
/**
* The framed input stream reads input that was framed by a framing output
* stream. Framing in this case simply means writing the length of the
* frame followed by the data associated with the frame so that an entire
* frame can be loaded from the network layer before any higher layer
* attempts to process it. Additionally, any failure in decoding a frame
* won't result in the entire stream being skewed due to the remainder of
* the undecoded frame remaining in the input stream.
*
* <p>The framed input stream reads an entire frame worth of data into its
* internal buffer when <code>readFrame()</code> is called. It then
* behaves as if this is the only data available on the stream (meaning
* that when the data in the frame is exhausted, it will behave as if the
* end of the stream has been reached). The buffer can only contain a
* single frame at a time, so any data left over from a previous frame
* will disappear when <code>readFrame()</code> is called again.
*
* <p><em>Note:</em> The framing input stream does not synchronize reads
* from its internal buffer. It is intended to only be accessed from a
* single thread.
*
* <p>Implementation note: maybe this should derive from
* <code>FilterInputStream</code> and be tied to a single
* <code>InputStream</code> for its lifetime.
*/
public class FramedInputStream extends InputStream
{
/**
* Creates a new framed input stream.
*/
public FramedInputStream ()
{
_buffer = ByteBuffer.allocate(INITIAL_BUFFER_CAPACITY);
}
/**
* Reads a frame from the provided channel, appending to any partially
* read frame. If the entire frame data is not yet available,
* <code>readFrame</code> will return false, otherwise true.
*
* <p> <em>Note:</em> when this method returns true, it is required
* that the caller read <em>all</em> of the frame data from the stream
* before again calling {@link #readFrame} as the previous frame's
* data will be elimitated upon the subsequent call.
*
* @return true if the entire frame has been read, false if the buffer
* contains only a partial frame.
*/
public boolean readFrame (ReadableByteChannel source)
throws IOException
{
// flush data from any previous frame from the buffer
if (_buffer.limit() == _length) {
// this will remove the old frame's bytes from the buffer,
// shift our old data to the start of the buffer, position the
// buffer appropriately for appending new data onto the end of
// our existing data, and set the limit to the capacity
_buffer.limit(_have);
_buffer.position(_length);
_buffer.compact();
_have -= _length;
// we may have picked up the next frame in a previous read, so
// try decoding the length straight away
_length = decodeLength();
}
// we may already have the next frame entirely in the buffer from
// a previous read
if (checkForCompleteFrame()) {
return true;
}
// read whatever data we can from the source
do {
int got = source.read(_buffer);
if (got == -1) {
throw new EOFException();
}
_have += got;
if (_length == -1) {
// if we didn't already have our length, see if we now
// have enough data to obtain it
_length = decodeLength();
}
// if there's room remaining in the buffer, that means we've
// read all there is to read, so we can move on to inspecting
// what we've got
if (_buffer.remaining() > 0) {
break;
}
// additionally, if the buffer happened to be exactly as long
// as we needed, we need to break as well
if ((_length > 0) && (_have >= _length)) {
break;
}
// otherwise, we've filled up our buffer as a result of this
// read, expand it and try reading some more
ByteBuffer newbuf = ByteBuffer.allocate(_buffer.capacity() << 1);
newbuf.put((ByteBuffer)_buffer.flip());
_buffer = newbuf;
// don't let things grow without bounds
} while (_buffer.capacity() < MAX_BUFFER_CAPACITY);
// finally check to see if there's a complete frame in the buffer
// and prepare to serve it up if there is
return checkForCompleteFrame();
}
/**
* Decodes and returns the length of the current frame from the buffer
* if possible. Returns -1 otherwise.
*/
protected final int decodeLength ()
{
// if we don't have enough bytes to determine our frame size, stop
// here and let the caller know that we're not ready
if (_have < HEADER_SIZE) {
return -1;
}
// decode the frame length
_buffer.rewind();
int length = (_buffer.get() & 0xFF) << 24;
length += (_buffer.get() & 0xFF) << 16;
length += (_buffer.get() & 0xFF) << 8;
length += (_buffer.get() & 0xFF);
_buffer.position(_have);
return length;
}
/**
* Returns true if a complete frame is in the buffer, false otherwise.
* If a complete frame is in the buffer, the buffer will be prepared
* to deliver that frame via our {@link InputStream} interface.
*/
protected final boolean checkForCompleteFrame ()
{
if (_length == -1 || _have < _length) {
return false;
}
// prepare the buffer such that this frame can be read
_buffer.position(HEADER_SIZE);
_buffer.limit(_length);
return true;
}
/**
* Reads the next byte of data from this input stream. The value byte
* is returned as an <code>int</code> in the range <code>0</code> to
* <code>255</code>. If no byte is available because the end of the
* stream has been reached, the value <code>-1</code> is returned.
*
* <p>This <code>read</code> method cannot block.
*
* @return the next byte of data, or <code>-1</code> if the end of the
* stream has been reached.
*/
@Override
public int read ()
{
return (_buffer.remaining() > 0) ? (_buffer.get() & 0xFF) : -1;
}
/**
* Reads up to <code>len</code> bytes of data into an array of bytes
* from this input stream. If <code>pos</code> equals
* <code>count</code>, then <code>-1</code> is returned to indicate
* end of file. Otherwise, the number <code>k</code> of bytes read is
* equal to the smaller of <code>len</code> and
* <code>count-pos</code>. If <code>k</code> is positive, then bytes
* <code>buf[pos]</code> through <code>buf[pos+k-1]</code> are copied
* into <code>b[off]</code> through <code>b[off+k-1]</code> in the
* manner performed by <code>System.arraycopy</code>. The value
* <code>k</code> is added into <code>pos</code> and <code>k</code> is
* returned.
*
* <p>This <code>read</code> method cannot block.
*
* @param b the buffer into which the data is read.
* @param off the start offset of the data.
* @param len the maximum number of bytes read.
*
* @return the total number of bytes read into the buffer, or
* <code>-1</code> if there is no more data because the end of the
* stream has been reached.
*/
@Override
public int read (byte[] b, int off, int len)
{
// if they want no bytes, we give them no bytes; this is
// purportedly the right thing to do regardless of whether we're
// at EOF or not
if (len == 0) {
return 0;
}
// trim the amount to be read to what is available; if they wanted
// bytes and we have none, return -1 to indicate EOF
if ((len = Math.min(len, _buffer.remaining())) == 0) {
return -1;
}
_buffer.get(b, off, len);
return len;
}
/**
* Skips <code>n</code> bytes of input from this input stream. Fewer
* bytes might be skipped if the end of the input stream is reached.
* The actual number <code>k</code> of bytes to be skipped is equal to
* the smaller of <code>n</code> and <code>count-pos</code>. The value
* <code>k</code> is added into <code>pos</code> and <code>k</code> is
* returned.
*
* @param n the number of bytes to be skipped.
*
* @return the actual number of bytes skipped.
*/
@Override
public long skip (long n)
{
throw new UnsupportedOperationException();
}
/**
* Returns the number of bytes that can be read from this input stream
* without blocking.
*
* @return the number of bytes remaining to be read from the buffered
* frame.
*/
@Override
public int available ()
{
return _buffer.remaining();
}
/**
* Always returns false as framed input streams do not support
* marking.
*/
@Override
public boolean markSupported ()
{
return false;
}
/**
* Does nothing, as marking is not supported.
*/
@Override
public void mark (int readAheadLimit)
{
// not supported; do nothing
}
/**
* Resets the buffer to the beginning of the buffered frames.
*/
@Override
public void reset ()
{
// position our buffer at the beginning of the frame data
_buffer.position(HEADER_SIZE);
}
/** The buffer in which we maintain our frame data. */
protected ByteBuffer _buffer;
/** The length of the current frame being read. */
protected int _length = -1;
/** The number of bytes total that we have in our buffer (these bytes
* may comprise more than one frame. */
protected int _have = 0;
/** The size of the frame header (a 32-bit integer). */
protected static final int HEADER_SIZE = 4;
/** The default initial size of the internal buffer. */
protected static final int INITIAL_BUFFER_CAPACITY = 32;
/** No need to get out of hand. */
protected static final int MAX_BUFFER_CAPACITY = 512 * 1024;
}
@@ -0,0 +1,93 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/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.nio.ByteBuffer;
/**
* The framing output stream accumulates output into a byte array just
* like the byte array output stream, but can then be instructed to send
* its contents over a channel, prefixed by the length (written as an
* integer) of the entire frame (contents plus length prefix). It does
* this efficiently so that data is copied as little as possible and so
* that the output stream to which the data is written need not be
* buffered because the framed output is written in a single call to
* <code>write()</code>.
*
* <p><em>Note:</em> The framing output stream does not synchronize writes
* to its internal buffer. It is intended to only be accessed from a
* single thread.
*/
public class FramingOutputStream extends ByteBufferOutputStream
{
public FramingOutputStream ()
{
_buffer.put(HEADER_PAD);
}
@Override
public ByteBuffer flip ()
{
throw new UnsupportedOperationException("Use frameAndReturnBuffer() instead.");
}
@Override
public void reset ()
{
throw new UnsupportedOperationException("Use resetFrame() instead.");
}
/**
* Writes the frame length to the beginning of our buffer and returns
* it for writing to the appropriate channel. This should be followed
* by a call to {@link #resetFrame} when the frame has been written.
*/
public ByteBuffer frameAndReturnBuffer ()
{
// flip the buffer which will limit it to its current position
_buffer.flip();
// then write the frame length and rewind back to the start of the
// buffer so that all the data is available
int count = _buffer.limit();
_buffer.put((byte)((count >>> 24) & 0xFF));
_buffer.put((byte)((count >>> 16) & 0xFF));
_buffer.put((byte)((count >>> 8) & 0xFF));
_buffer.put((byte)((count >>> 0) & 0xFF));
_buffer.rewind();
return _buffer;
}
/**
* Resets our internal buffer and prepares to write a new frame.
*/
public void resetFrame ()
{
_buffer.clear();
_buffer.put(HEADER_PAD);
}
/** We pad the beginning of our buffer so that we can write the frame
* length when the time comes. */
protected static final byte[] HEADER_PAD = new byte[4];
}
@@ -0,0 +1,38 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/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.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Flags a {@link String} field in a streamable object as being a member of the global string pool
* accessed using {@link String#intern}. Each unique value will be streamed only once, with
* further instances replaced by a compact reference.
*/
@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface Intern
{
}
@@ -0,0 +1,36 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/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.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* This annotation specifies that the property or field is not Streamable.
*/
@Target(value=ElementType.FIELD)
@Retention(value=RetentionPolicy.RUNTIME)
public @interface NotStreamable
{
}
@@ -0,0 +1,359 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/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.util.ArrayList;
import java.util.HashMap;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.samskivert.util.StringUtil;
import static com.threerings.NaryaLog.log;
/**
* Used to read {@link Streamable} objects from an {@link InputStream}. Other common object types
* are supported as well (@see {@link ObjectOutputStream}).
*
* @see Streamable
*/
public class ObjectInputStream extends DataInputStream
{
/**
* Constructs an object input stream which will read its data from the supplied source stream.
*/
public ObjectInputStream (InputStream source)
{
super(source);
}
/**
* Customizes the class loader used to instantiate objects read from the input stream.
*/
public void setClassLoader (ClassLoader loader)
{
_loader = loader;
}
/**
* Configures this object input stream with a mapping from an old class name to a new
* one. Serialized instances of the old class name will use the new class name when
* unserializing.
*/
public void addTranslation (String oldname, String newname)
{
if (_translations == null) {
_translations = Maps.newHashMap();
}
_translations.put(oldname, newname);
}
/**
* Reads a {@link Streamable} instance or one of the supported object types from the input
* stream.
*/
public Object readObject ()
throws IOException, ClassNotFoundException
{
try {
// read the class mapping
ClassMapping cmap = readClassMapping();
if (cmap == null) {
if (STREAM_DEBUG) {
log.info(hashCode() + ": Read null.");
}
return null;
}
if (STREAM_DEBUG) {
log.info(hashCode() + ": Reading with " + cmap.streamer + ".");
}
// create an instance of the appropriate object
Object target = cmap.streamer.createObject(this);
readBareObject(target, cmap.streamer, true);
return target;
} catch (OutOfMemoryError oome) {
throw (IOException)new IOException("Malformed object data").initCause(oome);
}
}
/**
* Reads a pooled string value from the input stream.
*/
public String readIntern ()
throws IOException
{
// create our intern map if necessary
if (_internmap == null) {
_internmap = Lists.newArrayList();
// insert a zeroth element
_internmap.add(null);
}
// read in the intern code for this instance
short code = readShort();
// a zero code indicates a null value
if (code == 0) {
return null;
// if the code is negative, that means that we've never seen if before and value follows
} else if (code < 0) {
// first swap the code into positive-land
code *= -1;
// read in the value
String value = readUTF().intern();
// create the mapping and return the value
mapIntern(code, value);
return value;
} else {
String value = (code < _internmap.size()) ? _internmap.get(code) : null;
// sanity check
if (value == null) {
// this will help with debugging
log.warning("Internal stream error, no intern value", "code", code,
"ois", this, new Exception());
log.warning("ObjectInputStream mappings", "map", _internmap);
String errmsg = "Read intern code for which we have no registered value " +
"metadata [code=" + code + "]";
throw new RuntimeException(errmsg);
}
return value;
}
}
/**
* Adds the intern mapping for the specified code and value.
*/
protected void mapIntern (short code, String value)
throws IOException
{
_internmap.add(code, value);
}
/**
* Reads a class mapping from the stream.
*
* @return the class mapping, or <code>null</code> to represent a null value.
*/
protected ClassMapping readClassMapping ()
throws IOException, ClassNotFoundException
{
// create our classmap if necessary
if (_classmap == null) {
_classmap = Lists.newArrayList();
// insert a zeroth element
_classmap.add(null);
}
// read in the class code for this instance
short code = readShort();
// a zero code indicates a null value
if (code == 0) {
return null;
// if the code is negative, that means that we've never seen it before and class
// metadata follows
} else if (code < 0) {
// first swap the code into positive-land
code *= -1;
// read in the class metadata
String cname = readUTF();
// if we have a translation (used to cope when serialized classes are renamed) use
// it
if (_translations != null) {
String tname = _translations.get(cname);
if (tname != null) {
cname = tname;
}
}
// create the class mapping
return mapClass(code, cname);
} else {
ClassMapping cmap = (code < _classmap.size()) ? _classmap.get(code) : null;
// sanity check
if (cmap == null) {
// this will help with debugging
log.warning("Internal stream error, no class metadata", "code", code,
"ois", this, new Exception());
log.warning("ObjectInputStream mappings", "map", _classmap);
String errmsg = "Read object code for which we have no registered class " +
"metadata [code=" + code + "]";
throw new RuntimeException(errmsg);
}
return cmap;
}
}
/**
* Creates, adds, and returns the class mapping for the specified code and class name.
*/
protected ClassMapping mapClass (short code, String cname)
throws IOException, ClassNotFoundException
{
// create a class mapping record, and cache it
ClassMapping cmap = createClassMapping(code, cname);
_classmap.add(code, cmap);
return cmap;
}
/**
* Creates and returns a class mapping for the specified code and class name.
*/
protected ClassMapping createClassMapping (short code, String cname)
throws IOException, ClassNotFoundException
{
// resolve the class and streamer
ClassLoader loader = (_loader != null) ? _loader :
Thread.currentThread().getContextClassLoader();
Class<?> sclass = Class.forName(cname, true, loader);
Streamer streamer = Streamer.getStreamer(sclass);
if (STREAM_DEBUG) {
log.info(hashCode() + ": New class '" + cname + "'", "code", code);
}
// sanity check
if (streamer == null) {
String errmsg = "Aiya! Unable to create streamer for newly seen class " +
"[code=" + code + ", class=" + cname + "]";
throw new RuntimeException(errmsg);
}
return new ClassMapping(code, sclass, streamer);
}
/**
* Reads an object from the input stream that was previously written with {@link
* ObjectOutputStream#writeBareObject(Object)}.
*
* @param object the object to be populated from data on the stream. It cannot be
* <code>null</code>.
*/
public void readBareObject (Object object)
throws IOException, ClassNotFoundException
{
readBareObject(object, Streamer.getStreamer(object.getClass()), true);
}
/**
* Reads an object from the input stream that was previously written with {@link
* ObjectOutputStream#writeBareObject(Object,Streamer,boolean)}.
*/
protected void readBareObject (Object object, Streamer streamer, boolean useReader)
throws IOException, ClassNotFoundException
{
_current = object;
_streamer = streamer;
try {
_streamer.readObject(object, this, useReader);
} finally {
// clear out our current object references
_current = null;
_streamer = null;
}
}
/**
* Reads the fields of the specified {@link Streamable} instance from the input stream using
* the default object streaming mechanisms (a call is not made to <code>readObject()</code>,
* even if such a method exists).
*/
public void defaultReadObject ()
throws IOException, ClassNotFoundException
{
// sanity check
if (_current == null) {
throw new RuntimeException("defaultReadObject() called illegally.");
}
// read the instance data
_streamer.readObject(_current, this, false);
}
/**
* Read a string encoded as real UTF-8 (rather than the modified format handled by
* {link #readUTF}).
*/
public String readUnmodifiedUTF ()
throws IOException
{
// find out how many raw bytes of UTF8 data there is
int utflen = readUnsignedShort();
// read precisely that many into a buffer
byte[] bbuf = new byte[utflen];
in.read(bbuf);
// decode the UTF-8 stream into a character buffer
char[] cbuf = new char[utflen];
int read = new InputStreamReader(new ByteArrayInputStream(bbuf), "UTF-8").read(cbuf);
// create and return the string given the number of decoded characters
return String.copyValueOf(cbuf, 0, read);
}
@Override
public String toString ()
{
return "[hash=" + hashCode() + ", mappings=" + _classmap.size() +
", current=" + StringUtil.safeToString(_current) + ", streamer=" + _streamer + "]";
}
/** Used to map classes to numeric codes and the {@link Streamer} instance used to write
* them. */
protected ArrayList<ClassMapping> _classmap;
/** Maps numeric codes to pooled strings. */
protected ArrayList<String> _internmap;
/** The object currently being read from the stream. */
protected Object _current;
/** The streamer being used currently. */
protected Streamer _streamer;
/** If set, an overridden class loader used to instantiate objects. */
protected ClassLoader _loader;
/** An optional set of class name translations to use when unserializing objects. */
protected HashMap<String, String> _translations;
/** Used to activate verbose debug logging. */
protected static final boolean STREAM_DEBUG = false;
}
@@ -0,0 +1,338 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/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.util.Map;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import com.google.common.collect.Maps;
import static com.threerings.NaryaLog.log;
/**
* Used to write {@link Streamable} objects to an {@link OutputStream}. Other common object types
* are supported as well: <code>Boolean, Byte, Character, Short, Integer, Long, Float, Double,
* boolean[], byte[], char[], short[], int[], long[], float[], double[], Object[]</code>.
*
* @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 = Maps.newHashMap();
}
_translations.put(className, streamedName);
}
/**
* Writes a {@link Streamable} instance or one of the support object types to the output
* stream.
*/
public void writeObject (Object object)
throws IOException
{
// if the object to be written is null, simply write a zero
if (object == null) {
writeShort(0);
return;
}
// otherwise, write the class mapping, then the bare object
Class<?> sclass = Streamer.getStreamerClass(object);
ClassMapping cmap = writeClassMapping(sclass);
writeBareObject(object, cmap.streamer, true);
}
/**
* Writes a pooled string value to the output stream.
*/
public void writeIntern (String value)
throws IOException
{
// if the value to be written is null, simply write a zero
if (value == null) {
writeShort(0);
return;
}
// create our intern map if necessary
if (_internmap == null) {
_internmap = Maps.newHashMap();
}
// look up the intern mapping record
Short code = _internmap.get(value);
// create a mapping for the value if we've not got one
if (code == null) {
if (ObjectInputStream.STREAM_DEBUG) {
log.info(hashCode() + ": Creating intern mapping", "code", _nextInternCode,
"value", value);
}
code = createInternMapping(_nextInternCode++);
_internmap.put(value.intern(), code);
// make sure we didn't blow past our maximum intern count
if (_nextInternCode <= 0) {
throw new RuntimeException("Too many unique interns written to ObjectOutputStream");
}
writeNewInternMapping(code, value);
} else {
writeExistingInternMapping(code, value);
}
}
/**
* Creates and returns a new intern mapping.
*/
protected Short createInternMapping (short code)
{
return code;
}
/**
* Writes a new intern mapping to the stream.
*/
protected void writeNewInternMapping (short code, String value)
throws IOException
{
// writing a negative code indicates that the value will follow
writeInternMapping(-code, value);
}
/**
* Writes an existing intern mapping to the stream.
*/
protected void writeExistingInternMapping (short code, String value)
throws IOException
{
writeShort(code);
}
/**
* Writes out the mapping for an intern.
*/
protected void writeInternMapping (int code, String value)
throws IOException
{
writeShort(code);
writeUTF(value);
}
/**
* Retrieves or creates the class mapping for the supplied class, writes it out to the stream,
* and returns a reference to it.
*/
protected ClassMapping writeClassMapping (Class<?> sclass)
throws IOException
{
// create our classmap if necessary
if (_classmap == null) {
_classmap = Maps.newHashMap();
}
// look up the class mapping record
ClassMapping cmap = _classmap.get(sclass);
// create a class mapping for this class if we've not got one
if (cmap == null) {
// create a streamer instance and assign a code to this class
Streamer streamer = Streamer.getStreamer(sclass);
// we specifically do not inline the getStreamer() call into the ClassMapping
// constructor because we want to be sure not to call _nextClassCode++ if getStreamer()
// throws an exception
if (ObjectInputStream.STREAM_DEBUG) {
log.info(hashCode() + ": Creating class mapping", "code", _nextClassCode,
"class", sclass.getName());
}
cmap = createClassMapping(_nextClassCode++, sclass, streamer);
_classmap.put(sclass, cmap);
// make sure we didn't blow past our maximum class count
if (_nextClassCode <= 0) {
throw new RuntimeException("Too many unique classes written to ObjectOutputStream");
}
writeNewClassMapping(cmap);
} else {
writeExistingClassMapping(cmap);
}
return cmap;
}
/**
* Creates and returns a new class mapping.
*/
protected ClassMapping createClassMapping (short code, Class<?> sclass, Streamer streamer)
{
return new ClassMapping(code, sclass, streamer);
}
/**
* Writes a new class mapping to the stream.
*/
protected void writeNewClassMapping (ClassMapping cmap)
throws IOException
{
// writing a negative class code indicates that the class name will follow
writeClassMapping(-cmap.code, cmap.sclass);
}
/**
* Writes an existing class mapping to the stream.
*/
protected void writeExistingClassMapping (ClassMapping cmap)
throws IOException
{
writeShort(cmap.code);
}
/**
* Writes out the mapping for a class.
*/
protected void writeClassMapping (int code, Class<?> sclass)
throws IOException
{
writeShort(code);
String cname = sclass.getName();
if (_translations != null) {
String tname = _translations.get(cname);
if (tname != null) {
cname = tname;
}
}
writeUTF(cname);
}
/**
* Writes a {@link Streamable} instance or one of the support object types <em>without
* associated class metadata</em> to the output stream. The caller is responsible for knowing
* the exact class of the written object, creating an instance of such and calling {@link
* ObjectInputStream#readBareObject(Object)} to read its data from the stream.
*
* @param object the object to be written. It cannot be <code>null</code>.
*/
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 <code>writeObject</code> 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);
}
/**
* Write a string encoded as real UTF-8 (rather than the modified format handled by
* {link #writeUTF}).
*/
public void writeUnmodifiedUTF (String str)
throws IOException
{
// prepare a buffer to accept the encoded UTF-8
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(baos, "UTF-8");
// do the deed
osw.write(str);
osw.flush();
// now that we know how many bytes we'll need, write that number to the stream
writeShort(baos.size());
// then finally the bytes themselves
write(baos.toByteArray());
}
/** Used to map classes to numeric codes and the {@link Streamer} instance used to write
* them. */
protected Map<Class<?>, ClassMapping> _classmap;
/** Used to map pooled strings to numeric codes. */
protected Map<String, Short> _internmap;
/** A counter used to assign codes to streamed classes. */
protected short _nextClassCode = 1;
/** A counter used to assign codes to pooled strings. */
protected short _nextInternCode = 1;
/** The object currently being written to the stream. */
protected Object _current;
/** The streamer being used currently. */
protected Streamer _streamer;
/** An optional set of class name translations to use when serializing objects. */
protected Map<String, String> _translations;
}
@@ -0,0 +1,52 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/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 com.samskivert.util.StringUtil;
import com.threerings.util.ActionScript;
/**
* A simple serializable object implements the {@link Streamable}
* interface and provides a default {@link Object#toString} implementation which
* outputs all public members.
*/
public class SimpleStreamableObject implements Streamable
{
@Override
public String toString ()
{
StringBuilder buf = new StringBuilder("[");
toString(buf);
return buf.append("]").toString();
}
/**
* Handles the toString-ification of all public members. Derived
* classes can override and include non-public members if desired.
*/
@ActionScript(name="toStringBuilder")
protected void toString (StringBuilder buf)
{
StringUtil.fieldsToString(buf, this);
}
}
@@ -0,0 +1,44 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/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;
/**
* Marks an object as streamable, meaning that it can be written to {@link ObjectOutputStream}
* instances and read from {@link ObjectInputStream} instances.
*
* <p> All non-<code>transient</code> fields will be automatically written and restored for a
* {@link Streamable} instance. Classes that wish to stream transient fields or customize the
* streaming process should implement methods with the following signatures:
*
* <p><code>
* public void writeObject ({@link ObjectOutputStream} out);
* public void readObject ({@link ObjectInputStream} in);
* </code>
*
* <p> They can then handle the entirety of the streaming process, or call {@link
* ObjectOutputStream#defaultWriteObject} and {@link ObjectInputStream#defaultReadObject} from
* within their <code>writeObject</code> and <code>readObject</code> methods to perform the
* standard streaming in addition to their customized behavior.
*/
public interface Streamable
{
}
@@ -0,0 +1,537 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/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.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.samskivert.util.ByteEnum;
import com.samskivert.util.ByteEnumUtil;
import com.samskivert.util.ClassUtil;
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.
*/
public class Streamer
{
/**
* Returns true if the supplied target class can be streamed using a streamer.
*/
public synchronized static boolean isStreamable (Class<?> target)
{
while (true) {
// if we've got a streamer for it, it's good
if (_streamers.containsKey(target)) {
return true;
}
// enums can be streamed
if (target.isEnum()) {
return true;
}
// if it's not an array, it must be streamable
if (!target.isArray()) {
return Streamable.class.isAssignableFrom(target);
}
// 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.
*/
public static Class<?> getStreamerClass (Object object)
{
return (object instanceof Enum<?>) ?
((Enum<?>)object).getDeclaringClass() : object.getClass();
}
/**
* 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.
*
* @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
{
// if we have not yet initialized ourselves, do so now
if (_streamers == null) {
_streamers = Maps.newHashMap(BasicStreamers.BSTREAMERS);
}
Streamer stream = _streamers.get(target);
if (stream == null) {
// make sure this is a streamable class
if (!isStreamable(target)) {
throw new IOException(
"Requested to stream invalid class '" + target.getName() + "'");
}
// create a streamer for this class and cache it
if (ObjectInputStream.STREAM_DEBUG) {
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
try {
stream = AccessController.doPrivileged(new PrivilegedExceptionAction<Streamer>() {
public Streamer run () throws IOException {
return new Streamer(target);
}
});
} catch (PrivilegedActionException pae) {
throw (IOException) pae.getCause();
}
_streamers.put(target, stream);
}
return stream;
}
/**
* Writes the supplied object to the specified stream.
*
* @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.
*/
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());
}
_writer.invoke(object, new Object[] { out });
} catch (Throwable t) {
if (t instanceof InvocationTargetException) {
t = ((InvocationTargetException)t).getTargetException();
}
if (t instanceof IOException) {
throw (IOException)t;
}
String errmsg = "Failure invoking streamable writer " +
"[class=" + _target.getName() + "]";
throw (IOException) new IOException(errmsg).initCause(t);
}
return;
}
// if we're writing an array, do some special business
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
int cmods = _target.getComponentType().getModifiers();
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) {
mask.set(ii);
}
}
// 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);
if (element != null) {
out.writeBareObject(element, _delegate, useWriter);
}
}
} 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
for (int ii = 0; ii < length; ii++) {
out.writeObject(Array.get(object, ii));
}
}
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 (_target.isEnum()) {
if (object instanceof ByteEnum) {
out.writeByte(((ByteEnum) object).toByte());
} else {
out.writeUTF(((Enum<?>)object).name());
}
return;
}
// otherwise simply write out the fields via our field marshallers
int fcount = _fields.length;
for (int ii = 0; ii < fcount; ii++) {
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() + "]";
throw new IOException(errmsg);
}
try {
if (ObjectInputStream.STREAM_DEBUG) {
log.info("Writing field", "class", _target.getName(), "field", field.getName());
}
fm.writeField(field, object, out);
} catch (Exception e) {
String errmsg = "Failure writing streamable field [class=" + _target.getName() +
", field=" + field.getName() + "]";
throw (IOException) new IOException(errmsg).initCause(e);
}
}
}
/**
* 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, ClassNotFoundException
{
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 (_target.isArray()) {
int length = in.readInt();
if (ObjectInputStream.STREAM_DEBUG) {
log.info(in.hashCode() + ": Creating array '" +
_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() + "'.");
}
@SuppressWarnings("unchecked") Class<EnumReader> eclass =
(Class<EnumReader>)_target;
if (ByteEnum.class.isAssignableFrom(_target)) {
return ByteEnumUtil.fromByte(eclass, in.readByte());
} else {
return Enum.valueOf(eclass, in.readUTF());
}
} else {
if (ObjectInputStream.STREAM_DEBUG) {
log.info(in.hashCode() + ": Creating object '" + _target.getName() + "'.");
}
return _target.newInstance();
}
} catch (InstantiationException ie) {
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() + "]";
throw (IOException) new IOException(errmsg).initCause(iae);
}
}
/**
* 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.
*/
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() + "." +
_reader.getName() + "()'.");
}
_reader.invoke(object, new Object[] { in });
} catch (Throwable t) {
if (t instanceof InvocationTargetException) {
t = ((InvocationTargetException)t).getTargetException();
}
if (t instanceof IOException) {
throw (IOException)t;
}
String errmsg = "Failure invoking streamable reader " +
"[class=" + _target.getName() + "]";
throw (IOException) new IOException(errmsg).initCause(t);
}
return;
}
if (ObjectInputStream.STREAM_DEBUG) {
log.info(in.hashCode() + ": Reading '" + _target.getName() + "'.");
}
// 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
int cmods = _target.getComponentType().getModifiers();
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
for (int ii = 0; ii < length; ii++) {
if (mask.isSet(ii)) {
if (ObjectInputStream.STREAM_DEBUG) {
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 + "'.");
}
}
} else {
// 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 + "'.");
}
Array.set(object, ii, in.readObject());
}
}
return;
}
// an enum has no additional state
if (_target.isEnum()) {
return;
}
// otherwise simply read the fields via our field marshallers
int fcount = _fields.length;
for (int ii = 0; ii < fcount; ii++) {
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() + "]";
throw new IOException(errmsg);
}
try {
if (ObjectInputStream.STREAM_DEBUG) {
log.info(in.hashCode() + ": Reading field '" + field.getName() + "' " +
"with " + fm + ".");
}
// gracefully deal with objects that have had new fields added to their class
// definition
if (in.available() > 0) {
fm.readField(field, object, in);
} else {
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 + "]";
throw (IOException) new IOException(errmsg).initCause(e);
}
}
if (ObjectInputStream.STREAM_DEBUG) {
log.info(in.hashCode() + ": Read object '" + object + "'.");
}
}
@Override
public String toString ()
{
return (_target == null) ? getClass().getName() :
"[target=" + _target.getName() + ", delegate=" + _delegate +
", fcount=" + (_fields == null ? 0 : _fields.length) +
", reader=" + _reader + ", writer=" + _writer + "]";
}
/**
* The constructor used by the basic streamers.
*/
protected Streamer ()
{
}
/**
* Constructs a streamer for the specified target class.
*/
protected Streamer (Class<?> target)
throws IOException
{
// keep a handle on the class
_target = target;
// if this is a non-static inner class, freak out because we cannot stream those
boolean isInner = false, isStatic = Modifier.isStatic(_target.getModifiers());
try {
isInner = (_target.getDeclaringClass() != null);
} catch (Throwable t) {
log.warning("Failure checking innerness of class", "class", _target.getName(),
"error", t);
}
if (isInner && !isStatic) {
throw new IllegalArgumentException(
"Cannot stream non-static inner class: " + _target.getName());
}
// 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() + "]";
throw new RuntimeException(errmsg);
}
// and that's all we'll need
return;
}
// look up the reader and writer methods
try {
_reader = target.getMethod(READER_METHOD_NAME, READER_ARGS);
} catch (NoSuchMethodException nsme) {
// nothing to worry about, we just don't have one
}
try {
_writer = target.getMethod(WRITER_METHOD_NAME, WRITER_ARGS);
} catch (NoSuchMethodException nsme) {
// nothing to worry about, we just don't have one
}
// reflect on all the object's fields and remove all marked with NotStreamable
List<Field> fields = Lists.newArrayList();
ClassUtil.getFields(target, fields);
_fields = Iterables.toArray(Iterables.filter(fields, _isStreamableFieldPred), Field.class);
int fcount = _fields.length;
// obtain field marshallers for all of our fields
_marshallers = new FieldMarshaller[fcount];
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() + ".");
}
}
}
/** Used to coerce the type system into quietude when reading enums from the wire. */
protected static enum EnumReader implements ByteEnum {
NOT_USED;
public byte toByte () { return 0; }
}
/** 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. */
protected Streamer _delegate;
/** 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. */
protected FieldMarshaller[] _marshallers;
/** 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. */
protected Method _writer;
/** Contains the mapping from class names to configured streamer instances. */
protected static Map<Class<?>, Streamer> _streamers;
/** A simple predicate to filter "NotStreamable" members from a Streamable object's fields. */
protected static final Predicate<Field> _isStreamableFieldPred = new Predicate<Field>() {
public boolean apply (Field obj) {
return (obj.getAnnotation(NotStreamable.class) == null);
}
};
/** The name of the custom reader method. */
protected static final String READER_METHOD_NAME = "readObject";
/** The argument list for the custom reader method. */
protected static final Class<?>[] READER_ARGS = { ObjectInputStream.class };
/** The name of the custom writer method. */
protected static final String WRITER_METHOD_NAME = "writeObject";
/** The argument list for the custom writer method. */
protected static final Class<?>[] WRITER_ARGS = { ObjectOutputStream.class };
}
@@ -0,0 +1,85 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/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.IOException;
import java.io.InputStream;
/**
* The counterpart of {@link UnreliableObjectOutputStream}.
*/
public class UnreliableObjectInputStream extends ObjectInputStream
{
/**
* Constructs an object input stream which will read its data from the supplied source stream.
*/
public UnreliableObjectInputStream (InputStream source)
{
super(source);
}
@Override
protected ClassMapping mapClass (short code, String cname)
throws IOException, ClassNotFoundException
{
// see if we already have a mapping
ClassMapping cmap = (code < _classmap.size()) ? _classmap.get(code) : null;
if (cmap != null) {
// sanity check
if (!cmap.sclass.getName().equals(cname)) {
throw new RuntimeException(
"Received mapping for class that conflicts with existing mapping " +
"[code=" + code + ", oclass=" + cmap.sclass.getName() + ", nclass=" +
cname + "]");
}
return cmap;
}
// insert null entries for missing mappings
cmap = createClassMapping(code, cname);
for (int ii = 0, nn = (code + 1) - _classmap.size(); ii < nn; ii++) {
_classmap.add(null);
}
_classmap.set(code, cmap);
return cmap;
}
@Override
protected void mapIntern (short code, String value)
{
// see if we already have a mapping
String ovalue = (code < _internmap.size()) ? _internmap.get(code) : null;
if (ovalue != null) {
// sanity check
if (!ovalue.equals(value)) {
throw new RuntimeException(
"Received mapping for intern that conflicts with existing mapping " +
"[code=" + code + ", ovalue=" + ovalue + ", nvalue=" + value + "]");
}
return;
}
// insert null entries for missing mappings
for (int ii = 0, nn = (code + 1) - _internmap.size(); ii < nn; ii++) {
_internmap.add(null);
}
_internmap.set(code, value);
}
}
@@ -0,0 +1,219 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/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.util.Collection;
import java.util.Set;
import java.io.IOException;
import java.io.OutputStream;
import com.google.common.collect.Sets;
/**
* Extends {@link ObjectOutputStream} for use in unreliable channels, where we must transmit class
* mappings with every object until we are explicitly notified that the receiver has cached the
* mappings.
*/
public class UnreliableObjectOutputStream extends ObjectOutputStream
{
/**
* Constructs an object output stream which will write its data to the supplied target stream.
*/
public UnreliableObjectOutputStream (OutputStream target)
{
super(target);
}
/**
* Sets the reference to the set that will hold the classes for which mappings have been
* written.
*/
public void setMappedClasses (Set<Class<?>> mappedClasses)
{
_mappedClasses = mappedClasses;
}
/**
* Returns a reference to the set of classes for which mappings have been written.
*/
public Set<Class<?>> getMappedClasses ()
{
return _mappedClasses;
}
/**
* Sets the reference to the set that will hold the pooled strings for which mappings have been
* written.
*/
public void setMappedInterns (Set<String> mappedInterns)
{
_mappedInterns = mappedInterns;
}
/**
* Returns a reference to the set of pooled strings for which mappings have been written.
*/
public Set<String> getMappedInterns ()
{
return _mappedInterns;
}
/**
* Notes that the receiver has received the mappings for a group of classes and thus that from
* now on, only the codes need be sent.
*/
public void noteClassMappingsReceived (Collection<Class<?>> sclasses)
{
// sanity check
if (_classmap == null) {
throw new RuntimeException("Missing class map");
}
// make each class's code positive to signify that we no longer need to send metadata
for (Class<?> sclass : sclasses) {
ClassMapping cmap = _classmap.get(sclass);
if (cmap == null) {
throw new RuntimeException("No class mapping for " + sclass.getName());
}
cmap.code = (short)Math.abs(cmap.code);
}
}
/**
* Notes that the receiver has received the mappings for a group of interns and thus that from
* now on, only the codes need be sent.
*/
public void noteInternMappingsReceived (Collection<String> sinterns)
{
// sanity check
if (_internmap == null) {
throw new RuntimeException("Missing intern map");
}
// make each intern's code positive to signify that we no longer need to send metadata
for (String sintern : sinterns) {
Short code = _internmap.get(sintern);
if (code == null) {
throw new RuntimeException("No intern mapping for " + sintern);
}
short scode = code;
if (scode < 0) {
_internmap.put(sintern, (short)-code);
}
}
}
@Override
protected Short createInternMapping (short code)
{
// the negative intern code indicates that we must rewrite the metadata for the first
// instance in each go-round; when we are notified that the other side has cached the
// mapping, we can simply write the (positive) code
return (short)-code;
}
@Override
protected void writeNewInternMapping (short code, String value)
throws IOException
{
writeInternMapping(code, value);
}
@Override
protected void writeExistingInternMapping (short code, String value)
throws IOException
{
// if the other side has received the mapping, we need only send the reference
if (code > 0) {
writeShort(code);
// likewise if we've written the value once this go-round
} else if (_mappedInterns.contains(value)) {
writeShort(-code);
// otherwise, we must retransmit the mapping
} else {
writeInternMapping(code, value);
}
}
@Override
protected void writeInternMapping (int code, String value)
throws IOException
{
super.writeInternMapping(code, value);
// note that we've written the mapping
_mappedInterns.add(value);
}
@Override
protected ClassMapping createClassMapping (short code, Class<?> sclass, Streamer streamer)
{
// the negative class code indicates that we must rewrite the metadata for the first
// instance in each go-round; when we are notified that the other side has cached the
// mapping, we can simply write the (positive) code
return new ClassMapping((short)(-code), sclass, streamer);
}
@Override
protected void writeNewClassMapping (ClassMapping cmap)
throws IOException
{
writeClassMapping(cmap.code, cmap.sclass);
}
@Override
protected void writeExistingClassMapping (ClassMapping cmap)
throws IOException
{
// if the other side has received the mapping, we need only send the reference
if (cmap.code > 0) {
writeShort(cmap.code);
// likewise if we've written the class once this go-round
} else if (_mappedClasses.contains(cmap.sclass)) {
writeShort(-cmap.code);
// otherwise, we must retransmit the mapping
} else {
writeClassMapping(cmap.code, cmap.sclass);
}
}
@Override
protected void writeClassMapping (int code, Class<?> sclass)
throws IOException
{
super.writeClassMapping(code, sclass);
// note that we've written the mapping
_mappedClasses.add(sclass);
}
/** The set of classes for which we have written mappings. */
protected Set<Class<?>> _mappedClasses = Sets.newHashSet();
/** The set of pooled strings for which we have written mappings. */
protected Set<String> _mappedInterns = Sets.newHashSet();
}