1.Hessian
import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class HessianSerializer implements Serializer{
public static final Serializer INSTANCE=new HessianSerializer();
@Override
public void serualize(Object object, OutputStream outputStream) throws IOException {
Hessian2Output ho = new Hessian2Output(outputStream);
try {
ho.writeObject(object);
ho.flush();
} catch (IOException e) {
throw e;
} finally {
try {
ho.close();
} catch (IOException e) {
}
}
}
@Override
public Object deserialize(InputStream inputStream, Class clazz) throws IOException {
Hessian2Input hi = new Hessian2Input(inputStream);
try {
Object result = hi.readObject(clazz);
return result;
} catch (IOException e) {
throw e;
}finally {
try {
hi.close();
} catch (Exception e) {
}
}
}
}
2.Jackson
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class JacksonSerializer implements Serializer{
public static final Serializer INSTANCE=new JacksonSerializer();
private final static ObjectMapper objectMapper = new ObjectMapper();
@Override
public void serualize(Object object, OutputStream outputStream) throws IOException {
objectMapper.writeValue(outputStream, object);
}
@Override
public Object deserialize(InputStream inputStream, Class clazz) throws IOException {
return objectMapper.readValue(inputStream,clazz);
}
}
3.Kryo
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class KryoSerializer implements Serializer{
public static final Serializer INSTANCE=new KryoSerializer();
private final ThreadLocal<Kryo> kryoLocal = new ThreadLocal<Kryo>() {
@Override
protected Kryo initialValue() {
Kryo kryo = new Kryo();
kryo.setReferences(true); //支持对象循环引用(否则会栈溢出)
kryo.setRegistrationRequired(false); //不强制要求注册类(注册行为无法保证多个 JVM 内同一个类的注册编号相同;而且业务系统中大量的 Class 也难以一一注册)
return kryo;
}
};
@Override
public void serualize(Object object, OutputStream outputStream) throws IOException {
Output output = new Output(outputStream);
try {
kryoLocal.get().writeObject(output, object);
output.flush();
} catch (Exception e) {
throw e;
} finally {
try {
output.close();
} catch (Exception e) {
}
}
}
@Override
public Object deserialize(InputStream inputStream, Class clazz) throws IOException {
Input input = new Input(inputStream);
try {
Object result = kryoLocal.get().readObject(input, clazz);
return result;
} catch (Exception e) {
throw e;
} finally {
try {
input.close();
} catch (Exception e) {
}
}
}
}
4.ProtostuffSerizlizer
import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.Schema;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
import org.objenesis.Objenesis;
import org.objenesis.ObjenesisStd;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class ProtostuffSerizlizer implements Serializer {
private static Objenesis objenesis = new ObjenesisStd(true);
public static final Serializer INSTANCE=new ProtostuffSerizlizer();
private static Map<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<Class<?>, Schema<?>>();
private static <T> Schema<T> getSchema(Class<T> cls) {
@SuppressWarnings("unchecked")
Schema<T> schema = (Schema<T>) cachedSchema.get(cls);
if (schema == null) {
schema = RuntimeSchema.createFrom(cls);
if (schema != null) {
cachedSchema.put(cls, schema);
}
}
return schema;
}
@Override
public void serualize(Object object, OutputStream outputStream) throws IOException {
Class clazz = (Class) object.getClass();
LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
try {
Schema schema = getSchema(clazz);
ProtostuffIOUtil.writeTo(outputStream, object, schema, buffer);
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
} finally {
buffer.clear();
}
}
@Override
public Object deserialize(InputStream inputStream,Class clazz) throws IOException {
try {
Object message = (Object) objenesis.newInstance(clazz);
Schema<Object> schema =getSchema(clazz);
ProtostuffIOUtil.mergeFrom(inputStream, message, schema);
return message;
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
}