...
Serialization captures all the fields of an object, provided that its class implements the Serializable
interface, including the non-public fields that are not normally accessibleinaccessible. If the byte stream to which the serialized values are written is readable, then the values of the normally inaccessible fields may be read. Moreover, it may be possible to modify or forge the preserved values so that when the class is deserialized, the values become corrupted.
Introducing a security manager does not fails to prevent the normally inaccessible fields from being serialized and deserialized (although permission must be granted to write to and read from the file or network if the byte stream is being stored or transmitted). Network traffic (including RMI) can be protected, however, by using SSL.
...
Code Block |
---|
private void writeObject(java.io.ObjectOutputStream out) throws IOException private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException; |
If When a Serializable
class does not fails to implement writeObject()
, it is serialized using a 'default' method, which serializes all its public, protected, and private fields, except for the ones those marked transient
. Likewise, if a Serializable
class does not fails to implement readObject()
, then it is deserialized by deserializing all its public, protected, and private fields, except for with the exception of the transient
fields.
When multiple objects are serialized on an ObjectOutputStream
, the ObjectOutputStream
ensures that each object is written to the stream only once. It accomplishes this by retaining a reference (or handle) to each object written to the stream. When a previously written object is subsequently written to the stream again, it is replaced with a reference to the originally written data in the stream. This substitution takes place without regard to whether the object's contents have changed in the interim. This table of serialized object references prevents garbage collection of the previously written objects because the garbage collector cannot collect live references. This behavior is both desirable and correct for data that potentially contains arbitrary object graphs, especially when the graphs are fully allocated and constructed prior to serialization. Likewise, the deserialization process can then use these references to efficiently deserialize a complete object graph.