Externalizable POJO
package io.acari.pojo;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.List;
public class ExternalizableProgrammer implements Externalizable {
private static final long serialVersionUID = 6757860161913660513L;
public static final int NULL_LIST = -1;
private String name;
private int age;
private ExternalizableComputer computer;
private List<String> languages;
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeUTF(name);
out.writeInt(age);
int size = languages == null ? NULL_LIST : languages.size();
out.writeInt(size);
for (int i = 0; i < size; ++i) {
out.writeUTF(languages.get(i));
}
computer.writeExternal(out);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
name = in.readUTF();
age = in.readInt();
int size = in.readInt();
if (size > NULL_LIST) {
languages = new ArrayList<>(size);
for (int i = 0; i < size; ++i) {
languages.add(i, in.readUTF());
}
}
computer = new ExternalizableComputer();
computer.readExternal(in);
}
//ACCESSOR METHODS OMITTED
}
package io.acari.pojo;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
public class ExternalizableComputer implements Externalizable {
private static final long serialVersionUID = -6235153548793669030L;
private String model;
private String subModel;
private int ram;
private String make;
@Override
public void writeExternal(ObjectOutput objectOutput) throws IOException {
objectOutput.writeUTF(model);
objectOutput.writeUTF(subModel);
objectOutput.writeInt(ram);
objectOutput.writeUTF(make);
}
@Override
public void readExternal(ObjectInput objectInput) throws IOException, ClassNotFoundException {
model = objectInput.readUTF();
subModel = objectInput.readUTF();
ram = objectInput.readInt();
make = objectInput.readUTF();
}
//ACCESSOR METHODS OMMITTED
}