use of com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream in project dubbo by alibaba.
the class BuilderTest method testOthers.
@Test
@SuppressWarnings("unchecked")
public void testOthers() throws Exception {
UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
StringBuffer buf = new StringBuffer();
for (int i = 0; i < 1024 * 32 + 32; i++) buf.append('A');
Builder<String> sb = Builder.register(String.class);
sb.writeTo(buf.toString(), os);
assertEquals(sb.parseFrom(os.toByteArray()), buf.toString());
os = new UnsafeByteArrayOutputStream();
Builder<HashMap> builder = Builder.register(HashMap.class);
Map services = new HashMap();
HashMap map = new HashMap();
services.put("test.service", "http://127.0.0.1:9010/test.service");
map.put("name", "qianlei");
map.put("password", "123455");
map.put("services", services);
builder.writeTo(map, os);
byte[] b = os.toByteArray();
System.out.println(b.length + ":" + Bytes.bytes2hex(b));
map = builder.parseFrom(b);
assertTrue(map.size() > 0);
assertEquals("http://127.0.0.1:9010/test.service", ((Map) map.get("services")).get("test.service"));
services = new ConcurrentHashMap();
services.put("test.service", "http://127.0.0.1:9010/test.service");
map.put("services", services);
os = new UnsafeByteArrayOutputStream();
builder.writeTo(map, os);
b = os.toByteArray();
System.out.println(b.length + ":" + Bytes.bytes2hex(b));
map = builder.parseFrom(b);
assertTrue(map.size() > 0);
assertEquals("http://127.0.0.1:9010/test.service", ((Map) map.get("services")).get("test.service"));
Node node1 = new Node();
Node node0 = new Node();
node0.value = "0";
node0.next = node1;
node1.value = "1";
node1.prev = node0;
// write.
Builder<Node> nodebuilder = Builder.register(Node.class);
os = new UnsafeByteArrayOutputStream();
nodebuilder.writeTo(node0, os);
b = os.toByteArray();
System.out.println("Node:" + b.length + ":" + Bytes.bytes2hex(b));
// parse
node0 = nodebuilder.parseFrom(b);
assertEquals(node0, node0.prev);
assertEquals(node0, node0.next.prev);
assertEquals(node0.value, "0");
}
use of com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream in project dubbo by alibaba.
the class BuilderTest method testBuilder_MyList.
// FIXME MyList的从ArrayList中继承来的属性size会在decode时设置好,再Add时就不对了!!
@Ignore
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testBuilder_MyList() throws Exception {
Builder<MyList> b1 = Builder.register(MyList.class);
MyList list = new MyList();
list.add(new boolean[] { true, false });
list.add(new int[] { 1, 2, 3, 4, 5 });
list.add("String");
list.add(4);
list.code = 4321;
UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
b1.writeTo(list, os);
byte[] b = os.toByteArray();
System.out.println(b.length + ":" + Bytes.bytes2hex(b));
MyList result = b1.parseFrom(b);
assertEquals(4, result.size());
assertEquals(result.code, 4321);
assertEquals(result.id, "feedback");
}
use of com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream in project dubbo by alibaba.
the class BuilderTest method testGenericBuilder.
@Test
public void testGenericBuilder() throws Exception {
UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
Builder<Object> ob = Builder.register(Object.class);
Object o = new Object();
ob.writeTo(o, os);
byte[] b = os.toByteArray();
os = new UnsafeByteArrayOutputStream();
Bean bean = new Bean();
bean.name = "ql";
bean.type = Type.High;
bean.types = new Type[] { Type.High, Type.High };
ob.writeTo(bean, os);
b = os.toByteArray();
bean = (Bean) ob.parseFrom(b);
assertEquals(bean.i, 123123);
assertEquals(bean.ni, -12344);
assertEquals(bean.d, 12.345);
assertEquals(bean.nd, -12.345);
assertEquals(bean.l, 1281447759383l);
assertEquals(bean.nl, -13445l);
assertEquals(bean.vl, 100l);
assertEquals(bean.type, Type.High);
assertEquals(bean.types.length, 2);
assertEquals(bean.types[0], Type.High);
assertEquals(bean.types[1], Type.High);
assertEquals(bean.list.size(), 3);
assertEquals(bean.list.get(0), 1);
assertEquals(bean.list.get(1), 2);
assertEquals(bean.list.get(2), 1308147);
}
use of com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream in project dubbo by alibaba.
the class BuilderTest method testArrayClassBuilder.
@Test
public void testArrayClassBuilder() throws Exception {
UnsafeByteArrayOutputStream os;
byte[] b;
Builder<Object[]> osb = Builder.register(Object[].class);
os = new UnsafeByteArrayOutputStream();
osb.writeTo(new Object[] { new String[0] }, os);
b = os.toByteArray();
Builder<long[]> lsb = Builder.register(long[].class);
os = new UnsafeByteArrayOutputStream();
lsb.writeTo(new long[] { 1, 121232, -3, 4, -5, 61321432413l }, os);
lsb.writeTo(new long[] { 1, 121232, -3, 4, -5, 61321432413l }, os);
lsb.writeTo(new long[] { 1, 2, 3, 12131314, 123132313135l, -6 }, os);
b = os.toByteArray();
long[] ls = lsb.parseFrom(b);
assertEquals(ls.length, 6);
Builder<byte[]> bsb = Builder.register(byte[].class);
os = new UnsafeByteArrayOutputStream();
bsb.writeTo("i am a string.".getBytes(), os);
b = os.toByteArray();
Builder<int[][]> iisb = Builder.register(int[][].class);
os = new UnsafeByteArrayOutputStream();
iisb.writeTo(new int[][] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10 }, { 122, 123, 444 } }, os);
b = os.toByteArray();
int[][] iis = iisb.parseFrom(b);
assertEquals(iis.length, 4);
Builder<int[][][]> iiisb = Builder.register(int[][][].class);
os = new UnsafeByteArrayOutputStream();
iiisb.writeTo(new int[][][] { { { 1, 2, 3, 4 } }, { { 5, 6, 7, 8 } }, { { 122, 123, 444 } } }, os);
b = os.toByteArray();
int[][][] iii = iiisb.parseFrom(b);
assertEquals(iii.length, 3);
}
use of com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream in project dubbo by alibaba.
the class GenericFilter method invoke.
public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException {
if (inv.getMethodName().equals(Constants.$INVOKE) && inv.getArguments() != null && inv.getArguments().length == 3 && !ProtocolUtils.isGeneric(invoker.getUrl().getParameter(Constants.GENERIC_KEY))) {
String name = ((String) inv.getArguments()[0]).trim();
String[] types = (String[]) inv.getArguments()[1];
Object[] args = (Object[]) inv.getArguments()[2];
try {
Method method = ReflectUtils.findMethodByMethodSignature(invoker.getInterface(), name, types);
Class<?>[] params = method.getParameterTypes();
if (args == null) {
args = new Object[params.length];
}
String generic = inv.getAttachment(Constants.GENERIC_KEY);
if (StringUtils.isEmpty(generic) || ProtocolUtils.isDefaultGenericSerialization(generic)) {
args = PojoUtils.realize(args, params, method.getGenericParameterTypes());
} else if (ProtocolUtils.isJavaGenericSerialization(generic)) {
for (int i = 0; i < args.length; i++) {
if (byte[].class == args[i].getClass()) {
try {
UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream((byte[]) args[i]);
args[i] = ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(Constants.GENERIC_SERIALIZATION_NATIVE_JAVA).deserialize(null, is).readObject();
} catch (Exception e) {
throw new RpcException("Deserialize argument [" + (i + 1) + "] failed.", e);
}
} else {
throw new RpcException(new StringBuilder(32).append("Generic serialization [").append(Constants.GENERIC_SERIALIZATION_NATIVE_JAVA).append("] only support message type ").append(byte[].class).append(" and your message type is ").append(args[i].getClass()).toString());
}
}
} else if (ProtocolUtils.isBeanGenericSerialization(generic)) {
for (int i = 0; i < args.length; i++) {
if (args[i] instanceof JavaBeanDescriptor) {
args[i] = JavaBeanSerializeUtil.deserialize((JavaBeanDescriptor) args[i]);
} else {
throw new RpcException(new StringBuilder(32).append("Generic serialization [").append(Constants.GENERIC_SERIALIZATION_BEAN).append("] only support message type ").append(JavaBeanDescriptor.class.getName()).append(" and your message type is ").append(args[i].getClass().getName()).toString());
}
}
}
Result result = invoker.invoke(new RpcInvocation(method, args, inv.getAttachments()));
if (result.hasException() && !(result.getException() instanceof GenericException)) {
return new RpcResult(new GenericException(result.getException()));
}
if (ProtocolUtils.isJavaGenericSerialization(generic)) {
try {
UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(512);
ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(Constants.GENERIC_SERIALIZATION_NATIVE_JAVA).serialize(null, os).writeObject(result.getValue());
return new RpcResult(os.toByteArray());
} catch (IOException e) {
throw new RpcException("Serialize result failed.", e);
}
} else if (ProtocolUtils.isBeanGenericSerialization(generic)) {
return new RpcResult(JavaBeanSerializeUtil.serialize(result.getValue(), JavaBeanAccessor.METHOD));
} else {
return new RpcResult(PojoUtils.generalize(result.getValue()));
}
} catch (NoSuchMethodException e) {
throw new RpcException(e.getMessage(), e);
} catch (ClassNotFoundException e) {
throw new RpcException(e.getMessage(), e);
}
}
return invoker.invoke(inv);
}
Aggregations