What is object serialization?

前端 未结 14 2120
长发绾君心
长发绾君心 2020-11-21 23:23

What is meant by \"object serialization\"? Can you please explain it with some examples?

14条回答
  •  鱼传尺愫
    2020-11-22 00:01

    Java Object Serialization

    Serialization is a mechanism to transform a graph of Java objects into an array of bytes for storage(to disk file) or transmission(across a network), then by using deserialization we can restore the graph of objects. Graphs of objects are restored correctly using a reference sharing mechanism. But before storing, check whether serialVersionUID from input-file/network and .class file serialVersionUID are the same. If not, throw a java.io.InvalidClassException.

    Each versioned class must identify the original class version for which it is capable of writing streams and from which it can read. For example, a versioned class must declare:

    serialVersionUID Syntax

    // ANY-ACCESS-MODIFIER static final long serialVersionUID = (64-bit has)L;
    private static final long serialVersionUID = 3487495895819393L;
    

    serialVersionUID is essential to the serialization process. But it is optional for the developer to add it into the java source file. If a serialVersionUID is not included, the serialization runtime will generate a serialVersionUID and associate it with the class. The serialized object will contain this serialVersionUID along with other data.

    Note - It is strongly recommended that all serializable classes explicitly declare a serialVersionUID, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected serialVersionUID conflicts during deserialization, causing deserialization to fail.

    Inspecting Serializable Classes


    A Java object is only serializable. if a class or any of its superclasses implements either the java.io.Serializable interface or its subinterface, java.io.Externalizable.

    • A class must implement java.io.Serializable interface in order to serialize its object successfully. Serializable is a marker interface and used to inform the compiler that the class implementing it has to be added serializable behavior. Here Java Virtual Machine (JVM) is responsible for its automatic serialization.

      transient Keyword: java.io.Serializable interface

      While serializing an object, if we don't want certain data members of the object to be serialized we can use the transient modifier. The transient keyword will prevent that data member from being serialized.

      • Fields declared as transient or static are ignored by the serialization process.

      TRANSIENT & VOLATILE

      +--------------+--------+-------------------------------------+
      |  Flag Name   |  Value | Interpretation                      |
      +--------------+--------+-------------------------------------+
      | ACC_VOLATILE | 0x0040 | Declared volatile; cannot be cached.|
      +--------------+--------+-------------------------------------+
      |ACC_TRANSIENT | 0x0080 | Declared transient; not written or  |
      |              |        | read by a persistent object manager.|
      +--------------+--------+-------------------------------------+
      
      class Employee implements Serializable {
          private static final long serialVersionUID = 2L;
          static int id;
      
          int eno; 
          String name;
          transient String password; // Using transient keyword means its not going to be Serialized.
      }
      
    • Implementing the Externalizable interface allows the object to assume complete control over the contents and format of the object's serialized form. The methods of the Externalizable interface, writeExternal and readExternal, are called to save and restore the objects state. When implemented by a class they can write and read their own state using all of the methods of ObjectOutput and ObjectInput. It is the responsibility of the objects to handle any versioning that occurs.

      class Emp implements Externalizable {
          int eno; 
          String name;
          transient String password; // No use of transient, we need to take care of write and read.
      
          @Override
          public void writeExternal(ObjectOutput out) throws IOException {
              out.writeInt(eno);
              out.writeUTF(name);
              //out.writeUTF(password);
          }
          @Override
          public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
              this.eno = in.readInt();
              this.name = in.readUTF();
              //this.password = in.readUTF(); // java.io.EOFException
          }
      }
      
    • Only objects that support the java.io.Serializable or java.io.Externalizable interface can be written to/read from streams. The class of each serializable object is encoded including the class name and signature of the class, the values of the object's fields and arrays, and the closure of any other objects referenced from the initial objects.

    Serializable Example For Files

    public class SerializationDemo {
        static String fileName = "D:/serializable_file.ser";
    
        public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
            Employee emp = new Employee( );
            Employee.id = 1; // Can not Serialize Class data.
            emp.eno = 77;
            emp.name = "Yash";
            emp.password = "confidential";
            objects_WriteRead(emp, fileName);
    
            Emp e = new Emp( );
            e.eno = 77;
            e.name = "Yash";
            e.password = "confidential";
            objects_WriteRead_External(e, fileName);
    
            /*String stubHost = "127.0.0.1";
            Integer anyFreePort = 7777;
            socketRead(anyFreePort); //Thread1
            socketWrite(emp, stubHost, anyFreePort); //Thread2*/
    
        }
        public static void objects_WriteRead( Employee obj, String serFilename ) throws IOException{
            FileOutputStream fos = new FileOutputStream( new File( serFilename ) );
            ObjectOutputStream objectOut = new ObjectOutputStream( fos );
            objectOut.writeObject( obj );
            objectOut.close();
            fos.close();
    
            System.out.println("Data Stored in to a file");
    
            try {
                FileInputStream fis = new FileInputStream( new File( serFilename ) );
                ObjectInputStream ois = new ObjectInputStream( fis );
                Object readObject;
                readObject = ois.readObject();
                String calssName = readObject.getClass().getName();
                System.out.println("Restoring Class Name : "+ calssName); // InvalidClassException
    
                Employee emp = (Employee) readObject;
                System.out.format("Obj[No:%s, Name:%s, Pass:%s]", emp.eno, emp.name, emp.password);
    
                ois.close();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        public static void objects_WriteRead_External( Emp obj, String serFilename ) throws IOException {
            FileOutputStream fos = new FileOutputStream(new File( serFilename ));
            ObjectOutputStream objectOut = new ObjectOutputStream( fos );
    
            obj.writeExternal( objectOut );
            objectOut.flush();
    
            fos.close();
    
            System.out.println("Data Stored in to a file");
    
            try {
                // create a new instance and read the assign the contents from stream.
                Emp emp = new Emp();
    
                FileInputStream fis = new FileInputStream(new File( serFilename ));
                ObjectInputStream ois = new ObjectInputStream( fis );
    
                emp.readExternal(ois);
    
                System.out.format("Obj[No:%s, Name:%s, Pass:%s]", emp.eno, emp.name, emp.password);
    
                ois.close();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
    

    Serializable Example Over Network

    Distributing object's state across different address spaces, either in different processes on the same computer, or even in multiple computers connected via a network, but which work together by sharing data and invoking methods.

    • Data Marshalling
    • Stubs and Skeletons
    /**
     * Creates a stream socket and connects it to the specified port number on the named host. 
     */
    public static void socketWrite(Employee objectToSend, String stubHost, Integer anyFreePort) {
        try { // CLIENT - Stub[marshalling]
            Socket client = new Socket(stubHost, anyFreePort);
            ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());
            out.writeObject(objectToSend);
            out.flush();
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // Creates a server socket, bound to the specified port. 
    public static void socketRead(  Integer anyFreePort ) {
        try { // SERVER - Stub[unmarshalling ]
            ServerSocket serverSocket = new ServerSocket( anyFreePort );
            System.out.println("Server serves on port and waiting for a client to communicate");
                /*System.in.read();
                System.in.read();*/
    
            Socket socket = serverSocket.accept();
            System.out.println("Client request to communicate on port server accepts it.");
    
            ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
            Employee objectReceived = (Employee) in.readObject();
            System.out.println("Server Obj : "+ objectReceived.name );
    
            socket.close();
            serverSocket.close();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    

    @see

    • implementing Serializable vs Externalizable

提交回复
热议问题