Object Persistence and Object serialization CSNB534 Asma Shakil.

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Streams and Input/Output Files Part I. 2 Introduction So far we have used variables and arrays for storing data inside the programs. This approach poses.
Chapter 19 Binary I/O.
Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
Exceptions. Definition Exception: something unexpected that can occur in the execution of a program e.g., divide by zero or attempt to open a file that.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
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.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Lab#1 (14/3/1431h) Introduction To java programming cs425
Lecture 31 File I/O -Part 2 COMP1681 / SE15 Introduction to Programming.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
1 Chapter 7 Inheritance, Polymorphism, and Scope.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 18 Binary I/O.
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.
ASP.NET Programming with C# and SQL Server First Edition
Files and Streams (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
CSC – Java Programming II Lecture 9 January 30, 2002.
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.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
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.
Java How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
JAVA I/O © EnhanceEdu, IIIT Hyderabad. Contents 3/29/2010EnhanceEdu, IIIT - H 2  Command Line I/O  File Class  Streams  Byte Streams [Low level and.
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CIS 270—App Dev II Big Java Chapter 19 Files and Streams.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
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.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Binary I/O.
©SoftMoore ConsultingSlide 1 Serialization. ©SoftMoore ConsultingSlide 2 Serialization Allows objects to be written to a stream Can be used for persistence.
1 Chapter 19 Binary I/O. 2 Motivations F Data stored in a text file – is represented in human-readable form –Text file –Readable –Java source programs.
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.
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 20.
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.
Java Programming, Second Edition Chapter Sixteen File Input and Output.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
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
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Lecture 8: I/O Streams types of I/O streams Chaining Streams
CSG2H3 Object Oriented Programming
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
Chapter 17 Binary I/O.
Object Writing in files
I/O Basics.
Chapter 17 Binary I/O 1.
Accessing Files in Java
Java Serialization B.Ramamurthy 11/8/2018 B.Ramamurthy.
Client server programming
Java Serialization B.Ramamurthy 11/14/2018 B.Ramamurthy.
Serialization and Deserialization Bullet points from Head First Java, Ch Dec-18 serialization.ppt.
Workshop for Programming And Systems Management Teachers
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
Workshop for Programming And Systems Management Teachers
Object Oriented Programming in java
OO Java Programming Input Output.
Presentation transcript:

Object Persistence and Object serialization CSNB534 Asma Shakil

Introduction Data that can be read or written ranges from individual bytes to primitives datatypes and strings. We can write a data structure (such as a sequence of a data records, composed of individual fields) out to a file. How about if we wanted to store an entire object (composed of a series of member variables)?

Cont. This requires each field of the object to be written individually; then at later time, each field would be read back and assigned to an object. A complicated process! However, saving data is one of the most important functions of software. The solution is to use object persistence.

What is object persistence? The ability of an object to persist over time (if moved to a different machine, over space) Most objects in Java Virtual Machine are fairly short-lived. When there is no reference to the object, the memory space allocated to it is reclaimed by the automatic garbage collector thread. All objects will die at some point in time resulting in a loss of reference.

Cont. Object persistence allows an object to outlive the JVM that hosts it. A custom class representing a key component of a system, a java.util.Vector list containing state data or java.util.Properties object containing system settings are all good examples of objects that might be important enough to be needed every time a program is run.

Object Serialization Object Serialization is the technique by which object persistence is realized. It controls how the data that comprises an object’s state information (the individual member variables, whether public, private or protected) is written as a sequence of bytes. The serialized object might be sent over the network or saved to a disk so that it can be accessed at some point in the future. This allows objects to move from one JVM to another whether located on the same machine or on a remote one.

Cont. Serialization works by examining the variables of an object and writing primitives datatypes like numbers and characters to a byte stream. It also caters to the situation where an object is inside another object. If an object has a reference to an object which has a reference to another object, they are all saved together. The set of all objects referenced is called a graph of objects and object serialization converts entire graphs to byte form.

Graphs Vector Object iObject n OutputStream …

How serialization works Any object that implements the java.io.Serializable interface may be serialized with only a few lines of code. Implementing the java.io.Serializable interface requires no additional effort on the part of developers, other than adding the appropriate “implements” statement during the class declaration and declaring a no-argument constructor. The interface acts as an indication that the developer endorses serialization – no method needs to be implemented to support serialization.

Example Public class SomeClass extends SomeOtherClass implements java.io.Serializable { public class SomeClass() } ……… }

Issues Legitimate reasons for not supporting serialization also exist. For example, if an object contained very sensitive information it might be unwise to serialize it and save it to disk or send it over a network. Suppose a class stored password data that can be easily obtained. To prevent individual member variables being serialized, they can be marked with the transient keyword.

Example Public class UserAccount implements java.io.Serializable { protected String username; protected transient String password; public UserAccount( ) { …. }

Reading and Writing Objects to Streams The main point of serialization is to write an object out to a stream and to read it back This is accomplished by using the java.io.ObjectOutputStream and java.io.ObjectInputStream classes

ObjectInputStream Class Used to read a serialized object from a byte stream. To allow an object to be reconstituted back to its original form The ObjectInputStream class implements the ObjectInput interface which extends the DataInput interface. This mean that this class provides many methods with the same signature as DataInputStream.

Cont. Constructors Protected ObjectInputStream ( ) – provides a default constructor for ObjectInputStream subclasses. ObjectInputStream( InputStream input) – creates an object input stream connected to the specified input stream, capable of restoring serialized objects. Methods Similar to the DataInputStream class Because it can read primitive datatypes. Additional important method Public final Object readObject( ) – reads a serialized object from the stream reconstructs it to its original state except for transient and static fields.

ObjectOutputStream Class Serializes an object to a byte stream for the purpose of object persistence. May be connected to any existing output stream such as a file or a networking stream. Objects written to an ObjectOutputStream have all their member variables written. If the object contains references to other objects, they too will be written. Therefore, the ObjectOutputStream can write entire object graphs.

Cont. Constructors Protected ObjectOutputStream ( ) – default constructor. ObjectOutputStream (OutputStream output) – creates an object output stream capable of serializing objects to the specified output stream. Methods Void writeObject (Object object) – writes the specified object to the output stream through object serialization

Serialization Example Sample file (SerializationDemo.java)SerializationDemo.java

Cont. In this example, the list created doesn’t die when the application terminates – it is serialized to a FileOutputStream for later access // Save list and terminate System.out.println ("Saving list"); FileOutputStream fout = new FileOutputStream ( "list.out" ); // Construct an object output stream ObjectOutputStream oout = new ObjectOutputStream ( fout ); // Write the object to the stream oout.writeObject (list); fout.close(); System.exit(0);

FileInputStream fin = new FileInputStream ("list.out"); // Connect an object input stream to the list ObjectInputStream oin = new ObjectInputStream ( fin ); try { // Read the vector back from the list Object obj = oin.readObject(); // Cast back to a vector list = (Vector) obj; } catch (ClassCastException cce) { // Can't read it, create a blank one list = new Vector(); } catch (ClassNotFoundException cnfe) { // Can't read it, create a blank one list = new Vector(); }

Cont. When the application starts, it checks to see whether the serialized file exists. If so, the vector list is read back from the file along with any object references by the vector list

Summary You have learned: What streams are and how they work What readers and writers are and how they work How to connect filter streams to low-level streams How to connect readers and writers to streams About object serialization and how to read and write objects