Search in sources :

Example 1 with UnsafeByteArrayOutputStream

use of com.alipay.sofa.rpc.common.struct.UnsafeByteArrayOutputStream 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 UnsafeByteArrayOutputStream

use of com.alipay.sofa.rpc.common.struct.UnsafeByteArrayOutputStream 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 UnsafeByteArrayOutputStream

use of com.alipay.sofa.rpc.common.struct.UnsafeByteArrayOutputStream 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)

Example 4 with UnsafeByteArrayOutputStream

use of com.alipay.sofa.rpc.common.struct.UnsafeByteArrayOutputStream in project sofa-rpc by sofastack.

the class ByteStreamWrapperByteBufTest method array.

@Test
public void array() throws IOException {
    AbstractByteBuf byteBuf = new ByteStreamWrapperByteBuf(null);
    Assert.assertNull(byteBuf.array());
    Assert.assertTrue(byteBuf.readableBytes() == 0);
    UnsafeByteArrayOutputStream bs = new UnsafeByteArrayOutputStream();
    bs.write(new byte[] { 1, 2, 3 });
    byteBuf = new ByteStreamWrapperByteBuf(bs);
    Assert.assertNotNull(byteBuf.array());
    Assert.assertTrue(byteBuf.array().length == 3);
    Assert.assertTrue(byteBuf.readableBytes() == 3);
    Assert.assertTrue(byteBuf.release());
}
Also used : UnsafeByteArrayOutputStream(com.alipay.sofa.rpc.common.struct.UnsafeByteArrayOutputStream) Test(org.junit.Test)

Example 5 with UnsafeByteArrayOutputStream

use of com.alipay.sofa.rpc.common.struct.UnsafeByteArrayOutputStream in project sofa-rpc by sofastack.

the class SimpleMapSerializerTest method testCompatible.

// when read null
@Test
public void testCompatible() throws Exception {
    SimpleMapSerializer simpleMapSerializer = new SimpleMapSerializer();
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("1", "2");
    map.put("", "x");
    map.put("b", null);
    map.put(null, null);
    byte[] bs = simpleMapSerializer.encode(map);
    Map<String, String> readMap = simpleMapSerializer.decode(bs);
    Assert.assertEquals(2, readMap.size());
    UnsafeByteArrayOutputStream out = new UnsafeByteArrayOutputStream(64);
    // key is null
    simpleMapSerializer.writeString(out, null);
    simpleMapSerializer.writeString(out, "value");
    // value is null
    simpleMapSerializer.writeString(out, "value");
    simpleMapSerializer.writeString(out, null);
    // value is null and key is null
    simpleMapSerializer.writeString(out, null);
    simpleMapSerializer.writeString(out, null);
    // value is not null and key is not null
    simpleMapSerializer.writeString(out, "key");
    simpleMapSerializer.writeString(out, "value");
    readMap = simpleMapSerializer.decode(out.toByteArray());
    Assert.assertEquals(1, readMap.size());
    Assert.assertEquals("value", readMap.get("key"));
}
Also used : HashMap(java.util.HashMap) UnsafeByteArrayOutputStream(com.alipay.sofa.rpc.common.struct.UnsafeByteArrayOutputStream) Test(org.junit.Test)

Aggregations

UnsafeByteArrayOutputStream (com.alipay.sofa.rpc.common.struct.UnsafeByteArrayOutputStream)6 IOException (java.io.IOException)4 ByteStreamWrapperByteBuf (com.alipay.sofa.rpc.transport.ByteStreamWrapperByteBuf)3 Hessian2Output (com.caucho.hessian.io.Hessian2Output)3 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 SerializationException (com.alipay.remoting.exception.SerializationException)1 CustomHessianSerializer (com.alipay.sofa.rpc.codec.sofahessian.serialize.CustomHessianSerializer)1 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)1 Map (java.util.Map)1