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);
}
}
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);
}
}
}
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);
}
}
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());
}
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"));
}
Aggregations