Search in sources :

Example 1 with CustomHessianSerializer

use of com.alipay.sofa.rpc.codec.sofahessian.serialize.CustomHessianSerializer in project sofa-rpc by sofastack.

the class SofaHessianSerializer method decode.

@Override
public Object decode(AbstractByteBuf data, Class clazz, Map<String, String> context) throws SofaRpcException {
    if (clazz == null) {
        throw buildDeserializeError("class is null!");
    } else {
        CustomHessianSerializer serializer = CustomHessianSerializerManager.getSerializer(clazz);
        if (serializer != null) {
            return serializer.decodeObject(data, context);
        } else {
            try {
                UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream(data.array());
                Hessian2Input input = new Hessian2Input(inputStream);
                input.setSerializerFactory(serializerFactory);
                Object object = input.readObject();
                input.close();
                return object;
            } catch (IOException e) {
                throw buildDeserializeError(e.getMessage(), e);
            }
        }
    }
}
Also used : Hessian2Input(com.caucho.hessian.io.Hessian2Input) UnsafeByteArrayInputStream(com.alipay.sofa.rpc.common.struct.UnsafeByteArrayInputStream) IOException(java.io.IOException) CustomHessianSerializer(com.alipay.sofa.rpc.codec.sofahessian.serialize.CustomHessianSerializer)

Example 2 with CustomHessianSerializer

use of com.alipay.sofa.rpc.codec.sofahessian.serialize.CustomHessianSerializer in project sofa-rpc by sofastack.

the class SofaHessianSerializer method encode.

@Override
public AbstractByteBuf encode(Object object, Map<String, String> context) {
    CustomHessianSerializer serializer = getCustomSerializer(object);
    if (serializer != null) {
        return serializer.encodeObject(object, context);
    } else {
        UnsafeByteArrayOutputStream byteArray = new UnsafeByteArrayOutputStream();
        Hessian2Output output = new Hessian2Output(byteArray);
        try {
            output.setSerializerFactory(serializerFactory);
            output.writeObject(object);
            output.close();
            return new ByteStreamWrapperByteBuf(byteArray);
        } catch (Exception e) {
            throw buildSerializeError(e.getMessage(), e);
        }
    }
}
Also used : Hessian2Output(com.caucho.hessian.io.Hessian2Output) ByteStreamWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteStreamWrapperByteBuf) UnsafeByteArrayOutputStream(com.alipay.sofa.rpc.common.struct.UnsafeByteArrayOutputStream) CustomHessianSerializer(com.alipay.sofa.rpc.codec.sofahessian.serialize.CustomHessianSerializer) IOException(java.io.IOException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Aggregations

CustomHessianSerializer (com.alipay.sofa.rpc.codec.sofahessian.serialize.CustomHessianSerializer)2 IOException (java.io.IOException)2 UnsafeByteArrayInputStream (com.alipay.sofa.rpc.common.struct.UnsafeByteArrayInputStream)1 UnsafeByteArrayOutputStream (com.alipay.sofa.rpc.common.struct.UnsafeByteArrayOutputStream)1 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)1 ByteStreamWrapperByteBuf (com.alipay.sofa.rpc.transport.ByteStreamWrapperByteBuf)1 Hessian2Input (com.caucho.hessian.io.Hessian2Input)1 Hessian2Output (com.caucho.hessian.io.Hessian2Output)1