Detailed serialization and deserialization stream flow

(Watch my blog - "Detailed byte stream" )

Serialization and deserialization stream stream:

There may be a problem when students see this post:
What is the serialization and de-serialization of it?

Well, now, I just took off in terms of the definition of these two terms:

  • Serialization:
    is the object storage through the streamed file
  • Deserialize:
    is stored in the object file to stream objects is reduced to

Now, I come on the role of these two streams:

ObjectInputStream basic data and objects previously written using an ObjectOutputStream deserialized.
ObjectInputStream ObjectOutputStream and when used in conjunction with each FileOutputStream and FileInputStream,
may be provided as an application persistent storage object graphic .
Moreover, the class of the object store must inherit java.io.Serializable interface. Otherwise, the stored data can not be read

Before explaining these two streams, I now want to explain under transient Keywords :

transient Keywords:
modified members, in not serialized.
So when we used to store serialized stream, when reading the results obtained and we read the unassigned members of the same results.

Well, now, himself under these two streams are explained:


First, the sequence of flow (the ObjectOutputStream class):

ObjectOutputStream 类:

(Serialization stream)

Overview :

Such is the basic data types of the Java object written to the OutputStream and graphics
may be used to read ObjectInputStream (reconstructed) object
by using the file in the stream object persistent storage may be achieved
if the stream is a network socket stream , on the other may be on a host or in another process to be rearranged

Now, I have to show this class constructor :

Constructor :

  • protected ObjectOutputStream ()
    provides a method for the subclass of ObjectOutputStream completely re-implemented,
    it is not necessary to use only the assigned private data implementation of ObjectOutputStream
  • ObjectOutputStream (OutputStream out)
    Create a ObjectOutputStream writes the specified OutputStream

Well, I have to show this kind of commonly used API :

Common API :

  • protected void annotateClass (Class <?> cl)
    Subclasses may implement this method, so as to allow in the stream stored in the class data
  • protected void annotateProxyClass (Class <?> cl)
    Subclasses may implement this method, so that in the stream is stored customization data and dynamic proxy class descriptor
  • void close ()
    Close stream
  • void defaultWriteObject ()
    of the current class of non-static and non-transient fields to this stream
  • protected void drain ()
    evacuated in ObjectOutputStream all buffered data
  • protected boolean enableReplaceObject (boolean enable)
    allows objects to be replaced in the convective
  • void flush ()
    Flush the stream
  • ObjectOutputStream.PutField putFields ()
    Gets the object used to buffer persistent fields written in the stream
  • protected Object replaceObject (Object obj)
    during serialization,
    this method allows the ObjectOutputStream trusted subclasses using one object to another object alternative
  • void reset ()
    resets the discard state of all objects already written in the stream
  • void useProtocolVersion (int version)
    Specify stream protocol version to use when writing the stream
  • void write (byte [] buf)
    writes a byte array
  • void write (byte [] buf, int off, int len)
    subarray of bytes to be written
  • void write (int val)
    Writes a byte
  • void writeBoolean (boolean val)
    write a boolean
  • void writeByte (int val)
    writes an 8-bit byte
  • void writeBytes (String str)
    written in the form of a sequence of bytes String
  • void writeChar (int val)
    writes a 16-bit value of char
  • void writeChars (String str)
    written in a form of a sequence String char
  • protected void writeClassDescriptor (ObjectStreamClass desc)
    specified class descriptor is written ObjectOutputStream
  • void writeDouble (double val)
    writes a 64 bit double
  • void writeFields ()
    which has been written to the stream buffer field
  • void writeFloat (float val)
    writes a 32-bit float value
  • void writeInt (int val)
    writes a 32-bit int value
  • void writeLong (long val)
    writes a 64-bit long value
  • void writeObject (Object obj)
    to write the specified object ObjectOutputStream
  • protected void writeObjectOverride (Object obj)
    subclasses to override the default method writeObject method
  • void writeShort (int val)
    writes a 16-bit short
  • protected void writeStreamHeader ()
    writeStreamHeader method is provided,
    so that subclasses can be added or added to the stream of its own pre-head
  • void writeUnshared (Object obj)
    will " unshared " object into the ObjectOutputStream
  • void writeUTF (String str)
    to write data of this String substantially modified UTF-8 Format

Now, I use to show the lower part of the API:


First of all, I give FanInfo a fan is used to store information:

package edu.youzg.about_io.about_file.core;

import java.io.Serializable;

public class FanInfo implements Serializable {
    private String name;
    //transient关键字 表示 某个成员变量不想序列化
    private transient int age;

    public FanInfo() {
    }

    public FanInfo(String name, int age) {
        this.name = name;
        this.age = age;
    }

    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;
    }

}

Now, I have to write a test class:

package edu.youzg.about_io.about_file.core.Test;

import edu.youzg.about_io.about_file.core.FanInfo;

import java.io.*;

public class Test {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        FanInfo FanInfo3 = new FanInfo("朝菌", 1);
        FanInfo FanInfo1 = new FanInfo("南冥灵者", 500);
        FanInfo FanInfo2 = new FanInfo("大椿", 8000);
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("FanInfo1.txt"));
        out.writeObject(FanInfo1);
        out.writeObject(FanInfo2);
        out.writeObject(FanInfo3);

        out.close();
    }

}

Well, now, I have to show the results of the run and the content of the resulting file:
Here Insert Picture Description


Now, I am speaking off his anti-serialized stream :

ObjectInputStream class:

(Deserialized flow):

Overview :

ObjectInputStream is used to recover those objects previously serialized.
Other uses include the use of socket stream passing objects between the host,
or for marshalling and unmarshalling telecommunications system and parameter arguments.
ObjectInputStream sure that the class displayed in the graphical flow created all of the objects of the Java virtual machine to match.
Use standard mechanisms to load classes.
Only support or java.io.Externalizable java.io.Serializable interface objects can be read from the stream.

I am the first to show this class constructor :

Constructor :

  • protected ObjectInputStream ()
    provides a way to completely re-implement the subclass of ObjectInputStream,
    it is not necessary to use only the assigned private data implementation of ObjectInputStream
  • ObjectInputStream (InputStream in)
    create ObjectInputStream reads from the specified InputStream

Now, I have to show this kind of commonly used API :

Common API:

  • int available ()
    returns the number of bytes that can be read without blocking
  • void close ()
    Closes the input stream
  • void defaultReadObject ()
    from the stream reading non-static and non-transient fields of the current class
  • protected boolean enableResolveObject (boolean enable)
    the flow is allowed to read from the stream object is an alternative
  • int read ()
    to read the data bytes
  • int read (byte [] buf, int off, int len)
    reads a byte array
  • the readBoolean boolean ()
    reads a boolean
  • byte readByte ()
    reads an 8-bit byte
  • the readChar char ()
    reads a 16-bit value of char
  • protected ObjectStreamClass readClassDescriptor ()
    from the serialization stream class descriptor read
  • double the readDouble ()
    to read a 64 bit double
  • ObjectInputStream.GetField readFields ()
    Reads the persistent fields from the stream and make it available by name
  • the readFloat float ()
    to read a 32-bit float values
  • void readFully (byte [] buf)
    read bytes, blocking until all bytes are read
  • void readFully (byte [] buf, int off, int len)
    Reads bytes, blocking until all bytes are read
  • the readInt int ()
    to read a 32-bit int value
  • long the readLong ()
    reads a 64-bit long value
  • Object readObject ()
    reads an object from the ObjectInputStream
  • protected Object readObjectOverride ()
    This method is called by trusted subclasses of ObjectOutputStream subclasses using the no-arg constructor constructed ObjectOutputStream protected
  • short the readShort ()
    reads a 16-bit short
  • The readStreamHeader void protected ()
    The readStreamHeader method is provided to allow subclasses to read and verify their own stream headers
  • Object readUnshared ()
    reads "non-shared" object from the ObjectInputStream
  • int readUnsignedByte ()
    to read an unsigned 8-bit bytes
  • int readUnsignedShort ()
    to read an unsigned 16-bit short
  • String the readUTF ()
    reads the modified version of the UTF-8 format String
  • void registerValidation (ObjectInputValidation obj, int prio )
    register the object to be validated before returning Graphics
  • protected Class <?> resolveClass (ObjectStreamClass desc)
    loading the specified stream class description local equivalence classes
  • protected Object resolveObject (Object obj)
    deserialization time, this method allows trusted subclasses of ObjectInputStream use an object other alternative
  • protected Class resolveProxyClass (String [] interfaces ) <?>
    returns a proxy class that implements named in a proxy class descriptor interfaces;
    Subclasses may implement this method to read from the stream and the dynamic proxy class descriptor custom data, allow them to use alternative loading mechanism and the proxy class interfaces
  • int skipBytes (int len)
    skipped bytes
  • String readLine ()
    Obsolete . This method does not properly convert bytes to characters. See DataInputStream for the details and alternatives

Well, now, I have to show by example a lower portion of the API used:

Now, a test class I given above for the code generation .txt files:

package edu.youzg.about_io.about_file.core.Test;

import edu.youzg.about_io.about_file.core.FanInfo;

import java.io.*;

public class Test {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectInputStream objin = new ObjectInputStream(new FileInputStream("FanInfo1.txt"));
        Object obj = objin.readObject();
        FanInfo FanInfo= (FanInfo) obj;
        System.out.println(FanInfo.getName());
        obj = objin.readObject();
        FanInfo = (FanInfo) obj;
        System.out.println(FanInfo.getName());
        obj = objin.readObject();
        FanInfo = (FanInfo) obj;
        System.out.println(FanInfo.getAge());

        objin.close();
    }

}

Now, I have to show at the results:
Here Insert Picture Description
you can see:
Age Fan members of each object we stored, because we use the transient keyword to modify.
So when we read, the output is 0
(after various types of modified transient keyword, as a result of reading the results and we read the unassigned members obtained)

Well, now, I am modifications FanInfo class - the name was changed to members of the public modifier modified .
Then, run the test again like I gave:
Here Insert Picture Description
you can see, there has been an exception.
This is because we store the object information and object information when we read different.
In fact, the flow detection of the class is serialized version number

In fact, we can also be overcome by setting yourself a serialized version of this number,
for example, it will FanInfo class to read as follows:

package edu.youzg.about_io.about_file.core;

import java.io.Serializable;

public class FanInfo implements Serializable {
    private static final long serialVersionUID = -7602640005373026150L;

    private String name;
    private transient int age;

    public FanInfo() {
    }

    public FanInfo(String name, int age) {
        this.name = name;
        this.age = age;
    }

    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;
    }

}

So, I again run before generating code file, and then name the members of the class changed FanInfo public modifier modified, again run the following code:

import edu.youzg.about_io.about_file.core.FanInfo;

import java.io.*;

public class Test {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectInputStream objin = new ObjectInputStream(new FileInputStream("FanInfo2.txt"));
        Object obj = objin.readObject();
        FanInfo FanInfo= (FanInfo) obj;
        System.out.println(FanInfo.getName());
        obj = objin.readObject();
        FanInfo = (FanInfo) obj;
        System.out.println(FanInfo.getName());
        obj = objin.readObject();
        FanInfo = (FanInfo) obj;
        System.out.println(FanInfo.getAge());
    }
    
}

Here Insert Picture Description
We can see, read no problem!

So, about the sequence of flow and knowledge deserialized stream, where basically explain finished!

(I "I / O stream" collection of articles Bowen link: https://blog.csdn.net/weixin_45238600/article/details/104153031 )

Published 133 original articles · won praise 82 · views 5994

Guess you like

Origin blog.csdn.net/weixin_45238600/article/details/104238896