Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

Slides:



Advertisements
Similar presentations
COS 461 Fall 1997 Network Objects u first good implementation: DEC SRC Network Objects for Modula-3 u recent implementation: Java RMI (Remote Method Invocation)
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.
CSC 243 – Java Programming, Spring 2013 March 26, 2013 Week 8, java.lang.Clonable & java.io.Serializable Interfaces.
Java Exception Very slightly modified from K.P. Chow University of Hong Kong (some slides from S.M. Yiu)
Chapter 91 Streams and File I/O Chapter 9. 2 Announcements Project 5 due last night Project 6 assigned Exam 2 –Wed., March 21, 7:00 – 8:00 pm, LILY 1105.
Unit 211 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files.
Java Iterators interface Collection { … Iterator iterator(); Iterator iterator(); …} interface Set extends Collection { … Iterator iterator(); Iterator.
1 Chapter 7 Inheritance, Polymorphism, and Scope.
Unit 201 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
Advanced Java Class Serialization. Serialization – what and why? What? –Translating the contents of an Object to a series of bytes that represent it,
Chapter 91 Streams and File I/O Chapter 9. 2 Reminders Project 6 released: due Nov 10:30 pm Project 4 regrades due by midnight tonight Discussion.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L08 (Chapter 18) Binary I/O.
Chapter 91 Streams and File I/O CS-180 Recitation-03/07/2008.
import java.io.*; class Tree implements Serializable { static private class Node implements Serializable { int _data; Node _ls, _rs; Node(int data) {_data.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
Web Services CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Windows Programming Using C# Windows Services, Serialization, and Isolated Storage.
Java File I/O (Continued). File I/O in Java Like every other programming language, Java supports the writing to and reading from different files with.
Java and C++, The Difference An introduction Unit - 00.
Exceptions 1. Your computer takes exception Exceptions are errors in the logic of a program (run-time errors). Examples: Exception in thread “main” java.io.FileNotFoundException:
Methods CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
1 TCSS 360, Spring 2005 Lecture Notes Design Patterns: Singleton, Memento, Flyweight.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Java I/O Writing and Reading Objects to File Serialization.
Generics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CORBA – Command Line CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Object Persistence and Object serialization CSNB534 Asma Shakil.
RMI – Command Line CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
RMI - Eclipse CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CORBA – Eclipse CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Streams and File I/O Chapter 9. Outline Overview of Streams and File I/O Text-File I/O Using the File Class Basic Binary-File I/O Object I/O with Object.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
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.
©SoftMoore ConsultingSlide 1 Serialization. ©SoftMoore ConsultingSlide 2 Serialization Allows objects to be written to a stream Can be used for persistence.
COSC 1P02 Data Structures and Abstraction 2.1 Cosc 1P02 Binary Files & Persistent Objects “For myself, I am an optimist--it does not seem to be much use.
Chapter 11 Exceptions and Input/Output Operations.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 12 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/ George Koutsogiannakis 1.
Java file manipulations
Semaphores CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
Classes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
1 Copyright © 2011 Tata Consultancy Services Limited TCS Internal.
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
Networking Code CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Files and Serialization. Files Used to transfer data to and from secondary storage.
CSE 1020: Exceptions and A multiclass application Mark Shtern 1-1.
Thread Pools CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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
Advanced Programming Practice Questions Advanced Programming. All slides copyright: Chetan Arora.
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
Principles of Software Development
Networking Serialization
Object Serialization in Java
Memento Design Pattern
Accessing Files in Java
Java Serialization B.Ramamurthy 11/8/2018 B.Ramamurthy.
Programming in Java Files and I/O Streams
CS 116 Object Oriented Programming II
Java Serialization B.Ramamurthy 11/14/2018 B.Ramamurthy.
Serialization and Deserialization Bullet points from Head First Java, Ch Dec-18 serialization.ppt.
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 Ch23f Title: Serialization
The command invocation protocol
CS18000: Problem Solving and Object-Oriented Programming
OO Java Programming Input Output.
Presentation transcript:

Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Outline USC CSCI 201L2/12 ▪S▪Serialization and File I/O

Saving Objects ▪When writing objects out to an output stream, you may be tempted to save all of the data that is within the object individually ▪Although this may work, you need to make sure to save all of the data from parent classes up the hierarchy as well ›This is necessary if you want to save the exact state of a program or copy that state to another program ▪Reading all of this data back into the exact variables may be challenging if you do not have access to set the values of all of the variables USC CSCI 201L3/12 Serialization

Saving Object Example 1 class Employee { 2 private String fname, lname; 3 private long salary; 4 public Employee(String fname, String lname, long salary) { 5 this.fname = fname; 6 this.lname = lname; 7 this.salary = salary; 8 } 9 public void saveData(PrintWriter pw) { 10 pw.println(fname + “,” + lname + “,” + salary); 11 } 12 } public class Test { 15 public static void main(String [] args) { 16 Employee emp = new Employee(“donald”, “trump”, “billionaire”); 17 PrintWriter pw = new PrintWriter(new FileWriter(“employees.txt”)); 18 emp.saveData(pw); 19 } 20 } USC CSCI 201L4/12 Serialization

Saving Object Example 1 class Person { 2 protected String fname, lname; 3 public Person(String fname, String lname) { 4 this.fname = fname; 5 this.lname = lname; 6 } 7 } 8 class Employee extends Person { 9 private long salary; 10 public Employee(String fname, String lname, long salary) { 11 super(fname, lname); 12 this.salary = salary; 13 } 14 public void saveData(PrintWriter pw) { 15 pw.println(fname + “,” + lname + “,” + salary); 16 } 17 } public class Test { 20 public static void main(String [] args) { 21 Employee emp = new Employee(“donald”, “trump”, “billionaire”); 22 PrintWriter pw = new PrintWriter(new FileWriter(“employees.txt”)); 23 emp.saveData(pw); 24 } 25 } USC CSCI 201L5/12 Serialization

Saving Object Example 1 class Person { 2 private String fname, lname; 3 public Person(String fname, String lname) { 4 this.fname = fname; 5 this.lname = lname; 6 } 7 } 8 class Employee extends Person { 9 private long salary; 10 public Employee(String fname, String lname, long salary) { 11 super(fname, lname); 12 this.salary = salary; 13 } 14 public void saveData(PrintWriter pw) { 15 pw.println(fname + “,” + lname + “,” + salary); // will this compile? 16 } 17 } public class Test { 20 public static void main(String [] args) { 21 Employee emp = new Employee(“donald”, “trump”, “billionaire”); 22 PrintWriter pw = new PrintWriter(new FileWriter(“employees.txt”)); 23 emp.saveData(pw); 24 } 25 } USC CSCI 201L6/12 Serialization

Serialization Overview ▪The java.io.Serializable interface allows us to specify to the JVM that the current object can be stored in an ObjectOutputStream and then retrieved from ObjectInputStream ▪The object’s state will be restored exactly when retrieved from the ObjectInputStream ›This includes all of the private variables from inherited classes as well ▪The java.io.Serializable interface does not contain any methods ▪If an object does not implement the java.io.Serializable interface and tries to be serialized, a NotSerializableException will be thrown ▪static and transient variables of the class are not serialized ›If a variable in the serializable class is another object, that object must be serializable or a NotSerializableException will be thrown ›You can make the variable transient instead, which means the variable will not be serialized USC CSCI 201L7/12 Serialization

serialVersionUID ▪The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. ▪If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. ▪A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long › public static final long serialVersionUID = 1; From USC CSCI 201L8/12 Serialization

Array Serialization Example 1 import java.io.*; // Note that I imported * to save space 2 3 public class Test { 4 public static void printArray(String [] arr) { 5 for (int i=0; i < arr.length; i++) { 6 System.out.println("[" + i + "] = " + arr[i]); 7 } 8 } 9 10 public static void main(String [] args) { 11 try { 12 String [] strarr = {"CSCI", "201", "Spring", "Summer", "Fall"}; 13 printArray(strarr); 14 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("o.txt")); 15 oos.writeObject(strarr); 16 oos.close(); 17 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("o.txt")); 18 String [] sarr = (String[])ois.readObject(); 19 printArray(sarr); 20 ois.close(); 21 } catch (FileNotFoundException fnfe) { 22 System.out.println("FileNotFoundException: " + fnfe.getMessage()); 23 } catch (IOException ioe) { 24 System.out.println("IOException: " + ioe.getMessage()); 25 } catch (ClassNotFoundException cnfe) { 26 System.out.println("ClassNotFoundException: " + cnfe.getMessage()); 27 } 28 } 29 } USC CSCI 201L9/12 Serialization

Custom Serialization Example 1 import java.io.*; 2 class Employee implements Serializable { 3 public static final long serialVersionUID = 1; 4 private String fname, lname; 5 private transient String pass; 6 public Employee(String fname, String lname, String pass) { 7 this.fname = fname; 8 this.lname = lname; 9 this.pass = pass; 10 } 11 public void printEmployee() { 12 System.out.println(fname + " " + lname + ": " + pass); 13 } 14 } 15 public class EmployeeMain { 16 public static void main(String [] args) { 17 Employee emp = new Employee("donald", "trump", "billionaire"); 18 try { 19 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("out.txt")); 20 oos.writeObject(emp); 21 oos.flush(); 22 oos.close(); 23 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("out.txt")); 24 Employee emp1 = (Employee)ois.readObject(); 25 ois.close(); 26 emp.printEmployee(); 27 emp1.printEmployee(); 28 } catch (FileNotFoundException fnfe) { 29 System.out.println("fnfe: " + fnfe.getMessage()); 30 } catch (IOException ioe) { 31 System.out.println("ioe: " + ioe.getMessage()); 32 } catch (ClassNotFoundException cnfe) { 33 System.out.println("cnfe: " + cnfe.getMessage()); 34 } 35 } 36 } USC CSCI 201L10/12 Serialization

Networking Serialization ▪Just as we have written out to a file and read it back into objects, we can do the same with transmitting data over the line to another program ▪We will see this when we talk about networking later in the semester USC CSCI 201L11/12 Serialization and Networking

Program ▪Create a class that contains information about an employee, including name, address, and social security number. For the address, create another class with number, street, city, state, and zip in it. ▪All of the data except social security number should be saved to a file and be read back into the same objects. USC CSCI 201L12/12 Serialization