190 likes | 201 Views
Program 23.3 explains the concept of serialization, storing and retrieving objects from an external file. It covers the implementation of Serializable interface and custom serialization methods.
E N D
Podcast Ch23f • Title: Serialization • Description: Overview; Program 23.3; custom serialization • Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) • Textbook: Data Structures for Java; William H. Ford and William R. Topp
Serialization • A persistent object can exist apart from the executing program and can be stored in a file. • Serialization involves storing and retrieving objects from an external file. • The classes ObjectOutputStream and ObjectInputStream are used for serialization.
Serialization (continued) • Assume an Object is an instance of a class that implements the Serializable interface. // the stream oos uses a FileOutputStream that is // attached to "storeFile" for storage of an object ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream("storeFile")); ... // write anObject to file "storeFile oos.writeObject(anObject);
Serialization (continued) • Deserializing an Object // the stream ois uses a FileInputStream that // is attached to file "storeFile" to // retrieve an object ObjectInputStream ois = new ObjectInputStream( new FileInputStream("storeFile")); ClassName recallObj; // retrieve from "storeFile" recallObj = (ClassName)ois.readObject();
What is meant by serialization? The process of storing and retrieving objects in an external file. A class that provides for serialization of its objects must implement the Serializable interface. What methods must be defined in this interface? None.
Class SerializableClass import ds.time.Time24; public class SerializableClass implements java.io.Serializable { public int n; public String str; public Time24 t; public Integer[] list = new Integer[4]; transient public Time24 currentTime;
Class SerializableClass(concluded) public SerializableClass(int n, String str, Time24 t, Time24 currentTime) { this.n = n; this.str = str; this.t = t; for (int i = 0; i < list.length; i++) list[i] = new Integer(i + 1); this.currentTime = currentTime; } }
Program 23.3 import ds.time.Time24; import ds.util.Arrays; import java.io.*; public class Program23_3 { public static void main(String[] args) throws Exception { // objects used for serialization SerializableClass obj, recallObj; // object stream connected to file // "storeFile" for output ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream("storeFile.dat"));
Program 23.3 (continued) // initial object with runtime // update of 45 minutes obj = new SerializableClass(45, "Shooting star", new Time24(9,30), new Time24(7, 10)); obj.t.addTime(45); // output object info before copy to the file System.out.println("Serialized object:"); System.out.println(" Integer: " + obj.n + " String: " + obj.str + " Time: " + obj.t + "\n Current time: " + obj.currentTime + " List: " + Arrays.toString(obj.list)); // send object and close down the output stream oos.writeObject(obj); oos.flush(); oos.close();
Program 23.3 (concluded) // object stream connected to file // "storeFile" for output ObjectInputStream ois = new ObjectInputStream( new FileInputStream("storeFile.dat")); // reconstruct object & allocate new currentTime recallObj = (SerializableClass)ois.readObject(); recallObj.currentTime = new Time24(15, 45); // output object after recall from the file System.out.println("Deserialized object:"); System.out.println(" Integer: " + recallObj.n + " String: " + recallObj.str + " Time: " + recallObj.t + '\n' + " Current time: " + recallObj.currentTime + " List: " + Arrays.toString(obj.list)); } }
Program 23.3 (Run) Serialized object: Integer: 45 String: Shooting star Time: 10:15 Current time: 7:10 List: [1, 2, 3, 4] Deserialized object: Integer: 45 String: Shooting star Time: 10:15 Current time: 15:45 List: [1, 2, 3, 4]
What does it mean to declare a variable as transient? An instance variable that is defined as transient is automatically copied to file with the writeObject() method True or False
Custom Serialization • In some cases a programmer needs to customize the write/read process. For instance, with collection objects, elements are dynamically generated and stored with some kind of ordering. The deserialization process must retrieve the elements and then rebuild the underlying storage structure for the collection. The ArrayList class is a good example.
Custom Serialization(continued) • In an ArrayList collection write out only listSize elements stored in the front of the array listArr.
Custom Serialization(continued) • For custom serialization, the programmer must implement private methods writeObject() and readObject().
ArrayList writeObject() • For custom serialization, the programmer must implement private methods writeObject() and readObject(). private void writeObject(ObjectOutputStream out) throws java.io.IOException { // write out element count out.defaultWriteObject(); // write out the ArrayList capacity out.writeInt(listArr.length); // write the first listSize elements of listArr for (int i=0; i < listSize; i++) out.writeObject(listArr[i]); }
ArrayList readObject() private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { // read in list size in.defaultReadObject(); // read in array length and allocate the array int listCapacity = in.readInt(); listArr = (T[]) new Object[listCapacity]; // read listSize elements into listArr for (int i=0; i < listSize; i++) listArr[i] = (T)in.readObject(); }
A class deserializes an object using the _____________________ stream and the method ______________ (a) DataInputStream/readObjectData() (b) ObjectInputStream/getObject() (c) DataInputStream/readObject() (d) ObjectInputStream/readObject()
When creating a custom writeObject method in a class, begin by calling the method ______________ • defaultWriteObject() (b) defaultObjectOut() (c) ObjectOut.default() (d) defaultOut(object)