Search in sources :

Example 1 with ByteStreamWrapperByteBuf

use of com.alipay.sofa.rpc.transport.ByteStreamWrapperByteBuf in project sofa-rpc by sofastack.

the class SofaRequestHessianSerializer method encodeObject.

@Override
public AbstractByteBuf encodeObject(SofaRequest sofaRequest, Map<String, String> context) {
    try {
        UnsafeByteArrayOutputStream outputStream = new UnsafeByteArrayOutputStream();
        Hessian2Output output = new Hessian2Output(outputStream);
        // 根据SerializeType信息决定序列化器
        boolean genericSerialize = context != null && isGenericRequest(context.get(RemotingConstants.HEAD_GENERIC_TYPE));
        if (genericSerialize) {
            output.setSerializerFactory(genericSerializerFactory);
        } else {
            output.setSerializerFactory(serializerFactory);
        }
        output.writeObject(sofaRequest);
        final Object[] args = sofaRequest.getMethodArgs();
        if (args != null) {
            for (Object arg : args) {
                output.writeObject(arg);
            }
        }
        output.close();
        return new ByteStreamWrapperByteBuf(outputStream);
    } catch (IOException 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) IOException(java.io.IOException)

Example 2 with ByteStreamWrapperByteBuf

use of com.alipay.sofa.rpc.transport.ByteStreamWrapperByteBuf 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)

Example 3 with ByteStreamWrapperByteBuf

use of com.alipay.sofa.rpc.transport.ByteStreamWrapperByteBuf in project sofa-rpc by sofastack.

the class SofaResponseHessianSerializer method encodeObject.

@Override
public AbstractByteBuf encodeObject(SofaResponse sofaResponse, Map<String, String> context) {
    try {
        UnsafeByteArrayOutputStream byteArray = new UnsafeByteArrayOutputStream();
        Hessian2Output output = new Hessian2Output(byteArray);
        output.setSerializerFactory(serializerFactory);
        output.writeObject(sofaResponse);
        output.close();
        return new ByteStreamWrapperByteBuf(byteArray);
    } catch (IOException 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) IOException(java.io.IOException)

Aggregations

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