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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

array output stream, a hacker can convert an object into a byte array. The byte array can be<br />

manipulated and modified without any access protection or security manager checks. Then the<br />

byte array can be reconstituted into a <strong>Java</strong> object by using it as the source of a byte array input<br />

stream.<br />

Security isn't the only potential problem. Some objects exist only as long as the current<br />

program is running. A java.net.Socket object represents an active connection to a remote<br />

host. Suppose a socket is serialized to a file, and the program exits. Later the socket is<br />

deserialized from the file in a new program—but the connection it represents no longer exists.<br />

Similar problems arise with file descriptors, I/O streams, and many more classes.<br />

For these and other reasons, <strong>Java</strong> does not allow instances of arbitrary classes to be serialized.<br />

You can only serialize instances of classes that implement the java.io.Serializable<br />

interface. By implementing this interface, a class indicates that it may be serialized without<br />

undue problems.<br />

public interface Serializable<br />

This interface does not declare any methods or fields; it serves purely to indicate that a class<br />

may be serialized. You should recall, however, that subclasses of a class that implements a<br />

particular interface also implement that interface by inheritance. Thus, many classes that do<br />

not explicitly declare that they implement Serializable are in fact serializable. For instance,<br />

java.awt.Component implements Serializable. Therefore, its direct and indirect<br />

subclasses, including Button, Scrollbar, TextArea, List, Container, Panel,<br />

java.applet.Applet, all subclasses of Applet, and all Swing components may be<br />

serialized. java.lang.Throwable implements Serializable. Therefore, all exceptions and<br />

errors are serializable.<br />

Table 11.1 lists the classes in the <strong>Java</strong> 2 core API that directly implement Serializable.<br />

Instances of these classes or their subclasses are serializable. Many packages not listed here<br />

do contain serializable classes. However, these are only serializable because their superclasses<br />

in another package are serializable. For example, java.applet.Applet is serializable<br />

because java.awt.Component is serializable. Some of the unfamiliar names in Table 11.1 are<br />

inner classes you don't normally see, like java.text.UnicodeClassMapping. Inner classes<br />

are only serializable if they are declared to implement Serializable. That their outer class<br />

implements Serializable is not enough to make the inner class serializable.<br />

Package<br />

Table 11.1. Serializable Classes in the java Packages<br />

Serializable<br />

BorderLayout, CardLayout, CheckboxGroup, Color,<br />

Component, ComponentOrientation, Cursor,<br />

Dimension, Event, FlowLayout, FocusManager, Font,<br />

java.awt<br />

FontMetrics,<br />

GridBagConstraints,<br />

GraphicsConfigTemplate,<br />

GridBagLayout,<br />

GridBagLayoutInfo, GridLayout, ImageMediaEntry,<br />

Insets, LightweightDispatcher, MediaTracker,<br />

java.awt.dnd<br />

MenuComponent, MenuShortcut, Point, Polygon,<br />

Rectangle, ScrollPaneAdjustable, SystemColor<br />

DropTarget<br />

java.awt.font TransformAttribute<br />

java.awt.geom AffineTransform<br />

242

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

Saved successfully!

Ooh no, something went wrong!