Java Reference
In-Depth Information
serialized object file instead of a class file. When such an <APPLET> tag is encoun-
tered, the applet viewer or web browser creates the applet by deserializing it.
The reason that this is interesting is that it allows an applet to be shipped in a
preinitialized state. The code for the applet need not even include the code that
performed the initialization. For example, imagine a GUI builder tool that allows a
programmer to build a GUI using a point-and-click interface. Such a tool could
create a tree of AWT components within an Applet panel and then serialize the
applet, including all the GUI components it contains. When deserialized, the
applet would have a complete GUI, despite the fact that the applet's class file
doesn't contain any code to create the GUI.
You can experiment with applet serialization with the appletviewer program. Start
an applet running in appletviewer in the usual way. This loads the applet and runs
its init() and start() methods. Next, select the Stop item from the menu to stop
the applet. Now use the Save menu item to serialize the applet to a file. By con-
vention, your serialized applet file should be given a .ser extension. If the applet
refers to any nonserializable objects, you may not be able to serialize it. For exam-
ple, you may encounter problems serializing applets that use threads or images.
Once you serialize an applet, create an HTML file with an <APPLET> tag like this:
<APPLET OBJECT="MyApplet.ser" WIDTH=400 HEIGHT=200></APPLET>
Finally, you can use appletviewer with this new HTML file. It should deserialize
and display the applet. When created in this way, the applet's init() method is
not called (since it was called before serialization), but its start() method is
called (because the applet should have been stopped before serialization).
Exercises
9-1. The java.util.Properties class is essentially a hashtable that maps string
keys to string values. It defines store() and load() methods that save and
load its contents to and from a byte stream. These methods save the Proper-
ties object in a human-readable text format despite the fact that they use
byte streams. Properties inherits the Serializable interface from its super-
class, java.util.Hashtable . Define a subclass of Properties that implements
storeBinary() and loadBinary() methods that use object serialization to
save and load the contents of the Properties object in binary form. You may
also want to use java.util.zip.GZIPOutputStream and java.util.zip.GZIP-
InputStream in your methods to make the binary format particularly compact.
Use the serialver program to obtain a serialVersionUID value for your class.
9-2. As noted in the previous exercise, the Properties object has store() and
load() methods that allow it to save and restore its state. Define an Exter-
nalizable subclass of Properties that uses these store() and load() meth-
ods as the basis for its writeExternal() and readExternal() methods. In
order to make this work, the writeExternal() method needs to determine
the number of bytes written by the store() method and write this value into
the stream before writing the bytes themselves. This allows the read-
External() method to know when to stop reading. Also include a version
number in the externalizable data format you use.
Search WWH ::




Custom Search