public class KryoSerializer implements ObjectSerializer {
@Override
public byte[] serialize(Object obj) throws ObjectSerializerException {
byte[] bytes;
ByteArrayOutputStream outputStream =
new
ByteArrayOutputStream();
try
{
Kryo kryo =
new
Kryo();
Output output =
new
Output(outputStream);
kryo.writeObject(output, obj);
bytes = output.toBytes();
output.flush();
}
catch
(Exception ex) {
throw
new
ObjectSerializerException(
"kryo serialize error"
+ ex.getMessage());
} finally {
try
{
outputStream.flush();
outputStream.close();
}
catch
(IOException e) {
}
}
return
bytes;
}
@Override
public <T> T deSerialize(byte[] param, Class<T> clazz) throws ObjectSerializerException {
T object;
try
(ByteArrayInputStream inputStream =
new
ByteArrayInputStream(param)) {
Kryo kryo =
new
Kryo();
Input input =
new
Input(inputStream);
object = kryo.readObject(input, clazz);
input.close();
}
catch
(Exception e) {
throw
new
ObjectSerializerException(
"kryo deSerialize error"
+ e.getMessage());
}
return
object;
}
@Override
public String getSchemeName() {
return
"kryoSerializer"
;
}
}