FileDocCategorySizeDatePackage
Person.javaAPI DocExample2503Mon May 24 17:06:24 BST 1999None

Person

public class Person extends Object implements Serializable, ObjectInputValidation

Fields Summary
static Hashtable
thePeople
String
name
String
ss
Constructors Summary
public Person(String name, String ss)


       
  
    this.name = name;
    this.ss = ss;
    thePeople.put(ss, name);
    
  
Methods Summary
public static voidmain(java.lang.String[] args)

  
    Person p1 = new Person("Rusty", "123-45-5678");
    Person p2 = new Person("Beth",  "321-45-5678");
    Person p3 = new Person("David", "453-45-5678");
    Person p4 = new Person("David", "453-45-5678");
  
    Enumeration en = thePeople.elements();
    while (en.hasMoreElements()) {
      System.out.println(en.nextElement());
    }
    
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream oout = new ObjectOutputStream(bout);
    oout.writeObject(p1);
    oout.writeObject(p2);
    oout.writeObject(p3);
    oout.writeObject(p4);
    oout.flush();
    oout.close();
    
    ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
    ObjectInputStream oin = new ObjectInputStream(bin);
    try {
      System.out.println(oin.readObject());
      System.out.println(oin.readObject());
      System.out.println(oin.readObject());
      System.out.println(oin.readObject());
    }
    catch (InvalidObjectException e) {
      System.err.println(e);
    }
    oin.close();

    // now empty the hashtable and try again
    thePeople.clear();
    bin = new ByteArrayInputStream(bout.toByteArray());
    oin = new ObjectInputStream(bin);
    try {
      System.out.println(oin.readObject());
      System.out.println(oin.readObject());
      System.out.println(oin.readObject());
      System.out.println(oin.readObject());
    }
    catch (InvalidObjectException e) {
      System.err.println(e);
    }
    oin.close();
    
    en = thePeople.elements();
    while (en.hasMoreElements()) {
      System.out.println(en.nextElement());
    }    
    
  
private voidreadObject(java.io.ObjectInputStream in)


    in.registerValidation(this, 5);
    in.defaultReadObject();

  
public java.lang.StringtoString()

    return this.name + "\t" + this.ss;
  
public voidvalidateObject()


    if (thePeople.containsKey(this.ss)) {
      throw new InvalidObjectException(this.name + " already exists");
    }
    else {
      thePeople.put(this.ss, this.name);
    }