In Java, everything is an object, and in a distributed environment, it is often necessary to pass an Object from one end of the network or device to the other end. This requires a protocol that can transmit data on both ends. The Java serialization mechanism is created to solve this problem.
After converting the object state into a byte stream, you can save it to a file using classes of various byte streams in the java.io package, pipe into another thread, or send object data to another host through a network connection. The object serialization function is very simple and powerful, and it is used in RMI, Socket, JMS, and EJB. The object serialization problem is not the most core issue in network programming, but it is very important and has many practical significance.
Java object serialization not only retains the data of one object, but also recursively saves the data of each object referenced by the object. The entire object hierarchy can be written into a byte stream, saved in a file, or passed on a network connection. The object serialization can be used to perform "deep copying" of the object, that is, copying the object itself and the referenced object itself. Serializing an object can result in a sequence of the entire object.
Basic usage method:
Serialization refers to persisting a class or basic data type into a data stream, including file, byte stream, and network data stream.
The implementation of serialization in JAVA mainly relies on two classes: ObjectOututStream and ObjectInputStream. They are subclasses of OutputStream and InputStream in the JAVA IO system. Since they are streams in JAVA IO, they can operate them like streams. Here is how they are used:
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class Pair implements Serializable{ private static final long serialVersionUID = -1874850715617681161L; private int type; private String name; public int getType() { return type; } public void setType(int type) { this.type = type; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Pair(int type, String name) { super(); this.type = type; this.name = name; } public static void main(String[] args) throws IOException, ClassNotFoundException { // TODO Auto-generated method stub //serialize object pair ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); Pair pair = new Pair(1, "charlie"); oos.writeObject(pair); //deserialize object, get new object newpair ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); Pair newpair = (Pair) ois.readObject(); System.out.println(newpair.getType()+":"+newpair.getName()); } }1. Both classes are in the decorator pattern. When creating them, they must pass in a byte-based stream. These streams are the ones that really store serialized data below.
2. The persisted class needs to implement the Serializable interface. This interface has no functions, but is just a marking interface. If serialization is performed on one machine and the resulting data is transmitted to another machine for deserialization, then the classes on these two machines should be exactly the same, otherwise the serialization will not be successful.
3. Remember not to use toString to get the String in the above code, then get the ByteArrayInputStream from this String, and then deserialize it. Bos is stored in bytes. Converting to String stored in characters will inevitably cause data changes, and the byte[] drawn from the String will not be the previous byte[]. I've encountered this problem because I want to store the serialized data in the xml file. See another article in my specific solution to this problem:
www.VeVB.COM/article/88130.htm
What does the java virtual machine do when serializing and deserializing it?
The java serialization mechanism in the description of these two classes is described in detail in javadoc:
Quote
The default serialization mechanism for an object writes the class of the object, the class signature, and the values of all non-transient and non-static fields. References to other objects (except in transient or static fields) cause those objects to be written also. Multiple references to a single object are encoded using a reference sharing mechanism so that graphs of objects can be restored to the same shape as when the original Was written.
The default serialization mechanism writes data to the stream include:
1. The class to which the object belongs
2. Class signature
3. All non-transient and non-static properties
4. References to other objects will also cause serialization of these objects.
5. If multiple references point to an object, then the sharing reference mechanism will be used.
Quote
Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures:
private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException; private void writeObject(java.io.ObjectOutputStream stream) throws IOException private void readObjectNoData() throws ObjectStreamException;