Java serialization Serializable
Java’s serialization process is as follows:
Java’s deserialization process is as follows:
Note: Not all classes need to be serialized, there are two main reasons
1) Security Question. Some classes in Java are sensitive classes, and the object data of this type is inconvenient to be disclosed to the outside world. The serialized object data is easy to crack, and the security of the data cannot be guaranteed. Therefore, generally this type of object will not be serialized.
2) Resource issues. Objects can be created using serialized byte streams, and this creation is not restricted. Sometimes creating too many objects can cause big resource problems, so such objects are not suitable for serialization.
Serializable
Serializable is a serialization interface provided by Java. It is an empty interface that provides standard serialization and deserialization operations for objects.
Serialization process:
Person p = new Person("name","id"); File file = new File("cache.txt"); FileOutputStream output = new FileOutputStream(file); ObjectOutputStream objectOutputStream = new ObjectOutputStream(output); objectOutputStream.writeObject(p); output.close(); objectOutputStream.close();
Deserialization process:
File file = new File("cache.txt"); FileInputStream input= new FileInputStream(file); ObjectInputStream objectInputStream = new ObjectInputStream(input); Person p = (Person)objectInputStream.readObject(); System.out.println(p.getName()+"---"+p.getId()); input.close(); objectInputStream.close();
Class members that need to be serialized
When an object is serialized, not all members need to be converted into binary byte sequences, because in order to save storage or Transmission space and improve serialization efficiency, some unnecessary members do not need to be serialized. These include:
Static variables. Because static variables belong to the attributes of the class and do not belong to a specific instance, there is no need to serialize during serialization. During deserialization, you can directly obtain the static member reference of the class.
Method. A method is just a set of operations. The method does not depend on the object, and the operations will not be different depending on the object. During deserialization, the method information can also be obtained directly from the class.
Serialization of inheritance relationship
When the parent class implements Serializable, the child class is serialized and the parent class will also be serialized.
When the parent class does not implement Serializable, the subclass is serialized and the parent class will not be serialized.
Serialization of reference relationships
If a serialization operation is performed on a class that implements Serializable, it will be serialized at the same time. The reference class performs serialization operations. If the reference class does not implement the Serializable interface, the JVM will throw java.io.NotSerializableExeception.
class Person implements Serializable{ private String name; private Tool tool = new Tool(); } class Tool implements Serializable{ }
At this time, if the Person class is serialized, the Tool class will be serialized at the same time. If the Tool class does not implement the Serializable interface, an exception will be thrown.
Protect sensitive data:
After a class is added with a serialization identifier, all attribute information of the object of this class will be serialized and then stored locally or transmitted over the network. Then sometimes some fields in the object are sensitive information and should not be exposed. If it is also serialized, it can be easily cracked, thus causing security risks, such as common password fields.
Java provides a keyword transient, which is the transient keyword. This keyword turns off the serialization of the field so that protected information is not exposed through serialization.
Serialization Identification ID
Imagine this scenario: Both ends are transmitting serialized objects over the network. For some reason, the versions of the classes used by both ends are different. Assume that the receiver's classes have been deleted. field. When the sender sends the serialized byte stream of the object to the receiver, it cannot be parsed because the receiver's class is missing several fields.
Java requires that classes that implement the serialization interface must declare a serialVersionUID static attribute. If there is no such attribute, the JVM will automatically declare the attribute and assign a value to the attribute (different values will be assigned when the class changes). The value of this attribute is unique and is used to identify different serialization classes. Only if the serialization identifier of the class is exactly the same, Java will perform deserialization work. This is the role of the serialization identifier.
For the aforementioned scenario, assuming that the serialVersionUID is not declared manually, the JVM assigns different values to the serialVersionUID in the classes used by the sender and the receiver, and the deserialization fails. When serialVersionUID is manually assigned, deserialization can be successful even if the fields of the class change.
Custom serialization strategy
Custom serialization strategy
Java provides a set of effective mechanisms that allow customized methods to be used for corresponding processing during serialization and deserialization. After the transmission parties agree on the serialization strategy, they only need to add a set of methods to the serialization class that needs to be transmitted to implement this set of strategies. During serialization, these specified methods will be automatically called for serialization and deserialization. The method is as follows:
1) private void writeObject(ObjectOutputSteam out) throws IOException
在方法的内部有重要的代码:out.defaultWriteObject() //将对象数据以默认方式写入到输出流中
2)private void readObject(ObjectInputStream in) throws IOException,ClassNotFoundException
同样的,此方法内部也有相似代码:in.defaultReadObject(); //以默认方式从输入流中恢复对象
这两个方法的作用分别是将特定的对象写入到输出流中以及从输入流中恢复特定的对象,通过这两个方法,用户即可实现自定义的序列化。当在实现Serializable接口的类中写了上面两个方法之后,序列化或反序列化该类时则会通过反射来调用这两个方法,从而实现自定义序列化。
限制序列化对象的数量
我们看下面的单例模式:
public class Singleton implements Serializable { private volatile static Singleton mInstance; private Singleton() { } public static Singleton getInstance() { if (mInstance == null) { synchronized (Singleton.class) { if (mInstance == null) { mInstance = new Singleton(); } } } return mInstance; } }
此时通过反序列化获取实例,则单例模式会失效。那该如何解决这个问题呢?
Java有一种机制,可以让我们在序列化和反序列化时,可以根据自己的需要,写入或读取指定的实例。使用这种机制,需要在实现Serializable接口的类中添加两个方法:
private Object readResolve() //如果用户在序列化类中添加了该方法,则在进行反序列化时,使用该方法返回的对象,作为反序列化对象。
private Object writeReplace() //如果用户在序列化类中添加了该方法,则在进行序列化时,序列化该类返回的对象。
再看使用了该机制的单例模式:
public class Singleton implements Serializable { private volatile static Singleton mInstance; private Singleton() { } public static Singleton getInstance() { if (mInstance == null) { synchronized (Singleton.class) { if (mInstance == null) { mInstance = new Singleton(); } } } return mInstance; } private Object readResolve() { return getInstance(); } private Object writeReplace() { return getInstance(); } }
此时的通过反序列化得到的对象也是同一个,即单例模式依然有效!
相关文章:
Java序列化Serializable和Externalizable区别的示例代码

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
