Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Serialization Flatten your object for automated storage or network.

Slides:



Advertisements
Similar presentations
CS Network Programming CS 3331 Fall CS 3331 Outline Socket programming Remote method invocation (RMI)
Advertisements

A Guide to Advanced Java Faculty:Nguyen Ngoc Tu. 2 Operating System Application #1 Application #2 Java Virtual Machine #1 Local Memory Shared Memory Threads.
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)
The Package Statement Group related interfaces and classes together Purpose: encapsulation and reduces name conflicts –private package classes not visible.
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Java Card Technology Ch07: Applet Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Computer Science & Engineering.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Class Hierarchy II Discussion E. Hierarchy A mail order business sells catalog merchandise all over the country. The rules for taxation on the merchandise.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Chapter 8 Three Interfaces: Cloneable, Serializable, and Runnable.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Iterators interface Collection { … Iterator iterator(); Iterator iterator(); …} interface Set extends Collection { … Iterator iterator(); Iterator.
1 Chapter 7 Inheritance, Polymorphism, and Scope.
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.
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.
Files and Streams (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Java I/O Writing and Reading Objects to File Serialization.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 RMI.
Object Persistence and Object serialization CSNB534 Asma Shakil.
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
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.
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.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
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.
The Prototype Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
The Singleton Pattern (Creational)
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 20.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 Copyright © 2011 Tata Consultancy Services Limited TCS Internal.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Files and Serialization. Files Used to transfer data to and from secondary storage.
Java Programming: Advanced Topics 1 Input/Output and Serialization.
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
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
Object Writing in files
Chapter 17 Binary I/O 1.
Java Serialization B.Ramamurthy 11/8/2018 B.Ramamurthy.
Java Serialization B.Ramamurthy 11/14/2018 B.Ramamurthy.
Java Programming Language
Lecture 22 Inheritance Richard Gesick.
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
System Models Bina Ramamurthy 9/7/2019 B.Ramamurthy.
OO Java Programming Input Output.
Presentation transcript:

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Serialization Flatten your object for automated storage or network transfer

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 2 Software object persistence Persistence: Saving information about an object to recreate at different time, or place or both. Object serialization means of implementing persistence: convert object’s state into byte stream to be used later to reconstruct (build-deserialized) a virtually identical copy of original object. Default serialization for an object writes: – the class of the object, –the class signature, –values of all non-transient and non-static fields.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 3 Serialization protocol For serialization: –java.io.ObjectOutputStream via writeObject which calls on defaultWriteObject, For deserialization: –java.io.ObjectInputStream via readObject which calls on defaultReadObject. Any object instance that belongs to the graph of the object being serialized must be serializable as well. Superclass must be Serializable.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 4 Serialization protocol Customize default: implement extended versions of default methods in: –writeObject –readObject –But final fields cannot be read with readObject. Need to use default. Create own complete serialization by implementing the interface Externalizable.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 5 Specifying persistent objects Class of the object to be serializable must implement interface: java.io.Serializable This interface is an empty interface and is used to mark the objects of such class as persistent.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 6 Deserialization It reads values written during serialization Static fields in the class are left untouched. –If class needs to be loaded, then normal initialization of the class takes place, giving static fields its initial values. Transient fields will be initialized to default values Recreation of the object graph will occur in reverse order from its serialization.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 7 import java.io.Serializable; import java.util.Date; import java.util.Calendar; public class PersistentTime implements Serializable { public PersistentTime() { time = Calendar.getInstance().getTime(); } public Date getTime() { return time; } private Date time; } Example

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 8 Class java.io.ObjectOutputStream An ObjectOutputStream instance writes primitive data types and graphs of Java objects to an OutputStream. The objects can be read (reconstituted) using an ObjectInputStream. Persistent storage of objects can be accomplished by using a file for the stream. If the stream is a network socket stream, the objects can be reconstituted on another host or in another process. Only objects that support the java.io.Serializable interface can be written to streams. The class of each serializable object is encoded including the class name and signature of the class, the values of the object's fields and arrays, and the closure of any other objects referenced from the initial objects.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 9 Class java.io.ObjectOutputStream The method writeObject is used to write an object to the stream. Any object, including Strings and arrays, is written with writeObject. Multiple objects or primitives can be written to the stream. The objects must be read back from the corresponding ObjectInputstream with the same types and in the same order as they were written. Primitive data types can also be written to the stream using the appropriate methods from DataOutput. Strings can also be written using the writeUTF method.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 10 Example import java.io.ObjectOutputStream; import java.io.FileOutputStream; import java.io.IOException; public class FlattenTime{ public static void main(String [] args){ String filename = "time.ser"; if(args.length > 0){ filename = args[0]; } PersistentTime time = new PersistentTime(); FileOutputStream fos = null; ObjectOutputStream out = null; try{ fos = new FileOutputStream(filename); out = new ObjectOutputStream(fos); out.writeObject(time); out.close(); } catch(IOException ex){ ex.printStackTrace(); } } }

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 11 import java.io.ObjectInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.Calendar; public class InflateTime{ public static void main(String [] args){ String filename = "time.ser"; if(args.length > 0){ filename = args[0]; } PersistentTime time = null; FileInputStream fis = null; ObjectInputStream in = null; try{ fis = new FileInputStream(filename); in = new ObjectInputStream(fis); time = (PersistentTime)in.readObject(); in.close(); } catch(IOException ex){ ex.printStackTrace(); } catch(ClassNotFoundException ex){ ex.printStackTrace(); } System.out.println("Flattened time: " + time.getTime()); System.out.println("Current time: " + Calendar.getInstance().getTime()); } }

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 12 Serializable vs. Non-Serializable objects Java.lang.Object does not implement serializable, so you must decide which of your classes need to implement it. AWT, Swing components, strings, arrays are defined serializable. Certain classes and subclasses are not serializable: Thread, OutputStream, Socket When a serializable class contains instance variables which are not or should not be serializable they should be marked as that with the keyword transient.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 13 Transient fields These fields will not be serialized. When deserialized, these fields will be initialized to default values –Null for object references –Zero for numeric primitives –False for boolean fields If these values are unacceptable –Provide a readObject() that invokes defaultReadObject() and then restores transient fields to their acceptable values. –Or, the fields can be initialized when used for the first time. (Lazy initialization.)

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 14 Serial version UID You should explicitly declare a serial version UID in every serializable class. –Eliminates serial version UID as a potential source of incompatibility. –Small performance benefit, as Java does not have to come up with this unique number. –private static final long serialVersionUID =rlv; –rlv can be any number out thin air, but must be unique for each serializable class in your development. –If you want to make a new version of the class incompatible with existing version, choose a different UID. Deserialization of previous version will fail with InvalidClassException.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 15 Customizing OutputObjectStream, InputObjectStream To provide special behavior in the writing or reading of stream object bytes implement private void writeObject(ObjectOutputStream out) throws IOException; private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 16 Creating your own protocol: Externalizable Instead of implementing the Serializable interface, implement Externalizable: interface Externalizable{ public void writeExternal(ObjectOutput out) throws IOException; public void readExternal(ObjectInput in) throws IOException; }

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 17 Performance Serialization is a very expensive process. You must clearly have reasons to serialize instead of you directly writing what you need to save about the state of an object.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 18 Default or Customized serialization? Or Implementing Serializable judiciously Allowing a class’s instances to be serializable can be as simple as adding the words “implements Serializable” to the class specification. This is a common misconception, the truth is far more complex. While efficiency it is one cost associated with it, there are other long-term costs that are much more substantial. Using default serialization is very easy but this a very specious

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 19 Serialization Costs Your object’s private structure is out for the viewing!!!! It’s become part of the API. A major cost is that it decreases flexibility to change a class’s implementation once the class has been release Increases the likelihood of bugs and security holes. Increases the testing associated with releasing a new version of the class.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 20 Serialization caveats Implementing Serializable is not a decision to be undertaken lightly. Classes design for inheritance should rarely implement serializable and interfaces should rarely extend it. –You should provide parameterless constructor on non- serializable classes designed for inheritance, in case it is subclassed and the subclass wants to provide serialization. Inner classes should rarely if ever, implement Serializable. A static member class can be serializable.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 21 Consider using a custom serialized form The default serialized form of an object is an encoding of the physical representation of the object graph rooted at the object –Data contained in the object –Data contained in every object reachable from it. –Topology by which all of these objects are interlinked. The ideal serialized form contains only the logical data represented by the object. It is independent of its physical representation.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 22 Consider using a custom serialized form Default serialization is likely to be appropriate if an object’s physical presentation is identical to its logical content. –Appropriate: A Name class. –Not appropriate: A doubly linked List class.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 23 Consider using a custom serialized form Disadvantages of default serialization when physical and logical representation differ: –Permanently ties the exported API to the internal representation. –Can consume excessive space. –Can consume excessive time. –Can cause stack overflow.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 24 Consider using a custom serialized form A reasonable serialized form for a List is the number of entries followed by each of the entries. Although default serialized form is correct for a List case, it may not be the case for any object whose invariants are tied to implementation- specific details. –Example: a hash table using buckets. This is based on the hash code of the key, which may change from JVM to JVM, or for different runs of the hash table in same JVM. Thus default serialized form can violate the invariant for hash tables in this case.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 25 readObject() and security attacks Deserialization uses defaultReadObject () and readObject () to create a new instance of a class. Thus readObject is a constructor!!!!! So, readObject must behave like any other constructor: –Check for argument’s validity if need be –Make copies of parameters where needed Otherwise, a very simple job for an attacker to violate object’s invariants. –Provide a hand-made serialization of the attack object.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 26 Guide for writing a bulletproof readObject Private reference fields should be initialized with copies of its values. Check invariants and throw an InvalidObjectException if they fail. As with constructors, do not invoke any overridable methods. If an entire object graph must be check for validity after deserialization, the objectInputValidation interface should be used.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 27 writeReplace() Sometimes it may not be appropriate to serialize the actual object, but some specifically given object. Object writeReplace() throws ObjectStreamException; Returns an object that will replace the current object during serialization. Any object may be returned including the current one.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 28 A comment about access qualifier These methods can be of any accessibility They will be used if they are accessible to the object type being serialized –If a class has private readResolve, it only affects serialization of objects that are exactly its type. –If package-accessible readResolve affects only subclasses within the same package –public and protected readResolve affect objects of all subclasses.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 29 readResolve() Recall that deserialization produces an instance of a class object. If a given class should only have one instance (singleton pattern), then via deserialization we can provide a different instance!!! In general you need to be concerned of what is being created for instance-controlled classes. Enter: readResolve(); this is a method that returns the appropriate instance of the class at hand by the readObject() or defaultReadObject() methods. readResolve() throws ObjectStreamException;