How Java uses Kryo serialization and deserialization
Kryo is an efficient Java object serialization framework that can replace Java's native serialization methods. Compared to native serialization, Kryo has faster serialization speed, smaller serialized data volume, and supports more data types and custom data structures.
Kryo provides the following commonly used methods:
1. Create Kryo objects: You can use the new keyword or use KryoFactory to create Kryo objects.
Kryo kryo = new Kryo();
2. Register the class to be serialized: Before serialization and deserialization, the class to be serialized needs to be registered first.
kryo.register(MyClass.class);
3. Serialization method: Use an Output object to serialize Java objects into byte arrays.
Output output = new Output(new FileOutputStream("file.bin"));
kryo.writeObject(output, object);
output.close();
4. Deserialization method: Use an Input object to deserialize a byte array into a Java object.
Input input = new Input(new FileInputStream("file.bin"));
MyClass object = kryo.readObject(input, MyClass.class);
input.close();
5. Register custom serializers and deserializers: For certain special data types, Kryo may not be able to provide default serialization and deserialization support. In this case, you can register custom serializers and deserializers.
kryo.register(CustomSerializer.class, new CustomSerializer());
When using Kryo, it is necessary to introduce Kryo's dependencies in the project. The following Maven dependencies can be used to introduce Kryo:
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
<version>5.0.0</version>
</dependency>
The following is a complete example that demonstrates how to use Kryo for serialization and deserialization:
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import java.io.*;
public class KryoSerializationExample {
public static void main(String[] args) throws IOException {
Kryo kryo = new Kryo();
kryo.register(MyClass.class);
//Serializing Objects
MyClass object = new MyClass("Hello World!", 123);
Output output = new Output(new FileOutputStream("file.bin"));
kryo.writeObject(output, object);
output.close();
//Deserialize objects
Input input = new Input(new FileInputStream("file.bin"));
MyClass deserializedObject = kryo.readObject(input, MyClass.class);
input.close();
System.out.println(deserializedObject);
}
}
class MyClass implements Serializable {
private String message;
private int value;
public MyClass(String message, int value) {
this.message = message;
this.value = value;
}
@Override
public String toString() {
return "MyClass{" +
"message='" + message + '\'' +
", value=" + value +
'}';
}
}
The above code first serializes the 'MyClass' object into a byte array through Kryo and stores it in the' file. bin 'file. Then read the byte array from the 'file. bin' file and deserialize it into a 'MyClass' object using Kryo. Finally, print the deserialized object content.