🗊Презентация Serialization in Java

Нажмите для полного просмотра!
Serialization in Java, слайд №1Serialization in Java, слайд №2Serialization in Java, слайд №3Serialization in Java, слайд №4Serialization in Java, слайд №5Serialization in Java, слайд №6Serialization in Java, слайд №7Serialization in Java, слайд №8Serialization in Java, слайд №9Serialization in Java, слайд №10Serialization in Java, слайд №11Serialization in Java, слайд №12Serialization in Java, слайд №13Serialization in Java, слайд №14Serialization in Java, слайд №15Serialization in Java, слайд №16Serialization in Java, слайд №17Serialization in Java, слайд №18Serialization in Java, слайд №19Serialization in Java, слайд №20

Вы можете ознакомиться и скачать презентацию на тему Serialization in Java. Доклад-сообщение содержит 20 слайдов. Презентации для любого класса можно скачать бесплатно. Если материал и наш сайт презентаций Mypresentation Вам понравились – поделитесь им с друзьями с помощью социальных кнопок и добавьте в закладки в своем браузере.

Слайды и текст этой презентации


Слайд 1





Serialization in Java
Описание слайда:
Serialization in Java

Слайд 2





Object Serialization

We all know the Java platform allows us to create reusable objects in memory. However, all of those objects exist only as long as the Java virtual machine remains running. It would be nice if the objects we create could exist beyond the lifetime of the virtual machine, wouldn't it?

Object serialization is the process of saving an object's state to a sequence of bytes, as well as the process of rebuilding those bytes into a live object at some future time.
Описание слайда:
Object Serialization We all know the Java platform allows us to create reusable objects in memory. However, all of those objects exist only as long as the Java virtual machine remains running. It would be nice if the objects we create could exist beyond the lifetime of the virtual machine, wouldn't it? Object serialization is the process of saving an object's state to a sequence of bytes, as well as the process of rebuilding those bytes into a live object at some future time.

Слайд 3





Serializable
Class is serializable 
if
It can be transformed to array of bytes, and 
re-created from those bytes.
Описание слайда:
Serializable Class is serializable if It can be transformed to array of bytes, and re-created from those bytes.

Слайд 4





Usage
RMI
Enterprise Java Beans
JMS
Object Cache (disk storage)
Application Server clustering
…
Описание слайда:
Usage RMI Enterprise Java Beans JMS Object Cache (disk storage) Application Server clustering …

Слайд 5





Your own protocol
Описание слайда:
Your own protocol

Слайд 6





The Default Mechanism

To persist an object in Java, we must have a persistent object. An object is marked serializable by implementing the java.io.Serializable interface, which signifies to the underlying API that the object can be flattened into bytes and subsequently inflated in the future.

public interface Serializable {
}

Serializable is a marker interface; it has no methods to implement!
Описание слайда:
The Default Mechanism To persist an object in Java, we must have a persistent object. An object is marked serializable by implementing the java.io.Serializable interface, which signifies to the underlying API that the object can be flattened into bytes and subsequently inflated in the future. public interface Serializable { } Serializable is a marker interface; it has no methods to implement!

Слайд 7





Working with ObjectOutputStream and ObjectInputStream

ObjectOutputStream.writeObject() // serialize and write
ObjectInputStream.readObject() // read and deserialize
Описание слайда:
Working with ObjectOutputStream and ObjectInputStream ObjectOutputStream.writeObject() // serialize and write ObjectInputStream.readObject() // read and deserialize

Слайд 8





Bare-bones example

import java.io.*;

class Cat implements Serializable { } // 1

public class SerializeCat {
  public static void main(String[] args) {
    Cat c = new Cat(); // 2
    try {
      FileOutputStream fs = new FileOutputStream("testSer.ser");
      ObjectOutputStream os = new ObjectOutputStream(fs);
      os.writeObject(c); // 3
      os.close();
    } catch (Exception e) { e.printStackTrace(); }
    try {
      FileInputStream fis = new FileInputStream("testSer.ser");
      ObjectInputStream ois = new ObjectInputStream(fis);
      c = (Cat) ois.readObject(); // 4
      ois.close();
    } catch (Exception e) { e.printStackTrace(); }
  }
}
Описание слайда:
Bare-bones example import java.io.*; class Cat implements Serializable { } // 1 public class SerializeCat { public static void main(String[] args) { Cat c = new Cat(); // 2 try { FileOutputStream fs = new FileOutputStream("testSer.ser"); ObjectOutputStream os = new ObjectOutputStream(fs); os.writeObject(c); // 3 os.close(); } catch (Exception e) { e.printStackTrace(); } try { FileInputStream fis = new FileInputStream("testSer.ser"); ObjectInputStream ois = new ObjectInputStream(fis); c = (Cat) ois.readObject(); // 4 ois.close(); } catch (Exception e) { e.printStackTrace(); } } }

Слайд 9





?
Описание слайда:
?

Слайд 10





Object Graphs

What if the instance variables are themselves references to objects?

class Dog implements Serializable {
  private Collar theCollar;
  private int dogSize;
  public Dog(Collar collar, int size) {
    theCollar = collar;
    dogSize = size;
  }
  public Collar getCollar() { return theCollar; }
}
class Collar {
  private int collarSize;
  public Collar(int size) { collarSize = size; }
  public int getCollarSize() { return collarSize; }
}
Описание слайда:
Object Graphs What if the instance variables are themselves references to objects? class Dog implements Serializable { private Collar theCollar; private int dogSize; public Dog(Collar collar, int size) { theCollar = collar; dogSize = size; } public Collar getCollar() { return theCollar; } } class Collar { private int collarSize; public Collar(int size) { collarSize = size; } public int getCollarSize() { return collarSize; } }

Слайд 11





What did we forget?

import java.io.*;
public class SerializeDog {
  public static void main(String[] args) {
    Collar c = new Collar(3);
    Dog d = new Dog(c, 8);
    try {
      FileOutputStream fs = new FileOutputStream("testSer.ser");
      ObjectOutputStream os = new ObjectOutputStream(fs);
      os.writeObject(d);
      os.close();
    } catch (Exception e) { e.printStackTrace(); }
  }
}

But when we run this code we get a runtime exception something like this:

java.io.NotSerializableException: Collar

The Collar class must ALSO be Serializable!
Описание слайда:
What did we forget? import java.io.*; public class SerializeDog { public static void main(String[] args) { Collar c = new Collar(3); Dog d = new Dog(c, 8); try { FileOutputStream fs = new FileOutputStream("testSer.ser"); ObjectOutputStream os = new ObjectOutputStream(fs); os.writeObject(d); os.close(); } catch (Exception e) { e.printStackTrace(); } } } But when we run this code we get a runtime exception something like this: java.io.NotSerializableException: Collar The Collar class must ALSO be Serializable!

Слайд 12





Good news!

Only objects marked Serializable can be persisted.

These classes are already serializable:
 - Integer, Double, etc.
 - String
 - Date, Calendar
 - ArrayList, LinkedList, HashSet, HashMap, etc.

Array of serializable objects is also serializable!
Описание слайда:
Good news! Only objects marked Serializable can be persisted. These classes are already serializable: - Integer, Double, etc. - String - Date, Calendar - ArrayList, LinkedList, HashSet, HashMap, etc. Array of serializable objects is also serializable!

Слайд 13





Transient

What if we didn't have access to the Collar class source code? In that case, can we ever persist objects of Dog type?

If you mark the Dog's Collar instance variable with transient, then serialization will simply skip the Collar during serialization:

class Dog implements Serializable {
  private transient Collar theCollar; // add transient
  // the rest of the class as before
}
Описание слайда:
Transient What if we didn't have access to the Collar class source code? In that case, can we ever persist objects of Dog type? If you mark the Dog's Collar instance variable with transient, then serialization will simply skip the Collar during serialization: class Dog implements Serializable { private transient Collar theCollar; // add transient // the rest of the class as before }

Слайд 14





Using writeObject and readObject

When the Dog is deserialized, it comes back with a null Collar.

class Dog implements Serializable {
  transient private Collar theCollar; // we can't serialize this
  private int dogSize;
  public Dog(Collar collar, int size) {
    theCollar = collar;
    dogSize = size;
  }
  public Collar getCollar() { return theCollar; }

  private void writeObject(ObjectOutputStream os) {
    try {
      os.defaultWriteObject(); // 1
      os.writeInt(theCollar.getCollarSize()); // 2
    } catch (Exception e) { e.printStackTrace(); }
  }
  private void readObject(ObjectInputStream is) {
    try {
      is.defaultReadObject(); // 3
      theCollar = new Collar(is.readInt()); // 4
    } catch (Exception e) { e.printStackTrace(); }
  }
}
Описание слайда:
Using writeObject and readObject When the Dog is deserialized, it comes back with a null Collar. class Dog implements Serializable { transient private Collar theCollar; // we can't serialize this private int dogSize; public Dog(Collar collar, int size) { theCollar = collar; dogSize = size; } public Collar getCollar() { return theCollar; } private void writeObject(ObjectOutputStream os) { try { os.defaultWriteObject(); // 1 os.writeInt(theCollar.getCollarSize()); // 2 } catch (Exception e) { e.printStackTrace(); } } private void readObject(ObjectInputStream is) { try { is.defaultReadObject(); // 3 theCollar = new Collar(is.readInt()); // 4 } catch (Exception e) { e.printStackTrace(); } } }

Слайд 15





Serialization Is Not for Statics

You should think of static variables purely as CLASS variables.

Serialization applies only to OBJECTS.

Static variables are NEVER saved as part of the object's state…because they do not belong to the object!
Описание слайда:
Serialization Is Not for Statics You should think of static variables purely as CLASS variables. Serialization applies only to OBJECTS. Static variables are NEVER saved as part of the object's state…because they do not belong to the object!

Слайд 16





?
Описание слайда:
?

Слайд 17





Create Your Own Protocol: the Externalizable Interface

Instead of implementing the Serializable interface, you can implement Externalizable, which contains two methods:

public void writeExternal(ObjectOutput out) throws IOException;
public void readExternal(ObjectInput in) throws IOException,
                                         ClassNotFoundException;

Just override those methods to provide your own protocol. Although it's the more difficult scenario, it's also the most controllable. An example situation for that alternate type of serialization: read and write PDF files with a Java application.
Описание слайда:
Create Your Own Protocol: the Externalizable Interface Instead of implementing the Serializable interface, you can implement Externalizable, which contains two methods: public void writeExternal(ObjectOutput out) throws IOException; public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException; Just override those methods to provide your own protocol. Although it's the more difficult scenario, it's also the most controllable. An example situation for that alternate type of serialization: read and write PDF files with a Java application.

Слайд 18





Externalizable
Описание слайда:
Externalizable

Слайд 19





serialVersionID
 Used when deserialization for deciding if the serialized and loaded classes are compatible
 If not present, JVM computes this value automatically
Описание слайда:
serialVersionID Used when deserialization for deciding if the serialized and loaded classes are compatible If not present, JVM computes this value automatically

Слайд 20





Links
Java Object Serialization Specification
Implementing Serializable: best practices
Описание слайда:
Links Java Object Serialization Specification Implementing Serializable: best practices



Теги Serialization in Java
Похожие презентации
Mypresentation.ru
Загрузить презентацию