
com.threerings.io.BasicStreamers Maven / Gradle / Ivy
//
// $Id: BasicStreamers.java 6522 2011-03-08 00:48:39Z ray $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2011 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.List;
import java.util.Map;
import java.util.Set;
import java.io.EOFException;
import java.io.IOException;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multiset;
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, Streamer> BSTREAMERS =
ImmutableMap., 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(Collection.class, ListStreamer.INSTANCE)
.put(Set.class, new SetStreamer())
.put(Map.class, new MapStreamer())
.put(Multiset.class, new MultisetStreamer())
.put(Iterable.class, new IterableStreamer())
.build();
/** Abstract base class for basic streamers. */
public abstract static class BasicStreamer extends Streamer
{
@Override
public void readObject (Object object, ObjectInputStream in, boolean useReader)
throws IOException, ClassNotFoundException
{
// nothing to do here
}
}
/** 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
{
/** The ordering for Collection/Iterable classes, most to least specific. */
public static final List> SPECIFICITY_ORDER = ImmutableList.of(
/** Pretty specific. */
List.class, Map.class, Set.class, Multiset.class,
/** General. */
Collection.class,
/** Last resort. */
Iterable.class);
@Override
public Object createObject (ObjectInputStream in)
throws IOException, ClassNotFoundException
{
int size = in.readInt();
Collection
© 2015 - 2025 Weber Informatics LLC | Privacy Policy