Last active
October 13, 2022 13:54
-
-
Save sudarshan-suresh/318073614ce38c09b3c7b40bb2916a81 to your computer and use it in GitHub Desktop.
An introduction to Kryo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* A simple Employee class */ | |
public class Employee { | |
private String name; | |
private Date birthDate; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*This is a cusotm EmployeeSerializer class which will be used to writed to a file and read it back*/ | |
public class EmployeeSerializer extends Serializer<Employee> { | |
@Override | |
public void write(Kryo kryo, Output output, Employee employee) { | |
output.writeString(employee.getName()); | |
output.writeLong(employee.getBirthDate().getTime()); | |
} | |
@Override | |
public Employee read(Kryo kryo, Input input, Class<? extends Employee> aClass) { | |
// we need default constructor | |
Employee employee = new Employee(); | |
employee.setName(input.readString()); | |
employee.setBirthDate(new Date(input.readLong())); | |
return employee; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import com.esotericsoftware.kryo.Kryo; | |
import com.esotericsoftware.kryo.io.Input; | |
import com.esotericsoftware.kryo.io.Output; | |
import com.esotericsoftware.kryo.serializers.JavaSerializer; | |
import org.junit.Assert; | |
import org.junit.Before; | |
import org.junit.Test; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.util.Date; | |
public class KryoTest { | |
private Kryo kryo; | |
private String fileName = "test_file.dat"; | |
private Output output; | |
private Input input; | |
@Before | |
public void setUp() throws FileNotFoundException { | |
kryo = new Kryo(); | |
output = new Output(new FileOutputStream(fileName)); | |
input = new Input(new FileInputStream(fileName)); | |
} | |
@Test | |
public void testKryoSerializeAndDeserializeString(){ | |
Object obj = "Hello World!!!"; | |
kryo.writeClassAndObject(output, obj); | |
output.close(); | |
Object readObj = kryo.readClassAndObject(input); | |
input.close(); | |
Assert.assertEquals(readObj, obj); | |
} | |
@Test | |
public void testKryoSerializeAndDeserializeMultiObj(){ | |
String string = "Hello World!!!"; | |
int value = 55; | |
Date date = new Date(651943079000L); | |
kryo.writeObject(output, string); | |
kryo.writeObject(output, value); | |
// Date class need to be registered. | |
kryo.register(Date.class); | |
kryo.writeObject(output, date); | |
output.close(); | |
String readString = kryo.readObject(input, String.class); | |
int readValue = kryo.readObject(input, int.class); | |
Date readDate = kryo.readObject(input,Date.class); | |
input.close(); | |
Assert.assertEquals(string, readString); | |
Assert.assertEquals(value, readValue); | |
Assert.assertEquals(date, readDate); | |
} | |
@Test | |
public void testSerializeAndDeserializeEmployee(){ | |
// registering class | |
kryo.register(Employee.class); | |
kryo.register(Date.class); | |
Employee employee = new Employee("Rose", new Date(651943079000L)); | |
kryo.writeObject(output, employee); | |
output.close(); | |
Employee readEmployee = kryo.readObject(input, Employee.class); | |
input.close(); | |
Assert.assertEquals(employee.getName(), readEmployee.getName()); | |
Assert.assertEquals(employee.getBirthDate(), readEmployee.getBirthDate()); | |
} | |
@Test | |
public void testCustomSerializerAndDeserializeEmployee(){ | |
Employee employee = new Employee("Rose", new Date(651943079000L)); | |
kryo.register(Employee.class, new EmployeeSerializer()); | |
kryo.writeObject(output, employee); | |
output.close(); | |
Employee readEmployee = kryo.readObject(input, Employee.class); | |
input.close(); | |
Assert.assertEquals(employee.getName(), readEmployee.getName()); | |
Assert.assertEquals(employee.getBirthDate(), readEmployee.getBirthDate()); | |
} | |
@Test | |
public void testJavaSerializerAndDeserializeEStudent(){ | |
Student student = new Student("Rose", "Geller", new Date(951943079000L), new String[]{"Science", "History"}, "A", "GOVT"); | |
kryo.register(Student.class, new JavaSerializer()); | |
kryo.writeObject(output, student); | |
output.close(); | |
Student readStudent = kryo.readObject(input, Student.class); | |
input.close(); | |
Assert.assertEquals(student.getFirstName(), readStudent.getFirstName()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* In this method we are going write multiple objects to a file and read them back */ | |
@Test | |
public void testKryoSerializeAndDeserializeMultiObj(){ | |
String string = "Hello World!!!"; | |
int value = 55; | |
Date date = new Date(651943079000L); | |
// register Date.class with kryo.register else it will throw an exception | |
kryo.register(Date.class); | |
kryo.writeObject(output, string); | |
kryo.writeObject(output, value); | |
kryo.writeObject(output, date); | |
// close output resource | |
output.close(); | |
String readString = kryo.readObject(input, String.class); | |
int readValue = kryo.readObject(input, int.class); | |
Date readDate = kryo.readObject(input,Date.class); | |
// close the input resource | |
input.close(); | |
// verify the contents | |
Assert.assertEquals(string, readString); | |
Assert.assertEquals(value, readValue); | |
Assert.assertEquals(date, readDate); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependencies> | |
<dependency> | |
<groupId>com.esotericsoftware</groupId> | |
<artifactId>kryo</artifactId> | |
<version>5.3.0</version> | |
</dependency> | |
<dependency> | |
<groupId>org.projectlombok</groupId> | |
<artifactId>lombok</artifactId> | |
<version>1.18.24</version> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.13.1</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*This is a sample on how to implement KryoSerializable methods */ | |
public class Employee implements KryoSerializable { | |
public void write(Kryo kryo, Output output) { | |
output.writeString(name); | |
// ... | |
} | |
public void read(Kryo kryo, Input input) { | |
name = input.readString(); | |
// ... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* This method is executed before each test */ | |
@Before | |
public void setUp() throws FileNotFoundException { | |
kryo = new Kryo(); | |
output = new Output(new FileOutputStream(fileName)); | |
input = new Input(new FileInputStream(fileName)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Student class with more fields */ | |
@AllArgsConstructor | |
@NoArgsConstructor | |
@Getter | |
@Setter | |
public class Student implements Serializable { | |
private String firstName; | |
private String lastName; | |
private Date dateOfBirth; | |
private String[] courses; | |
private String classGroup; | |
private String schoolName; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* In this we are going to write an object to a file and read the same file and verify the objects are same */ | |
@Test | |
public void testKryoSerializeAndDeserializeString(){ | |
Object obj = "Hello World!!!"; | |
kryo.writeClassAndObject(output, obj); | |
output.close(); | |
Object readObj = kryo.readClassAndObject(input); | |
input.close(); | |
Assert.assertEquals(readObj, obj); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* In this test case we are going to serialize and deserialize Employee class with custom serializer we created */ | |
@Test | |
public void testCustomSerializerAndDeserializeEmployee(){ | |
Employee employee = new Employee("Rose", new Date(651943079000L)); | |
// register Employee class and its serialzer with kryo. | |
kryo.register(Employee.class, new EmployeeSerializer()); | |
kryo.writeObject(output, employee); | |
// close output resource | |
output.close(); | |
Employee readEmployee = kryo.readObject(input, Employee.class); | |
// close input resource | |
input.close(); | |
// verify contents | |
Assert.assertEquals(employee.getName(), readEmployee.getName()); | |
Assert.assertEquals(employee.getBirthDate(), readEmployee.getBirthDate()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* In this method we are going to serialze and Deserialize the Employee class. */ | |
@Test | |
public void testSerializeAndDeserializeEmployee(){ | |
// register Employee and Date | |
kryo.register(Employee.class); | |
kryo.register(Date.class); | |
Employee employee = new Employee("Rose", new Date(651943079000L)); | |
kryo.writeObject(output, employee); | |
// close output resource | |
output.close(); | |
Employee readEmployee = kryo.readObject(input, Employee.class); | |
// close input resource | |
input.close(); | |
// verify contents | |
Assert.assertEquals(employee.getName(), readEmployee.getName()); | |
Assert.assertEquals(employee.getBirthDate(), readEmployee.getBirthDate()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*In this testcase we are going to serialze and deserialize student class using JavaSerializer. */ | |
@Test | |
public void testJavaSerializerAndDeserializeEStudent(){ | |
Student student = new Student("Rose", "Geller", new Date(951943079000L), new String[]{"Science", "History"}, "A", "GOVT"); | |
// register student class with kryo. | |
kryo.register(Student.class, new JavaSerializer()); | |
kryo.writeObject(output, student); | |
// close output resource | |
output.close(); | |
Student readStudent = kryo.readObject(input, Student.class); | |
// close input resource | |
input.close(); | |
// verify contents | |
Assert.assertEquals(student.getFirstName(), readStudent.getFirstName()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment