Obiect Serialization and Persistence 对象序列化和持久化 Institute of Computer Software 2021/1/28 Nanjing University
对象序列化和持久化 Object Serialization and Persistence 2021/1/28 Institute of Computer Software Nanjing University 1
最病 过摘要 口对象序列化 口对象持久化 a language level a Databases Hibernate Institute of Computer Software 2021/1/28 Nanjing University
摘要 对象序列化 对象持久化 Language level Databases Hibernate 2021/1/28 Institute of Computer Software Nanjing University 4
最病 ○ bject Serialization 5 口Why a What 口HoW Institute of Computer Software 2021/1/28 Nanjing University
Object Serialization Why What How 2021/1/28 Institute of Computer Software Nanjing University 5
最病 A Java Object Serialization --Why 6 a Serialization is used for light weight persistence and for communication via sockets or remote method Invocation (RMI) Institute of Computer Software 2021/1/28 Nanjing University
Java Object Serialization -- Why Serialization is used for lightweight persistence and for communication via sockets or Remote Method Invocation (RMI). 2021/1/28 Institute of Computer Software Nanjing University 6
最病 A Java Object Serialization --Example 7 public class Client public static void main( String args[) try(//Create a socket Socket soc new Socket(Inet Address getLocalHost(), 8020) ○ utput Strean。= soc. get Output Stream(); Object Output s new Object Output Stream(o); s write Object("Todays date"); swrite Object(new Date())i sclose();] catch(Exception e)t System. out. printIn(e get Message()) System. out. printIn (" Error during serialization") System. exit(1);] Institute of Computer Software 2021/1/28 Nanjing University
Java Object Serialization -- Example public class Client { public static void main(String args[]) { try { // Create a socket Socket soc = new Socket(InetAddress.getLocalHost(), 8020); OutputStream o = soc.getOutputStream(); ObjectOutput s = new ObjectOutputStream(o); s.writeObject("Today's date"); s.writeObject(new Date()); s.flush(); s.close(); } catch (Exception e) { System.out.println(e.getMessage()); System.out.println("Error during serialization"); System.exit(1); } } } 2021/1/28 Institute of Computer Software Nanjing University 7
最病 A Java Object Serialization --Example 8 public class Server public static void main(String args[)( Server Socket ser= null: Socket soc String str null; Date d null; ser= new Server Socket(8020); sOc= ser. accept( Input Stream o F soc. getlnput Stream(; Objectlnput s new ObjectInput Stream(o); tr=(String)s readobject(; d=(Date)s readObject( sclose( System. out. printIn(str ); System. out. printin( d); 3 catch(Exception e) System. out. printin(e getMessage () System. out printIn("Error during serialization") System. exit(1) Institute of Computer Software 2021/1/28 Nanjing University
Java Object Serialization -- Example public class Server { public static void main(String args[]) { ServerSocket ser = null; Socket soc = null; String str = null; Date d = null; try { ser = new ServerSocket(8020); soc = ser.accept(); InputStream o = soc.getInputStream(); ObjectInput s = new ObjectInputStream(o); str = (String) s.readObject(); d = (Date) s.readObject(); s.close(); System.out.println(str); System.out.println(d); } catch (Exception e) { System.out.println(e.getMessage()); System.out.println("Error during serialization"); System.exit(1); } } } 2021/1/28 Institute of Computer Software Nanjing University 8
最病 A Java Object Serialization --Example 9 a Writing to an object stream l/ Serialize today's date to a file FileOutputStream f new FileOutput Stream(tmp") Objectoutput s= new ObjectoutputStream(t) s writeobject( Today) s writeobject(new DateD) S flush Institute of Computer Software 2021/1/28 Nanjing University
Java Object Serialization -- Example Writing to an object stream 2021/1/28 Institute of Computer Software Nanjing University 9 // Serialize today's date to a file. FileOutputStream f = new FileOutputStream("tmp"); ObjectOutput s = new ObjectOutputStream(f); s.writeObject("Today"); s.writeObject(new Date()); s.flush();
最病 A Java Object Serialization --Example n Reading from an object stream // Deserialize a string and date from a file FileInputStream in= new FileInputStream (" tmp") ObjectInputStream s= new objectInputstream(in) String today =(String)s. readobjecto Date date =(Date)s readobject( Institute of Computer Software 2021/1/28 Nanjing University
Java Object Serialization -- Example Reading from an object stream 2021/1/28 Institute of Computer Software Nanjing University 10 // Deserialize a string and date from a file. FileInputStream in = new FileInputStream("tmp"); ObjectInputStream s = new ObjectInputStream(in); String today = (String)s.readObject(); Date date = (Date)s.readObject();
最病 A Java Object Serialization --What a Object Serialization extends the core Java Input Output classes with support for objects a Object Serialization supports the encoding of objects, and the objects reachable from them, into a stream of bytes; and it supports the complementary reconstruction of the object graph from the stream Institute of Computer Software 2021/1/28 Nanjing University
Java Object Serialization -- What Object Serialization extends the core Java Input/Output classes with support for objects. Object Serialization supports the encoding of objects, and the objects reachable from them, into a stream of bytes; and it supports the complementary reconstruction of the object graph from the stream. 2021/1/28 Institute of Computer Software Nanjing University 11
最病 A Java Object Serialization --Goal o Have a simple yet extensible mechanism a maintain the Java object type and safety properties in the serialized form n Be extensible to support marshaling and unmarshaling as needed for remote objects a Be extensible to support simple persistence of java objects n Require per class implementation only for customization. o Allow the object to define its external format Institute of Computer Software 2021/1/28 Nanjing University
Java Object Serialization -- Goal Have a simple yet extensible mechanism. Maintain the Java object type and safety properties in the serialized form. Be extensible to support marshaling and unmarshaling as needed for remote objects. Be extensible to support simple persistence of Java objects. Require per class implementation only for customization. Allow the object to define its external format. 2021/1/28 Institute of Computer Software Nanjing University 12