How do you Externalize a List in Java

810 views Asked by At

I have a class which implements Externalizable which contains three objects and a List of one of those three object types. I have assumed you would simply implement externalizable in the sub classes and then define read and write external for the member objects but is it possible to also include the List and if so how?

public class AuthSuccessPacket extends Packet implements Externalizable {

public static final long serialVersionUID = 10003L;
Contact contact;
Network network;
Account account;
List<Contact> contacts;

public AuthSuccessPacket(){
    super(Type.AUTH_SUCCESS);
}

public AuthSuccessPacket(Contact contact, Network network, Account account, List<Contact> contacts){
    super(Type.AUTH_SUCCESS);
    this.contact = contact;
    this.network = network;
    this.account = account;
    this.contacts = contacts;
}

@Override
public void writeExternal(ObjectOutput out) {
  out.writeObject(account);
  out.writeObject(network);
  out.writeObject(contact);
// ??? write list
}

@Override
public void readExternal(ObjectInput in)  {
}
contact = (Contact) in.readObject();
network = (Network) in.readObject();
account = (Account) in.readObject();
// ??? read list
}
2

There are 2 answers

1
Johntor On BEST ANSWER

I would try something like:

@Override
public void writeExternal(ObjectOutput out) throws IOException {
  out.writeObject(account);
  out.writeObject(network);
  out.writeObject(contact);
  // ??? write list
  out.writeInt(contacts.size());
  for (Contact cntct : contacts) {
    out.writeObject(cntct);
  }
}

@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {

  contact = (Contact) in.readObject();
  network = (Network) in.readObject();
  account = (Account) in.readObject();
  // ??? read list
  contacts = new ArrayList<>();
  int contactsNo = (int) in.readInt();
  for (int i = 0; i < contactsNo; i++) {
    Contact cntct = (Contact) in.readObject();
    contacts.add(cntct);
  }
}
0
OulinaArt On
public static class Person implements Externalizable{
    private String firstName;
    private String lastName;
    private int age;
    private Person mother;
    private Person father;
    private List<Person> children;

    public Person(){
    }

    public Person(String firstName, String lastName, int age) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }

    public void setMother(Person mother) {
        this.mother = mother;
    }

    public void setFather(Person father) {
        this.father = father;
    }

    public void setChildren(List<Person> children) {
        this.children = children;
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeObject(mother);
        out.writeObject(father);
        out.writeObject(firstName);
        out.writeObject(lastName);
        out.writeInt(age);
        out.writeObject(children);
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        mother = (Person) in.readObject();
        father = (Person) in.readObject();
        firstName = (String)in.readObject();
        lastName = (String) in.readObject();
        age = in.readInt();
        children = (List<Person>) in.readObject();
    }
}