Deserialization using the Externalizable Interface in Java

I am learning the Serializable and Externalizable interface, and I see that when the Externalizable object is restored, the instance is created first using the open no-arg constructor, then the readExternal method is called. If the object does not support Externalizable, Serializable objects are restored by reading them from ObjectInputStream.

I don’t understand why we use ObjectInputStream for Appearance if the object cannot be read from there? What exactly is read from an ObjectInputStream? I think we are reading something from there if we use it.

I also found this diagram about deserializing an Externalizable or Serializable interface

enter image description here

What is the difference between Serializable and Externalizable during deserialization?

, Externalizable , ObjectInputStream , Serializable?

ObjectInputStream ois = new ObjectInputStream(
                new FileInputStream("employee.ser"))

, FileInputStream , . ObjectInputStream , .

.

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

public class Employee implements Externalizable {

    private int id;
    private String name;
    private int age;

    public void Employee() {


    } 

    public int getId() {

        return id;
    }

    public void setId(int id) {

        this.id = id;
    }

    public String getName() {

        return name;
    }

    public void setName(String name) {

        this.name = name;
    }

    public int getAge() {

        return age;
    }

    public void setAge(int age) {

        this.age = age;
    }

    public String toString() {

        return "Employee [id=" + id + ", name=" + name + ", age=" + age + "]";
    }

    public void writeExternal(ObjectOutput oo) throws IOException {

        System.out.println("Inside writeExternal method");
        oo.writeInt(id); 
        oo.writeObject(name); 
    }

    public void readExternal(ObjectInput oi) throws IOException, ClassNotFoundException {

        System.out.println("Inside readExternal method");
        id = oi.readInt();
        name = (String) oi.readObject();
    }
}

ExternalizableWrite

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class ExternalizableWrite {

    public static void main(String args[]) {

        ExternalizableWrite ew = new ExternalizableWrite();
        ew.writeEmployeeObject();
    }

    private void writeEmployeeObject() {

        try (ObjectOutputStream oos = new ObjectOutputStream(
                new FileOutputStream("employee.ser"))) {

            Employee employee = new Employee();
            employee.setId(101);
            employee.setName("Peter"); 
            employee.setAge(25); 

            System.out.println(employee);

            oos.writeObject(employee);  // write the specified object to the ObjectOutputStream

            System.out.println("Successfully written employee object to the file.\n");

        } catch (FileNotFoundException ex) {

            System.out.printf("ERROR: %s", ex); 

        } catch (IOException ex) {

            System.out.printf("ERROR: %s", ex); 
        }
    }
}

ExternalizableRead

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

public class ExternalizableRead {

    public static void main(String args[]) {

        ExternalizableRead er = new ExternalizableRead();
        er.readEmployeeObject();
    }

    private void readEmployeeObject() {

        try (ObjectInputStream ois = new ObjectInputStream(
                new FileInputStream("employee.ser"))) {

            Employee employee = (Employee) ois.readObject();

            System.out.println(employee);

            System.out.println("Successfully read employee objecct to the file.\n");

        } catch (FileNotFoundException ex) {

            System.out.printf("ERROR: %s", ex);

        } catch (IOException | ClassNotFoundException ex) {

            System.out.printf("ERROR: %s", ex);
        } 
    }
}
+4
2

Serializable Externalizable ?

ObjectInputStream, Externalizable , Serializable , :

if (desc.isExternalizable()) {
    readExternalData((Externalizable) obj, desc);
} else {
    readSerialData(obj, desc);
}

, readExternalData Externalizable#readExternal , , readSerialData .

, ObjectInputStream , ?

, , ObjectInputStream Externalizable, .

, Externalizable , ObjectInputStream , Serializable?

Externalizable , Serializable .

0

, ObjectInputStream , ?

Externalizable ObjectInputStream.
ObjectInputStream - , readExternal. , readInt, readFloat .. ObjectInputStream.

Serializable Externalizable ?

( Serializable) , , :

private void writeObject(java.io.ObjectOutputStream out) throws IOException

private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;

, A B,

  class A implement Serializable {
     writeObject(...){....}
     readObject(...){....}
  } 

  class B extends A implements Serializable{
     writeObject(...){....}
     readObject(...){....}
  }

B /-, writeObject/readObject / A , B, ", / -serialize.

, Externalizable, . readExternal writeExternal, /-.

0

Source: https://habr.com/ru/post/1696221/


All Articles