23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Java</strong> I/O<br />

3. Serializing it is a potential security risk (java.lang.SecurityManager,<br />

java.security.MessageDigest).<br />

4. The class is mostly a holder for static methods without any real internal state<br />

(java.beans.Beans, java.lang.Math).<br />

5. The person who wrote the class simply didn't think about serialization.<br />

11.5.1 Classes That Implement Serializable but Aren't<br />

Just because a class may be serialized does not mean that it can be serialized. Several<br />

problems can prevent a class that implements Serializable from actually being serialized.<br />

Attempting to serialize such a class throws a NotSerializableException, a kind of<br />

<strong>IO</strong>Exception:<br />

public class NotSerializableException extends ObjectStreamException<br />

11.5.1.1 Problem 1: References to nonserializable objects<br />

The first common problem that prevents a serializable class from being serialized is that its<br />

graph contains objects that do not implement Serializable. The graph of an object is the<br />

collection of all objects that the object holds references to, and all the objects those objects<br />

hold references to, and all the objects those objects hold references to, and so on, until there<br />

are no more connected objects that haven't appeared in the collection. For an object to be<br />

serialized, all the objects it holds references to must also be serializable, and all the objects<br />

they hold references to must be serializable, and so on. For instance, consider this skeleton of<br />

a class:<br />

import java.applet.*;<br />

import java.net.*;<br />

public class MyNetworkingApplet extends Applet {<br />

}<br />

Socket theConnection;<br />

//...<br />

MyNetworkingApplet extends Applet, which extends Panel, which extends Container,<br />

which extends Component, which implements Serializable. Thus, MyNetworkingApplet<br />

should be serializable. However, MyNetworkingApplet contains a reference to a<br />

java.net.Socket object. Socket is not a serializable class. Therefore, if you try to pass a<br />

MyNetworkingApplet instance to writeObject(), a NotSerializableException will be<br />

thrown.<br />

The situation is even worse for container classes like Hashtable and Vector. Since<br />

serialization performs a deep copy to the output stream, storing even a single nonserializable<br />

class inside a vector or hash table prevents it from being serialized. Since the objects stored in<br />

a container can vary from program to program or run to run, there's no sure-fire way to know<br />

whether or not a particular instance of a container class can be serialized, short of trying it.<br />

The same problem commonly occurs in applets and other GUI programs that use a Container<br />

to hold many different components connected by events. If any of the objects in the container<br />

244

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!