Skip to content

Instantly share code, notes, and snippets.

@darionyaphet
Last active July 2, 2016 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darionyaphet/531a039d13af03bd5c1aef5cdb128e6c to your computer and use it in GitHub Desktop.
Save darionyaphet/531a039d13af03bd5c1aef5cdb128e6c to your computer and use it in GitHub Desktop.
KryoExample
package org.darion.yaphet.kryo;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
public class Examples {
private static class Person {
private String firstName;
private String lastName;
private int age;
private float height;
private double weight;
public Person() {
super();
}
public Person(String firstName, String lastName, int age, float height, double weight) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.height = height;
this.weight = weight;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}
private static class PersonSerializer extends Serializer<Person> {
@Override
public void write(Kryo kryo, Output output, Person person) {
output.writeString(person.getFirstName());
output.writeString(person.getLastName());
output.writeInt(person.getAge());
output.writeFloat(person.getHeight());
output.writeDouble(person.getWeight());
}
@Override
public Person read(Kryo kryo, Input input, Class<Person> type) {
String firstName = input.readString();
String lastName = input.readString();
int age = input.readInt();
float height = input.readFloat();
double weight = input.readDouble();
return new Person(firstName, lastName, age, height, weight);
}
}
public static void main(String[] args) throws FileNotFoundException {
Kryo kryo = new Kryo();
// 6 + 6 + 4 + 4 + 8 = 28
Person person = new Person("darion", "yaphet", 25, 170, 60);
Output output = new Output(new FileOutputStream("/tmp/person"));
kryo.writeObject(output, person);
output.close();
Input input = new Input(new FileInputStream("/tmp/person"));
Person darionyaphet = kryo.readObject(input, Person.class);
input.close();
System.out.println(darionyaphet.getFirstName());
System.out.println(darionyaphet.getLastName());
System.out.println(darionyaphet.getAge());
System.out.println(darionyaphet.getHeight());
System.out.println(darionyaphet.getWeight());
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.darion.yaphet</groupId>
<artifactId>examples</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>examples</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.esotericsoftware/kryo -->
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo-shaded</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment