Java Code Examples for com.caucho.hessian.io.Hessian2Input#readObject()

The following examples show how to use com.caucho.hessian.io.Hessian2Input#readObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestArray.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenericArray() throws IOException {
    GenericArray ga = dg.generateGenericArray();

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(bout);
    hout.setSerializerFactory(new GenericSerializerFactory());

    hout.writeObject(ga);

    hout.close();

    byte[] body = bout.toByteArray();
    ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length);
    Hessian2Input hin = new Hessian2Input(bin);
    hin.setSerializerFactory(new SerializerFactory());

    Person[] pArr = (Person[]) hin.readObject();

    assertEquals(ga.getLength(), pArr.length);
    cmpGPersonEqualPerson((GenericObject) ga.get(0), (Person) pArr[0]);
    cmpGPersonEqualPerson((GenericObject) ga.get(1), (Person) pArr[1]);
    cmpGPersonEqualPerson((GenericObject) ga.get(2), (Person) pArr[2]);

}
 
Example 2
Source File: HessianSerializer.java    From Jupiter with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T readObject(InputBuf inputBuf, Class<T> clazz) {
    Hessian2Input input = Inputs.getInput(inputBuf);
    try {
        Object obj = input.readObject(clazz);
        return clazz.cast(obj);
    } catch (IOException e) {
        ThrowUtil.throwException(e);
    } finally {
        try {
            input.close();
        } catch (IOException ignored) {}

        inputBuf.release();
    }
    return null; // never get here
}
 
Example 3
Source File: ArrayInjectTest.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
@Test
public void testHessian2Array() throws IOException {
    SerializerFactory factory = new SerializerFactory();
    String s = "567400075b6f626a6563746e025674001e5b636f6d2e73756e2e726f777365742e4a646263526f77536574496d706c6e014fad786f6d2e73756e2e726f777365742e4a646263526f77536574496d706cac07636f6d6d616e640355524c0a64617461536f757263650a726f77536574547970650b73686f7744656c657465640c717565727954696d656f7574076d6178526f77730c6d61784669656c6453697a650b636f6e63757272656e637908726561644f6e6c791065736361706550726f63657373696e670969736f6c6174696f6e08666574636844697209666574636853697a6504636f6e6e02707302727306726f77734d44057265734d440d694d61746368436f6c756d6e730f7374724d61746368436f6c756d6e730c62696e61727953747265616d0d756e69636f646553747265616d0b617363696953747265616d0a6368617253747265616d036d6170096c697374656e65727306706172616d736f904e4e4ecbec46909090cbf0545492cbe8904e4e4e4e4e567400106a6176612e7574696c2e566563746f726e0a8f8f8f8f8f8f8f8f8f8f7a76929a4e4e4e4e4e4e4e4e4e4e4e4e4e4e4e7692904d7400136a6176612e7574696c2e486173687461626c657a7a4a027a";
    byte[] bs = hex2byte(s.getBytes());
    Assert.assertTrue(s.equalsIgnoreCase(byte2hex(bs)));

    ByteArrayInputStream input = new ByteArrayInputStream(bs, 0, bs.length);
    Hessian2Input hin = new Hessian2Input(input);
    hin.setSerializerFactory(factory);

    try {
        hin.readObject();
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e instanceof IOException);
    }
}
 
Example 4
Source File: MultipleClassLoaderSofaSerializerFactoryTest.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Test
public void testAll() throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output h2out = new Hessian2Output(bout);
    h2out.setSerializerFactory(factory);

    Map map = new HashMap();
    map.put("1", new long[] { 1L, 2L });

    h2out.writeObject(map);

    h2out.flush();
    byte[] body = bout.toByteArray();

    ByteArrayInputStream input = new ByteArrayInputStream(body, 0, body.length);
    Hessian2Input hin = new Hessian2Input(input);

    Map copy = (Map) hin.readObject();

    long[] a1 = (long[]) map.get("1");
    long[] a2 = (long[]) copy.get("1");
    assertEquals(a1.length, a2.length);
    for (int i = 0; i < a1.length; ++i)
        assertEquals(a1[i], a2[i]);
}
 
Example 5
Source File: HessianExample.java    From pragmatic-java-engineer with GNU General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    //序列化
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    Hessian2Output out = new Hessian2Output(os);
    out.startMessage();
    TestUser user = new TestUser();
    out.writeObject(user);
    out.completeMessage();
    out.flush();
    byte[] bytes = os.toByteArray();
    out.close();
    os.close();

    //反序列化
    ByteArrayInputStream ins = new ByteArrayInputStream(bytes);
    Hessian2Input input = new Hessian2Input(ins);
    input.startMessage();
    user = (TestUser) input.readObject();
    input.completeMessage();
    input.close();
    ins.close();
}
 
Example 6
Source File: CollectionTest.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
@Test
public void test2StringArray() throws Exception {

    Object[][] strs = new String[2][3];
    strs[0][0] = "11111";
    strs[1][1] = "22222";

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(bout);
    hout.setSerializerFactory(new GenericSerializerFactory());
    hout.writeObject(strs);
    hout.close();

    byte[] body = bout.toByteArray();
    ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length);
    Hessian2Input hin = new Hessian2Input(bin);
    hin.setSerializerFactory(new GenericSerializerFactory());

    Object o = hin.readObject();
    assertTrue(o.getClass() == String[][].class);
    String[][] s = (String[][]) o;
    assertEquals("11111", s[0][0]);
    assertEquals("22222", s[1][1]);

}
 
Example 7
Source File: SpecialClassTest.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
/**
 * 这个用例说明hessian写入的Iterator,反序列化时是ArrayList, 不兼容
 * @throws Exception
 */
@Test
public void testIterator() throws Exception {

    List<Integer> list = new ArrayList<Integer>();
    list.add(1);
    list.add(2);
    Iterator<Integer> iterable = list.iterator();

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(bout);
    hout.setSerializerFactory(new SerializerFactory());
    hout.writeObject(iterable);
    hout.close();

    byte[] body = bout.toByteArray();
    ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length);
    Hessian2Input hin = new Hessian2Input(bin);
    hin.setSerializerFactory(new SerializerFactory());

    Object o = hin.readObject();
    assertEquals(ArrayList.class, o.getClass());
}
 
Example 8
Source File: ComplexTestGO2GO.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
@Test
public void testMap() throws IOException {
    Object go = dg.generateMapGenericPerson_1();

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(bout);
    hout.setSerializerFactory(new GenericSerializerFactory());

    hout.writeObject(go);

    hout.close();

    byte[] body = bout.toByteArray();
    ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length);
    Hessian2Input hin = new Hessian2Input(bin);
    hin.setSerializerFactory(new GenericSerializerFactory());

    Map dmap = (Map) hin.readObject();
    Map map = (Map) dg.generateMapPerson_1();

    assertEquals(dmap.size(), map.size());
    cmpGPersonEqualPerson((GenericObject) dmap.get(1), (Person) map.get(1));
    cmpGPersonEqualPerson((GenericObject) dmap.get(2), (Person) map.get(2));

    Map dmap2 = (Map) dmap.get("map");
    Map map2 = (Map) map.get("map");
    assertEquals(dmap2.size(), map2.size());
    cmpGPersonEqualPerson((GenericObject) dmap2.get("lll"), (Person) map2.get("lll"));
    cmpGPersonEqualPerson((GenericObject) dmap2.get("qqq"), (Person) map2.get("qqq"));
    cmpGPersonEqualPerson((GenericObject) dmap2.get("www"), (Person) map2.get("www"));
    assertSame(dmap2.get("lll"), dmap2.get("www"));
}
 
Example 9
Source File: HessianSerializer.java    From voyage with Apache License 2.0 5 votes vote down vote up
@Override
public RpcResponse decodeResponse(InputStream inputStream)
		throws IOException {
	  Hessian2Input in = new Hessian2Input(inputStream);  
	  RpcResponse obj = (RpcResponse)in.readObject();  
	  return obj;
}
 
Example 10
Source File: CollectionTest.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenericCollection() throws Exception {

    TestList testList = new TestList();
    testList.add(1);
    testList.add(new SimplePerson("wang", "coder"));

    GenericCollection collection = new GenericCollection(TestList.class.getName());
    collection.setCollection(testList);

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(bout);
    hout.setSerializerFactory(new GenericSerializerFactory());
    hout.writeObject(collection);
    hout.close();

    byte[] body = bout.toByteArray();
    ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length);
    Hessian2Input hin = new Hessian2Input(bin);
    hin.setSerializerFactory(new GenericSerializerFactory());

    Object o = hin.readObject();
    assertTrue(o.getClass() == GenericCollection.class);
    GenericCollection genericCollection = (GenericCollection) o;
    assertEquals(genericCollection.getType(), TestList.class.getName());
    assertEquals(genericCollection.getCollection().size(), 2);
    assertEquals(((ArrayList) genericCollection.getCollection()).get(0), 1);
    assertEquals(((ArrayList) genericCollection.getCollection()).get(1).getClass(),
        GenericObject.class);

    GenericObject genericObject = (GenericObject) ((ArrayList) genericCollection
        .getCollection()).get(1);
    assertEquals(genericObject.getType(), SimplePerson.class.getName());
    assertEquals(genericObject.getField("name"), "wang");
    assertEquals(genericObject.getField("job"), "coder");
}
 
Example 11
Source File: ComplexTestGO2GO.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
public void testArray() throws IOException {
    Object go = dg.generateArrayGenericPerson_1();

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(bout);
    hout.setSerializerFactory(new GenericSerializerFactory());

    hout.writeObject(go);

    hout.close();

    byte[] body = bout.toByteArray();
    ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length);
    Hessian2Input hin = new Hessian2Input(bin);
    hin.setSerializerFactory(new SerializerFactory());

    Object[] darr = (Object[]) hin.readObject();
    Object[] arr = (Object[]) dg.generateArrayPerson_1();

    assertEquals(darr.length, arr.length);
    cmpGPersonEqualPerson((GenericObject) darr[0], (Person) arr[0]);
    cmpGPersonEqualPerson((GenericObject) darr[1], (Person) arr[1]);

    Object[] darr2 = (Object[]) darr[2];
    Object[] arr2 = (Object[]) arr[2];
    assertEquals(darr2.length, arr2.length);
    cmpGPersonEqualPerson((GenericObject) darr2[0], (Person) arr2[0]);
    cmpGPersonEqualPerson((GenericObject) darr2[1], (Person) arr2[1]);
    cmpGPersonEqualPerson((GenericObject) darr2[2], (Person) arr2[2]);
    assertSame(darr2[0], darr2[2]);

}
 
Example 12
Source File: LongArrayTest.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
@Test
public void threeArray() throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output h2out = new Hessian2Output(bout);

    Map map = new HashMap();
    map.put("1", new long[] { 1L, 2L });
    map.put("2", new long[] { 3L, 4L, 5L });
    map.put("3", new long[] { 3L, 4L, 5L, 6L });

    h2out.writeObject(map);

    h2out.flush();
    byte[] body = bout.toByteArray();

    ByteArrayInputStream input = new ByteArrayInputStream(body, 0, body.length);
    Hessian2Input hin = new Hessian2Input(input);

    Map copy = (Map) hin.readObject();

    long[] a1 = (long[]) map.get("1");
    long[] a2 = (long[]) copy.get("1");
    assertEquals(a1.length, a2.length);
    for (int i = 0; i < a1.length; ++i)
        assertEquals(a1[i], a2[i]);

    a1 = (long[]) map.get("2");
    a2 = (long[]) copy.get("2");
    assertEquals(a1.length, a2.length);
    for (int i = 0; i < a1.length; ++i)
        assertEquals(a1[i], a2[i]);

    a1 = (long[]) map.get("3");
    a2 = (long[]) copy.get("3");
    assertEquals(a1.length, a2.length);
    for (int i = 0; i < a1.length; ++i)
        assertEquals(a1[i], a2[i]);
}
 
Example 13
Source File: LongArrayTest.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
@Test
public void twoArray() throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output h2out = new Hessian2Output(bout);

    Map map = new HashMap();
    map.put("1", new long[] { 1L, 2L });
    map.put("2", new long[] { 3L, 4L });

    h2out.writeObject(map);

    h2out.flush();
    byte[] body = bout.toByteArray();

    ByteArrayInputStream input = new ByteArrayInputStream(body, 0, body.length);
    Hessian2Input hin = new Hessian2Input(input);

    Map copy = (Map) hin.readObject();

    long[] a1 = (long[]) map.get("1");
    long[] a2 = (long[]) copy.get("1");
    assertEquals(a1.length, a2.length);
    for (int i = 0; i < a1.length; ++i)
        assertEquals(a1[i], a2[i]);

    a1 = (long[]) map.get("2");
    a2 = (long[]) copy.get("2");
    assertEquals(a1.length, a2.length);
    for (int i = 0; i < a1.length; ++i)
        assertEquals(a1[i], a2[i]);
}
 
Example 14
Source File: SofaHessianSerializeService.java    From spring-boot-starter-grpc with MIT License 5 votes vote down vote up
@Override
public GrpcResponse deserialize(GrpcService.Response response) {
    byte[] bytes = response.getResponse().toByteArray();
    UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream(bytes);
    try {
        Hessian2Input input = new Hessian2Input(inputStream);
        input.setSerializerFactory(serializerFactory);
        GrpcResponse grpcResponse = (GrpcResponse) input.readObject();
        input.close();
        return grpcResponse;
    } catch (IOException e) {
        throw new GrpcException("sofa-hessian deserialize fail: " + e.getMessage());
    }
}
 
Example 15
Source File: TestPerformance.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
@Test
public void testHessian2Deserialize() throws IOException {
    Object person = dg.generateMixObject();

    ByteArrayOutputStream bout = new ByteArrayOutputStream();

    Hessian2Output hout = new Hessian2Output(bout);
    hout.setSerializerFactory(new SerializerFactory());
    hout.writeObject(person);
    hout.close();

    byte[] body = bout.toByteArray();

    long startTime = System.currentTimeMillis();
    for (int i = 0; i < TEST_TIMES; ++i) {
        ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length);
        Hessian2Input hin = new Hessian2Input(bin);
        hin.setSerializerFactory(new SerializerFactory());

        hin.readObject();

        hin.close();
        bin.close();
    }
    long endTime = System.currentTimeMillis();

    System.out.println("time of hessian2 deserialization: " + (endTime - startTime) / 1000);

}
 
Example 16
Source File: SimpleTestO2GO.java    From sofa-hessian with Apache License 2.0 4 votes vote down vote up
@org.junit.Test
public void testObject() throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(bout);
    SerializerFactory factory = new SerializerFactory();
    factory.setAllowNonSerializable(true);
    hout.setSerializerFactory(factory);

    Object obj1 = dg.generateObject_0();
    Object obj2 = dg.generateObject_16();
    Object obj3 = dg.generateObject_1();
    Object obj4 = dg.generateObject_2();
    Object obj5 = dg.generateObject_2a();
    Object obj6 = dg.generateObject_2b();
    Object obj7 = dg.generateObject_3();

    hout.writeObject(obj1);
    hout.writeObject(obj2);
    hout.writeObject(obj3);
    hout.writeObject(obj4);
    hout.writeObject(obj5);
    hout.writeObject(obj6);
    hout.writeObject(obj7);

    hout.close();

    byte[] body = bout.toByteArray();
    ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length);
    Hessian2Input hin = new Hessian2Input(bin);
    hin.setSerializerFactory(new GenericSerializerFactory());

    Object dobj1 = hin.readObject();
    Object dobj2 = hin.readObject();
    Object dobj3 = hin.readObject();
    Object dobj4 = hin.readObject();
    Object dobj5 = hin.readObject();
    Object dobj6 = hin.readObject();
    Object dobj7 = hin.readObject();

    assertEquals(obj1.getClass().getName(), ((GenericObject) dobj1).getType());

    List listO2 = (List) obj2;
    List listGO2 = (List) dobj2;
    for (int i = 0; i < listO2.size(); ++i)
        assertEquals(listO2.get(i).getClass().getName(),
            ((GenericObject) listGO2.get(i)).getType());

    assertEquals(obj3.getClass().getName(), ((GenericObject) dobj3).getType());
    assertEquals(((TestObject) obj3).getValue(),
        ((GenericObject) dobj3).getField("_value"));

    List listO4 = (List) obj4;
    List listGO4 = (List) dobj4;
    assertEquals(listO4.get(0).getClass().getName(), ((GenericObject) listGO4.get(0)).getType());
    assertEquals(((TestObject) listO4.get(0)).getValue(),
        ((GenericObject) listGO4.get(0)).getField("_value"));
    assertEquals(listO4.get(1).getClass().getName(), ((GenericObject) listGO4.get(1)).getType());
    assertEquals(((TestObject) listO4.get(1)).getValue(),
        ((GenericObject) listGO4.get(1)).getField("_value"));

    List listO5 = (List) obj5;
    List listGO5 = (List) dobj5;
    assertEquals(listO5.get(0).getClass().getName(), ((GenericObject) listGO5.get(0)).getType());
    assertEquals(((TestObject) listO5.get(0)).getValue(),
        ((GenericObject) listGO5.get(0)).getField("_value"));
    assertSame(listGO5.get(0), listGO5.get(1));

    List listO6 = (List) obj6;
    List listGO6 = (List) dobj6;
    assertEquals(listO6.get(0).getClass().getName(), ((GenericObject) listGO6.get(0)).getType());
    assertEquals(((TestObject) listO6.get(0)).getValue(),
        ((GenericObject) listGO6.get(0)).getField("_value"));
    assertEquals(listO6.get(1).getClass().getName(), ((GenericObject) listGO6.get(1)).getType());
    assertEquals(((TestObject) listO6.get(1)).getValue(),
        ((GenericObject) listGO6.get(1)).getField("_value"));

    GenericObject gobj7 = (GenericObject) dobj7;
    assertEquals(obj7.getClass().getName(), gobj7.getType());
    assertSame(gobj7, gobj7.getField("_rest"));

    hin.close();

}
 
Example 17
Source File: ComplexTestMix.java    From sofa-hessian with Apache License 2.0 4 votes vote down vote up
public void testGOGO2GOO() throws IOException {
    Object obj1 = dg.generateMixGenericObject();
    Object obj2 = dg.generateMixGenericObject();

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(bout);
    hout.setSerializerFactory(new GenericSerializerFactory());

    hout.writeObject(obj1);
    hout.writeObject(obj2);

    hout.close();

    byte[] body = bout.toByteArray();
    ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length);
    Hessian2Input hin = new Hessian2Input(bin);

    hin.setSerializerFactory(new SerializerFactory());
    obj1 = hin.readObject();

    hin.setSerializerFactory(new GenericSerializerFactory());
    obj2 = hin.readObject();

    hin.close();

    Object[][] arr = (Object[][]) obj1;
    Object[][] garr = (Object[][]) obj2;

    assertEquals(garr.length, 3);
    assertEquals(garr[0].length, 3);
    assertEquals(arr.length, 3);
    assertEquals(arr[0].length, 3);

    assertSame(garr, garr[2][0]);
    assertSame(arr, arr[2][0]);
    cmpGPersonEqualPerson((GenericObject) garr[2][1], (Person) arr[2][1]);
    cmpGPersonEqualPerson((GenericObject) garr[2][2], (Person) arr[2][2]);

    Object[] garr1 = (Object[]) garr[1][0];
    Map gmap1 = (Map) garr[1][1];
    List glist1 = (List) garr[1][2];

    Object[] arr1 = (Object[]) arr[1][0];
    Map map1 = (Map) arr[1][1];
    List list1 = (List) arr[1][2];

    //check the circle reference for generic  hessian
    Object[] garr2 = (Object[]) garr1[2];
    assertSame(garr2[2], gmap1);
    Map gmap2 = (Map) gmap1.get(3);
    assertSame(gmap2.get("www"), glist1);
    List glist2 = (List) glist1.get(2);
    assertSame(glist2.get(2), garr1);

    //check the circle reference for hessian
    Object[] arr2 = (Object[]) arr1[2];
    assertSame(arr2[2], map1);
    Map map2 = (Map) map1.get(3);
    assertSame(map2.get("www"), list1);
    List list2 = (List) list1.get(2);
    assertSame(list2.get(2), arr1);

    cmpGPersonEqualPerson((GenericObject) garr1[0], (Person) arr1[0]);
    //check the same object for generic hessian
    assertSame(garr1[0], garr2[0]);
    assertSame(garr1[0], gmap1.get(1));
    assertSame(garr1[0], gmap2.get("lll"));
    assertSame(garr1[0], glist1.get(0));
    assertSame(garr1[0], glist2.get(0));
    //check the same object for hessian
    assertSame(arr1[0], arr2[0]);
    assertSame(arr1[0], map1.get(1));
    assertSame(arr1[0], map2.get("lll"));
    assertSame(arr1[0], list1.get(0));
    assertSame(arr1[0], list2.get(0));

    cmpGPersonEqualPerson((GenericObject) garr1[1], (Person) arr1[1]);
    //check the same object for generic hessian
    assertSame(garr1[1], garr2[1]);
    assertSame(garr1[1], gmap1.get(2));
    assertSame(garr1[1], gmap2.get("qqq"));
    assertSame(garr1[1], glist1.get(1));
    assertSame(garr1[1], glist2.get(1));
    //check the same object for hessian
    assertSame(arr1[1], arr2[1]);
    assertSame(arr1[1], map1.get(2));
    assertSame(arr1[1], map2.get("qqq"));
    assertSame(arr1[1], list1.get(1));
    assertSame(arr1[1], list2.get(1));
}
 
Example 18
Source File: PersonTest.java    From sofa-hessian with Apache License 2.0 4 votes vote down vote up
public void testDiffDefinition() throws Exception {
    GenericObject go1 = new GenericObject(Person.class.getName());
    go1.putField("name", "zhangsan");
    //        go1.putField("age", 12);
    go1.putField("gender", null);
    go1.putField("scores", new HashMap<String, Integer>());
    go1.putField("friend", null);
    go1.putField("pet", GenericUtils.convertToGenericObject(new Pet()));
    //        go1.putField("mapValue", null);
    go1.putField("listValue", new ArrayList<Object>());

    GenericObject go2 = new GenericObject(Person.class.getName());
    go2.putField("name", "lisi");
    go2.putField("age", 21);
    //        go2.putField("gender", "Femal");
    go2.putField("scores", new HashMap<String, Integer>());
    go2.putField("friend", GenericUtils.convertToGenericObject(new Person()));
    //        go2.putField("pet", null);
    go2.putField("mapValue", null);
    go2.putField("listValue", null);

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(bout);
    hout.setSerializerFactory(new GenericSerializerFactory());

    hout.writeObject(go1);
    hout.writeObject(go2);

    hout.close();

    byte[] body = bout.toByteArray();

    ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length);
    Hessian2Input hin = new Hessian2Input(bin);
    hin.setSerializerFactory(new SerializerFactory());

    Person p1 = (Person) hin.readObject();
    assertEquals("zhangsan", p1.getName());
    //        assertEquals(12, p1.getAge());
    assertEquals(null, p1.getGender());
    assertEquals(0, p1.getScores().size());
    assertEquals(null, p1.getFriend());
    assertNotNull(p1.getPet());
    //        assertEquals(null, p1.getMapValue());
    assertEquals(0, p1.getListValue().size());

    Person p2 = (Person) hin.readObject();
    assertEquals("lisi", p2.getName());
    assertEquals(21, p2.getAge());
    //        assertEquals("Femal", p2.getGender());
    assertEquals(0, p2.getScores().size());
    assertNotNull(p2.getFriend());
    //        assertEquals(null, p2.getPet());
    assertEquals(null, p2.getMapValue());
    assertEquals(null, p2.getListValue());
}
 
Example 19
Source File: DubboHessianPipe.java    From Ak47 with Apache License 2.0 4 votes vote down vote up
@Override
public void decodeDubboRequest(DubboData dd, Request<DubboHessianRequest> request) throws Exception {
    
    DubboHessianRequest dubboreq = new DubboHessianRequest();
    DubboHeader dh = dd.getDubboHeader();
    byte flag = dh.getFlag();
    if( (flag & FLAG_EVENT) != 0 ){
           // heart beat
        // do nothing.
        
    }else if( (flag & SERIALIZATION_MASK) == HESSIAN_SERIALIZATION_ID ){
        // decode hessian
        ByteArrayInputStream bis = new ByteArrayInputStream(dd.getBody());
           Hessian2Input h2in = hessianFactory.createHessian2Input(bis);
           
           String dubboVersion = h2in.readString();
           dubboreq.setDubboVersion(dubboVersion);
           
           String service = h2in.readString();
           dubboreq.setService(service);
           
           String version = h2in.readString();
           dubboreq.setVersion(version);
           
           String method = h2in.readString();
           dubboreq.setMethod(method);
           
           // this dont work!
           // Object[] args = h2in.readArguments(); 
           String argsdesc = h2in.readString();
           Class<?>[] argtypes = ReflectUtils.desc2classArray(argsdesc);
           List<Object> args = new ArrayList<Object>(argtypes.length);
           for(int i=0;i<argtypes.length;i++){
               args.add(h2in.readObject(argtypes[i]));
           }
           dubboreq.setArgs(args);
           
           @SuppressWarnings("unchecked")
           Map<String, String> attachments = (Map<String, String>) h2in.readObject(Map.class);
           dubboreq.setAttachments(attachments);
           
           log.debug("decodeDubboRequest dubboVersion:{} service:{} version:{} method:{} args.size:{} attachments:{}",
                   dubboVersion, service, version, method, args.size(), YmlUtil.obj2Yml(attachments));
           
    }else{
        // not hessian serialization
        throw new Ak47RuntimeException("Unsupport serialization type. flag is " + flag );
    }

       dubboreq.setDubboHeader(dh);
       request.pojo(dubboreq);
       
}
 
Example 20
Source File: ComplexTestGO2O.java    From sofa-hessian with Apache License 2.0 4 votes vote down vote up
@Test
public void singlePerson() throws IOException {
    Object gp1 = dg.generateGenericPerson_1();
    Object gp2 = dg.generateGenericPerson_2();
    Object gp3 = dg.generateGenericPerson_3();
    Object gp4 = dg.generateGenericPerson_4();
    Object gp5 = dg.generateGenericPerson_5();
    Object gp6 = dg.generateGenericPerson_6();

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(bout);
    hout.setSerializerFactory(new GenericSerializerFactory());

    hout.writeObject(gp1);
    hout.writeObject(gp2);
    hout.writeObject(gp3);
    hout.writeObject(gp4);
    hout.writeObject(gp5);
    hout.writeObject(gp6);

    hout.close();

    byte[] body = bout.toByteArray();
    ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length);
    Hessian2Input hin = new Hessian2Input(bin);
    hin.setSerializerFactory(new SerializerFactory());

    Person dp1 = (Person) hin.readObject();
    Person dp2 = (Person) hin.readObject();
    Person dp3 = (Person) hin.readObject();
    Person dp4 = (Person) hin.readObject();
    Person dp5 = (Person) hin.readObject();
    Person dp6 = (Person) hin.readObject();

    ComplexTestGO2GO.cmpGPersonEqualPerson((GenericObject) gp1, dp1);
    ComplexTestGO2GO.cmpGPersonEqualPerson((GenericObject) gp2, dp2);
    ComplexTestGO2GO.cmpGPersonEqualPerson((GenericObject) gp3, dp3);
    ComplexTestGO2GO.cmpGPersonEqualPerson((GenericObject) gp4, dp4);
    ComplexTestGO2GO.cmpGPersonEqualPerson((GenericObject) gp5, dp5);
    ComplexTestGO2GO.cmpGPersonEqualPerson((GenericObject) gp6, dp6);

    //System.out.println(dp6);
}