Docsity

Object Serialization and Externalization in Java: Persisting and Transmitting Objects, Slides of Computer Engineering and Programming

Partial preview of the text

Download Object Serialization and Externalization in Java: Persisting and Transmitting Objects and more Slides Computer Engineering and Programming in PDF only on Docsity!

Object Serialization

• Normally, objects live only during the life

of the program. Stop the program, and the

object disappears

• JDK offers the Serializable interface

to create ‘lightweight persistence’.

• Persistent objects live beyond the life of the

program, usually in byte from on disk or on

a network.

Object Serialization

• The Serializable interface provides

this capabilities for lightweight persistence.

• The Serializable interface is

lightweight because one must manually

create/load persistent objects.

• This technique is necessary for remote

method invocation (RMI), where objects

live on another machine

Saving Serialized Objects

  • Simple: create an OutputStream , and wrap it with an ObjectOutputStream.
  • Twin methods:
    • writeObject();
    • readObject(); /* requires InputStream wrapped by ObjectInputStream */

• Object Web

  • import java.io.*;

public class Data implements Serializable { public static void main(String[] args) throws ClassNotFoundException, IOException { Data d = new Data(); ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("data.out")); out.writeObject("Data storage"); out.writeObject(d); out.close();

ObjectInputStream in = new ObjectInputStream( new FileInputStream("data.out")); String s = (String)in.readObject(); Data d2 = (Data)in.readObject(); } } Docsity.com

Externalization of Objects

  • JDK also has the java.io.Externalizable class.
  • Externalization is different from serialization.
  • Th Externalizable interface extends Serializable and adds two methods one must implement:

public abstract void readExternal(ObjectInput in) throws IOException, ClassNotFoundException

public abstract void writeExternal(ObjectOutput out) throws IOException

Externalized Objects--More

  • Externalized Objects are similar to serialized

objects, except that the objects themselves control

the field creation during reading and writing.

  • The readExternal and writeExternal are

called with readObject() and

writeObject()

  • This produces greater security, and offers the

possibility of selective serialization

  • Constructors for externalized objects must be

public

  • public void readExternal(ObjectInput in) { s = (String)in.readObject(); i = in.readInt(); } public static void main(String[] args) throws IOException, ClassNotFoundException { Data d = new Data("String value",1514); System.out.println(d); ObjectOutputStream o = new ObjectOutputStream( New FileOutputStream("data.out")); o.writeObject(d); o.close();

// Now deserialize ObjectInputStream in = new ObjectInputStream( new FileInputStream("data.out")); d = (Data)in.readObject(); } }

Serialization & the transient State

  • There may be time when we want to serialize an

object, but omit certain key features, like

passwords or other sensitive data.

  • Even objects and data identified as private get

serialized. How does one keep data out of

externalized objects?

  • Solution: use the transient keyword: private transient String password = “pssst.”;
  • Data and methods identified as transient are

not serialized.