0

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
}
nrmad
  • 422
  • 9
  • 19
  • 1
    I do not understand the question as it stands. What should the subclasses do? In your implementations of writeExternal and readExternal you are free to write or read whatever you want, including or excluding the list. Have you tried it? What went wrong? – tucuxi Jul 03 '20 at 15:31
  • They simply contain primitive datatypes but their use is not important, I have written writeExternal and readExternal definitions in Contact, Account and Network respectively. My question is can you externalize a List in this example a list of Contacts. I have not because I am not sure which method I should be using for it or should I extend it or something. – nrmad Jul 03 '20 at 15:41
  • If `Contact` is `Serializable`, I think you do not need to even have `writeExternal` and `readExternal` do loops. – tucuxi Jul 03 '20 at 19:22
  • It's also externalizable – nrmad Jul 03 '20 at 19:25

2 Answers2

1

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);
  }
}
Johntor
  • 587
  • 11
  • 26
0
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();
    }
}
OulinaArt
  • 324
  • 4
  • 13