From b48e5b90a6393d5aa766b841f58c7dd578c7478d Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Thu, 4 Oct 2001 23:02:07 +0000 Subject: [PATCH] Created a list class that can be used to send around an ordered collection of streamable objects. (They are manipulated all as one unit. For fine grained distributed set management, use a DSet.) git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@396 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../presents/util/StreamableArrayList.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/java/com/threerings/presents/util/StreamableArrayList.java diff --git a/src/java/com/threerings/presents/util/StreamableArrayList.java b/src/java/com/threerings/presents/util/StreamableArrayList.java new file mode 100644 index 000000000..0de48d4d8 --- /dev/null +++ b/src/java/com/threerings/presents/util/StreamableArrayList.java @@ -0,0 +1,74 @@ +// +// $Id: StreamableArrayList.java,v 1.1 2001/10/04 23:02:07 mdb Exp $ + +package com.threerings.cocktail.cher.util; + +import java.io.IOException; +import java.io.DataInputStream; +import java.io.DataOutputStream; + +import java.util.ArrayList; +import com.threerings.cocktail.cher.io.Streamable; + +/** + * Provides a means by which an ordered collection of streamable instances + * (all of the exact same class) can be delivered over the network. A + * streamable array list can be supplied anywhere that a distributed + * object value can be supplied, but bear in mind that once the list is + * created, it's elements cannot be changed without rebroadcasting the + * entire list. It is not like a {@link + * com.threerings.cocktail.cher.dobj.DSet} which allows individual + * elements to be added or removed. + */ +public class StreamableArrayList + extends ArrayList implements Streamable +{ + // documentation inherited + public void writeTo (DataOutputStream out) + throws IOException + { + int count = size(); + out.writeInt(count); + + // only write element info if we have elements + if (count > 0) { + Streamable first = (Streamable)get(0); + // write out the classname of our elements + out.writeUTF(first.getClass().getName()); + + // now write out our elements + for (int i = 0; i < count; i++) { + Streamable s = (Streamable)get(i); + s.writeTo(out); + } + } + } + + // documentation inherited + public void readFrom (DataInputStream in) + throws IOException + { + // read in our element count + int count = in.readInt(); + + // only read in elements if we have some + if (count > 0) { + // read in the element class and load it up + String cname = in.readUTF(); + try { + Class clazz = Class.forName(cname); + + // now read in the elements + for (int i = 0; i < count; i++) { + Streamable s = (Streamable)clazz.newInstance(); + s.readFrom(in); + add(s); + } + + } catch (Exception e) { + throw new IOException("Error reading streamable " + + "[class=" + cname + ", err=" + e + "]"); + } + } + } +}