Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
High performance Hessian Serialization and Derialization
package com.alibaba.mq.internal.tools;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import com.caucho.hessian.io.SerializerFactory;
public class HessianUtil {
private static final SerializerFactory serializerFactory = new SerializerFactory();
public static Serializable deserialize(byte[] array) throws IOException {
Object obj = null;
ByteArrayInputStream bais = new ByteArrayInputStream(array);
Hessian2Input hi = new Hessian2Input(bais);
hi.setSerializerFactory(serializerFactory);
hi.setCloseStreamOnClose(true);
hi.startMessage();
obj = hi.readObject();
hi.completeMessage();
hi.close();
return (Serializable) obj;
}
public static byte[] serialize(Object data) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Hessian2Output ho = new Hessian2Output(baos);
ho.setSerializerFactory(serializerFactory);
ho.setCloseStreamOnClose(true);
ho.startMessage();
ho.writeObject(data);
ho.completeMessage();
ho.close();
return baos.toByteArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.