FileDocCategorySizeDatePackage
SerializableList.javaAPI DocExample1599Mon Feb 13 12:47:46 GMT 2006None

SerializableList

public class SerializableList extends ArrayList implements Externalizable

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)


    SerializableList list = new SerializableList();
    list.add("Element 1");
    list.add(new Integer(9));
    list.add(new URL("http://www.oreilly.com/"));

    // not Serializable
    list.add(new Socket("www.oreilly.com", 80));

    list.add("Element 5");
    list.add(new Integer(9));
    list.add(new URL("http://www.oreilly.com/"));

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream temp = new ObjectOutputStream(bout);
    temp.writeObject(list);
    temp.close();

    ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
    ObjectInputStream oin = new ObjectInputStream(bin);
    List out = (List) oin.readObject();
    Iterator iterator = out.iterator();
    while (iterator.hasNext()) {
      System.out.println(iterator.next());
    }
  
public voidreadExternal(java.io.ObjectInput in)


   int elementCount = in.readInt();
   this.ensureCapacity(elementCount);
   for (int i = 0; i < elementCount; i++) {
     this.add(in.readObject());
   }
  
public voidwriteExternal(java.io.ObjectOutput out)


   out.writeInt(size());
   for (int i = 0; i < size(); i++) {
     if (get(i) instanceof Serializable) {
       out.writeObject(get(i));
     }
     else {
       out.writeObject(null);
     }
    }