Podcast Ch23f Title: Serialization

Slides:



Advertisements
Similar presentations
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Advertisements

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Serialization Flatten your object for automated storage or network.
Slide 10.1 Advanced Programming 2004, based on LY Stefanus’s Slides Object Serialization Object serialization: the process of converting an object into.
Chapter - 12 File and Streams (continued) This chapter includes -  DataOutputStream  DataInputStream  Object Serialization  Serializing Objects 
King Saud University College of Computer and Information Sciences Department of Computer Science Dr. S. HAMMAMI Chapter 3 File Input/Output Chapter 3 File.
Unit 211 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files.
File-based Persistence: Serializability COMP53 Dec 7, 2007.
Java Iterators interface Collection { … Iterator iterator(); Iterator iterator(); …} interface Set extends Collection { … Iterator iterator(); Iterator.
Unit 201 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files.
Advanced Java Class Serialization. Serialization – what and why? What? –Translating the contents of an Object to a series of bytes that represent it,
1 Java object model Part 3: Serialization & Reflection.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L08 (Chapter 18) Binary I/O.
Working with files By the end of this lecture you should be able to: explain the principles of input and output and identify a number of different input.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 23 Bit Arrays.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Java I/O Writing and Reading Objects to File Serialization.
Object Persistence and Object serialization CSNB534 Asma Shakil.
Object Serialization.  When the data was output to disk, certain information was lost, such as the type of each value.  If the value "3" is read from.
Outline  Basic IO  File class  The Stream Zoo  Serialization  Regular Expressions.
Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
©SoftMoore ConsultingSlide 1 Serialization. ©SoftMoore ConsultingSlide 2 Serialization Allows objects to be written to a stream Can be used for persistence.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 12 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/ George Koutsogiannakis 1.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 20.
1 Copyright © 2011 Tata Consultancy Services Limited TCS Internal.
Files and Serialization. Files Used to transfer data to and from secondary storage.
File Input & Output1 Streams & File I/O. Introduction (1) The Java platform includes a number of packages that are concerned with the movement of data.
1 CSE 331 Memento Pattern and Serialization slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Iterators. Chapter Scope The purpose of an iterator The Iterator and Interable interfaces The concept of fail-fast collections Using iterators to solve.
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
Podcast Ch23e Title: Implementing Huffman Compression
Podcast Ch17b Title: Iterative Tree Traversal
Networking Serialization
Podcast Ch26a Title: Representing Graphs
Object Writing in files
RMI Packages Overview java.rmi java.rmi.server java.rmi.registry
CIS265/506 Files & Indexing CIS265/506: File Indexing.
Memento Design Pattern
Chapter 16 Iterators.
Accessing Files in Java
File I/O & collection frame work
Java Serialization B.Ramamurthy 11/8/2018 B.Ramamurthy.
Client server programming
CS 116 Object Oriented Programming II
Chapter 7 Iterators.
Java Serialization B.Ramamurthy 11/14/2018 B.Ramamurthy.
An Introduction to Java – Part I, language basics
Serialization and Deserialization Bullet points from Head First Java, Ch Dec-18 serialization.ppt.
Podcast Ch18b Title: STree Class
Computer Science and Engineering
Command Invocation Protocol
slides created by Ethan Apter
Podcast Ch22c Title: Deleting from a Heap
CSE 331 Memento Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
OBJECT ORIENTED PROGRAMMING II LECTURE 22 GEORGE KOUTSOGIANNAKIS
Podcast Ch23b Title: BitArray Implementation
Podcast Ch22b Title: Inserting into a Heap
Podcast Ch20b Title: TreeMap Design
Podcast Ch21d Title: Hash Class Iterators
Podcast Ch27a Title: Overview of AVL Trees
Podcast Ch21a Title: Hash Functions
Podcast Ch21f Title: HashSet Class
Podcast Ch27b Title: AVLTree implementation
Podcast Ch22a Title: Array-based Binary Trees
Podcast Ch24b Title: Strongly Connected Components
The command invocation protocol
Podcast Ch23a Title: Bit Arrays
Podcast Ch23c Title: Binary Files
System Models Bina Ramamurthy 9/7/2019 B.Ramamurthy.
OO Java Programming Input Output.
Presentation transcript:

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: 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)