1 / 19

Serialization: Overview of Custom Serialization

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.

rayfordg
Télécharger la présentation

Serialization: Overview of Custom Serialization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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.

  3. 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);

  4. 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();

  5. 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.

  6. 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;

  7. 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; } }

  8. 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"));

  9. 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();

  10. 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)); } }

  11. 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]

  12. 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

  13. 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.

  14. Custom Serialization(continued) • In an ArrayList collection write out only listSize elements stored in the front of the array listArr.

  15. Custom Serialization(continued) • For custom serialization, the programmer must implement private methods writeObject() and readObject().

  16. 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]); }

  17. 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(); }

  18. A class deserializes an object using the _____________________ stream and the method ______________ (a) DataInputStream/readObjectData() (b) ObjectInputStream/getObject() (c) DataInputStream/readObject() (d) ObjectInputStream/readObject()

  19. When creating a custom writeObject method in a class, begin by calling the method ______________ • defaultWriteObject() (b) defaultObjectOut() (c) ObjectOut.default() (d) defaultOut(object)

More Related