Thursday, February 9, 2017

Kryo serialization

≃ 1/4 smaller results and more than 3x faster than standard Java.
Widely used (Spark, ...).
Kryo instances  are not thread safe.

Serialization

Kryo kryo = new Kryo();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Output output = new Output(bos);
byte bytes[]; 
try {
  kryo.writeClassAndObject(output, obj);
  bytes = output.toBytes();
} finally {
  bos.close();
}

Deserialization

Kryo kryo = new Kryo();
Input input = new Input(bytes);
Object obj;
try {
  obj = kryo.readClassAndObject(input);
}
finally {
  input.close();
}

No comments:

Post a Comment