Java Reference
In-Depth Information
out.writeUTF(title);
}
@Override
public void readExternal(ObjectInput in) throws
IOException, ClassNotFoundException {
locationOnScreen = new Point(in.readInt(),
in.readInt());
frameSize = new Dimension(in.readInt(),
in.readInt());
defaultFontColor = new Color(in.readInt());
title = in.readUTF();
}
// getters and setters omitted for brevity
}
How It Works
The Java Serialization framework provides the ability for you to specify the imple-
mentation for serializing an object. As such, it requires implementing the
External-
izable
interface in lieu of the
Serializable
interface. The
Externalizable
interface contains two methods:
writeExternal(ObjectOutput out)
and
readExternal(ObjectInput in)
. By implementing these methods, you are
telling the framework how to encode/decode your object.
The
writeExternal()
method will pass in as a parameter an
ObjectOutput
object. This object will then let you write your own encoding for the serialization. The
ObjectOutput
contains the methods listed in
Table 8-1
.
ObjectOutput
ObjectInput
Description
Read/writes the Boolean primitive.
writeBoolean
(boolean v)
booleanreadBoolean
()
Read/writes a byte.
Note: Java doesn't have a byte primit-
ive, so an
int
is used as a parameter,
writeByte(int
v)
intreadByte()