Java Reference
In-Depth Information
putStream
instead of
FilterInputStream
, these classes behave as filter
streams.
Java provides the concrete
ObjectOutputStream
class to initiate the serializa-
tionofanobject'sstatetoanobjectoutputstream.Thisclassdeclaresan
ObjectOut-
putStream(OutputStream out)
constructorthatchainstheobjectoutputstream
to the output stream specified by
out
.
Whenyoupassanoutputstreamreferenceto
out
,thisconstructorattemptstowritea
serializationheadertothatoutputstream.Itthrows
NullPointerException
when
out
is
null
,and
IOException
whenanI/Oerrorpreventsitfromwritingthishead-
er.
ObjectOutputStream
serializes an object via its
writeObject()
method.
Thismethodattemptstowriteinformationabout
obj
'sclassfollowedbythevaluesof
obj
's instance fields to the underlying output stream.
writeObject()
doesn't serialize the contents of
static
fields. In contrast, it
serializes the contents of all instance fields that are not explicitly prefixed with the
transient
reserved word. For example, consider the following field declaration:
public transient char[] password;
This declaration specifies
transient
to avoid serializing a password for some
hacker to encounter. The JVM's serialization mechanism ignores any instance field
that's marked
transient
.
writeObject()
throws
IOException
or an instance of an
IOException
subclass when something goes wrong. For example, this method throws
NotSeri-
alizableException
whenitencounters anobject whoseclass doesn'timplement
Serializable
.
Note
Because
ObjectOutputStream
implements
DataOutput
, it also de-
clares methods for writing primitive type values and strings to an object output stream.
Javaprovidestheconcrete
ObjectInputStream
classtoinitiate thedeserializa-
tionofanobject'sstatefromanobjectinputstream.Thisclassdeclaresan
ObjectIn-
putStream(InputStream in)
constructorthatchainstheobjectinputstreamto
the input stream specified by
in
.
When you pass an input stream reference to
in
, this constructor attempts to read
a serialization header from that input stream. It throws
NullPointerException
when
in
is
null
,
IOException
when an I/O error prevents it from reading this