Java Reference
In-Depth Information
8-2. Serializing Java Objects More Effi-
ciently
Problem
You want to serialize a class, but want to make the output more efficient, or smaller in
size, than the product generated via the built-in serialization method.
Solution
By making the object implement the Externalizable interface, you instruct the
Java Virtual Machine to use a custom serialization/deserialization mechanism, as
provided by the readExternal / writeExternal methods in the following ex-
ample.
public class ExternalizableProgramSettings implements
Externalizable {
private Point locationOnScreen;
private Dimension frameSize;
private Color defaultFontColor;
private String title;
// Empty constructor, required for Externalizable
implementors
public ExternalizableProgramSettings() {
}
@Override
public void writeExternal(ObjectOutput out) throws
IOException {
out.writeInt(locationOnScreen.x);
out.writeInt(locationOnScreen.y);
out.writeInt(frameSize.width);
out.writeInt(frameSize.height);
out.writeInt(defaultFontColor.getRGB());
Search WWH ::




Custom Search