
























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of object serialization in java, including the process of converting objects to a serialized form and the use cases of rmi and lightweight persistence. It also covers the serialization api and its methods, such as objectoutputstream and objectinputstream, and provides an example of serializing and deserializing an object. The document also discusses the importance of handling other referenced objects correctly and making instances of classes serializable.
Typology: Slides
1 / 32
This page cannot be seen from the preview
Don't miss anything!

























^ RMI
(Remote
Method
Invocation)
^ Lightweight
persistence
^ ^ created
by^ the
new^
operator
^ …^ exists
^ †^ destroyed
by^ the
Java^
VM’s
garbage
collector when
the^ program
no
longer
holds
a^ reference
to^ the
object
5
docsity.com
7
ObjectInputStream(InputStream)
Creates an ObjectInputStream that reads from the specifiedInputStream.
readObject()
Reads an object from the input stream; is the opposite ofwriteObject().
available()
Returns the number of bytes that can be read withoutblocking.
close()
Closes the input stream.
readInt()
Reads a 32-bit int from the stream.
defaultReadObject()
Reads the nonstatic and nontransient fields of the currentclass from the stream, ensuring the standard encodingformat. You can call this method only from the readObject()method of the class being decoded.
For more details consult the Java API.
docsity.com
8
docsity.com
10
FileInputStream in = new FileInputStream("theTime");ObjectInputStream s = new ObjectInputStream(in);String today = (String)s.readObject();Date date = (Date)s.readObject();
13
/** pstruct is an object containing fields of different types, e.g. String, double, etc.*/obj_outs.writeObject(pstruct.product_id);obj_outs.writeObject(pstruct.product_name);obj_outs.writeObject(pstruct.product_description);obj_outs.writeObject(new Double(pstruct.price));obj_outs.writeObject(pstruct.currency);obj_outs.writeObject(new Boolean(pstruct.soft_good_p));obj_outs.writeObject(new Boolean(pstruct.for_sale_p));obj_outs.writeObject(pstruct.terms_and_conditions);data = b_outs.toByteArray();obj_outs.close();b_outs.close();
14
/** at this point, byte buffer contains the serialized form of pstruct //* reconstruct pstruct as follows */ByteArrayInputStream b_ins = new ByteArrayInputStream(data);ObjectInputStream obj_ins = new ObjectInputStream(b_ins);
16
17
public class MySerializableClass implements Serializable {
^ Differs
from
Serializable
in^ :
^ the
amount
of^ control
in^ the
serialization
process
^ the
extent
of^ customizations
^ writeObject,
readObject
^ responsible
for^ serializing
only
the^ immediate
class.
^ Any
serialization
required
by^ the
superclasses
is^ handled
automatically.
^ Class
explicitly
coordinate
with
its^ superclasses
to^ serialize
itself
implementing
the^ Externalizable
interface.
:^ Complete,
explicit
control
of^ the
serialization
process.
:^ Only
the^ class
identification
of^ the
object
is^ automatically
saved
by^ the
stream.
The^ class
is^ responsible
for^ writing
and^
reading
its^ contents,
and^
it^ must
coordinate
with^
its^ superclasses
to^ do
so^ (whether
or^ not
the^ state
of^ superclasses
and^
which
fields
are^ stored).
^ writeExternal()
must
encode
the^ data
of^ the
object
in^ a^ form
and^
sequence
supported
by^ readExternal(). Does
not^ support
code
versioning
^ you
must
provide
your
own
versioning
approach.
^ Above
methods
are^ public
^ security
risk^
(a^ client
may be^ able
to^ write
or^ read
information
in^ the
object
other
than
by^ using
its^ methods).
20
readExternal(ObjectInput)
The object implements this method to restore its contents bycalling the methods of DataInput for primitive types andreadObject() for objects, strings and arrays.
writeExternal(ObjectOutput)
The object implements this method to save its contents by callingthe methods of DataOutput for its primitive values or calling thewriteObject() method of ObjectOutput for objects, strings andarrays.
For more details consult the Java API.