blob: 57a4554bb213b9f106db7fd2546f5a077ef31f43 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 1998, 2012 Oracle and/or its affiliates. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
* which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Oracle - initial impl
******************************************************************************/
package example;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import model.Order;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
/**
* Uses Kyro to serialize the object.
* @author James Sutherland
*/
public class KryoSerializer implements Serializer {
Kryo kryo;
public KryoSerializer() {
this.kryo = new Kryo();
}
public Kryo getKryo() {
return kryo;
}
public void setKryo(Kryo kryo) {
this.kryo = kryo;
}
public byte[] serialize(Object object) {
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
//FileOutputStream stream = new FileOutputStream("test");
Output output = new Output(stream);
this.kryo.writeObject(output, object);
//stream.flush();
output.close();
return stream.toByteArray();
} catch (Exception exception) {
throw new RuntimeException(exception);
}
}
public Object deserialize(byte[] bytes) {
try {
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
//FileInputStream stream = new FileInputStream("test");
Input input = new Input(stream);
Object result = this.kryo.readObject(input, Order.class);
input.close();
return result;
} catch (Exception exception) {
throw new RuntimeException(exception);
}
}
public String toString() {
return getClass().getSimpleName();
}
}