Advanced Java Class Serialization. Serialization – what and why? What? –Translating the contents of an Object to a series of bytes that represent it,

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.
Chapter 19 Binary I/O.
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.
Chapter - 12 File and Streams (continued) This chapter includes -  DataOutputStream  DataInputStream  Object Serialization  Serializing Objects 
Geoff Holmes Overview IO Zoo Stream I/O File I/O Buffering Random-Access Text Streams Examples Serialization Java IO – programs that start with import.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Lecture 31 File I/O -Part 2 COMP1681 / SE15 Introduction to Programming.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
Java Iterators interface Collection { … Iterator iterator(); Iterator iterator(); …} interface Set extends Collection { … Iterator iterator(); Iterator.
1 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
1 Chapter 7 Inheritance, Polymorphism, and Scope.
File I/O in Java CS 311, Winter File Basics Recall that a file is block structured. What does this mean? What happens when an application opens.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 18 Binary I/O.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 19 Binary I/O.
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.
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.
Object Persistence and Object serialization CSNB534 Asma Shakil.
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
Lecture 19 Serialization Richard Gesick. Serialization Sometimes it is easier to read or write entire objects than to read and write individual fields.
Getting Started on Lab 1 Requirements MigratableProcess TransactionalFile{Input, Output}Stream ProcessManager MigratableProcesses.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
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.
Copyright(c) Systems and Computer Engineering, Carleton Univeristy, * Object-Oriented Software Development Unit 13 I/O Stream Hierarchy Case.
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.
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.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 20.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 13 Java Fundamentals File I/O Serializing an.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Files in Java 1.
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
Serialization 4: Serializable refresh Review of Java Object Serialization and Relationship with RMI-IIOP June 25, 2002.
Operating Systems {week 11a}
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
Lecture 20 File I/O D&D 14 Date.
Chapter 8: Input and Output
Chapter 17 Binary I/O.
Object Writing in files
Memento Design Pattern
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
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
Java Basics Introduction to Streams.
The command invocation protocol
OO Java Programming Input Output.
Presentation transcript:

Advanced Java Class Serialization

Serialization – what and why? What? –Translating the contents of an Object to a series of bytes that represent it, and may later be used to re-create it Reasons to Serialize –Send them over a network connection including RMI, Sockets, etc. –Save to file

Pre-existing Serialization Functionality For primitives and Strings: –DataOutputStream and DataInputStream For other Objects: –ObjectOutputStream and ObjectInputStream –The object to be serialized must be an instance of the Serializable interface.

ObjectOutputStream and ObjectInputStream new ObjectOutputStream(OutputStream) new ObjectInputStream(InputStream) The arguments to the constructor can be any subclass of OutputStream / InputStream, and the Object Stream will send the serialized representation of the object along the stream that was passed into the constructor

Refusing Serialization To prevent an object from being serialized by the Output Streams, you can just not implement Serializable If you want to exclude one particular variable from Serialization, you may declare it transient. For runtime control of which fields are serialized, use –private static final ObjectStreamField[] serialPersistentFields =...;

Controlling Serialization Optional methods you can include in a class definition: (These are NOT in the Serializable interface.) –private void readObject (ObjectInputStream ois) throws ClassNotFoundException, IOException {...[must include a call to ois.defaultReadObject()]... } –private void writeObject (ObjectOutputStream oos) throws IOException {...[must include a call to oos.defaultWriteObject]... }

ObjectOutputStream details To send the Object over the stream more than once, you must call the reset method on the stream. You may set the version number of your class if you want: –to find the number: serialver class_name (at command line) –to set the number in the class: static final long serialVersionUID =...; If you don’t set it yourself, the JVM will set one automatically, but it may not change as often as you wish.

Your Serializer Project Must not use the Serializable interface Must not use ObjectInputStream Must not use ObjectOutputStream Must convert the Object to XML.