com.caucho.hessian.io.SerializerFactory Java Examples

The following examples show how to use com.caucho.hessian.io.SerializerFactory. 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: 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 #2
Source File: TestPerformance.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
@Test
public void testHessian2Serialize() throws IOException {
    Object person = dg.generateMixObject();

    ByteArrayOutputStream bout = new ByteArrayOutputStream();

    long startTime = System.currentTimeMillis();
    for (int i = 0; i < TEST_TIMES; ++i) {
        bout.reset();
        Hessian2Output hout = new Hessian2Output(bout);
        hout.setSerializerFactory(new SerializerFactory());
        hout.writeObject(person);
        hout.close();
    }
    long endTime = System.currentTimeMillis();

    bout.close();

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

}
 
Example #3
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 #4
Source File: TestHessian2Servlet.java    From jvm-sandbox-repeater with Apache License 2.0 6 votes vote down vote up
/**
 * Invoke the object with the request from the input stream.
 *
 * @param in the Hessian input stream
 * @param out the Hessian output stream
 */
@Override
public void invoke(InputStream is, OutputStream os, String objectId,
                   SerializerFactory serializerFactory)
  throws Exception
{
  CharArrayWriter writer = new CharArrayWriter();

  _threadWriter.set(writer);

  PrintWriter dbg = new PrintWriter(writer);

  HessianDebugInputStream debug = new HessianDebugInputStream(is, dbg);
  debug.startTop2();
  
  super.invoke(debug, os, objectId, serializerFactory);
}
 
Example #5
Source File: SpecialClassTest.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
/**
 * 这个用例说明目前的GenericObject结构是支持Throwable的
 * @throws Exception
 */
@Test
public void testThrowable() throws Exception {
    MyException ex = new MyException("hello exception!");
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(bout);
    hout.setSerializerFactory(new SerializerFactory());
    hout.writeObject(ex);
    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();
    assertEquals(GenericObject.class, o.getClass());
    assertEquals(MyException.class.getName(), ((GenericObject) o).getType());
    MyException myException = GenericUtils.convertToObject(o);
    assertEquals(myException.getMessage(), "hello exception!");
}
 
Example #6
Source File: SpecialClassTest.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
/**
 * 这个用例说明hessian写入的Enumeration,反序列化时时是ArrayList, 不兼容
 * @throws Exception
 */
@Test
public void testEnumeration() throws Exception {

    MyEnumerator myEnumerator = new MyEnumerator(0, 5, new Object[] { 1, 2, 3, 4, 5 });
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(bout);
    hout.setSerializerFactory(new SerializerFactory());
    hout.writeObject(myEnumerator);
    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.getName(), o.getClass().getName());
}
 
Example #7
Source File: SerializerExample.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    // Initial SerializerFactory
    // It is highly recommended to cache this factory for every serialization and deserialization.
    SerializerFactory serializerFactory = new SerializerFactory();

    // Do serializer
    String src = "xxx";
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(bout);
    hout.setSerializerFactory(serializerFactory);
    hout.writeObject(src);
    hout.close();
    byte[] data = bout.toByteArray();

    // Do deserializer
    ByteArrayInputStream bin = new ByteArrayInputStream(data, 0, data.length);
    Hessian2Input hin = new Hessian2Input(bin);
    hin.setSerializerFactory(new SerializerFactory());
    String dst = (String) hin.readObject();
    hin.close();
    System.out.println(dst);
}
 
Example #8
Source File: SimpleTestGO2O.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
@org.junit.Test
public void testNull() throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(bout);
    hout.setSerializerFactory(new GenericSerializerFactory());

    hout.writeObject(dg.generateNull());
    hout.close();

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

    assertNull(hin.readObject());

    hin.close();
}
 
Example #9
Source File: SimpleTestGO2O.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
@org.junit.Test
public void testBoolean() throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(bout);
    hout.setSerializerFactory(new GenericSerializerFactory());

    hout.writeObject(dg.generateTrue());
    hout.writeObject(dg.generateFalse());
    hout.writeObject(dg.generateTrue());
    hout.close();

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

    assertEquals(true, hin.readObject());
    assertEquals(false, hin.readObject());
    assertEquals(true, hin.readObject());

    hin.close();
}
 
Example #10
Source File: ArrayInjectTest.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
@Test
public void testHessian1Array() 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);
    HessianInput hin = new HessianInput(input);
    hin.setSerializerFactory(factory);

    try {
        hin.readObject();
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e instanceof IOException);
    }
}
 
Example #11
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 #12
Source File: SimpleTestGO2O.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
@org.junit.Test
public void testDate() throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(bout);
    hout.setSerializerFactory(new GenericSerializerFactory());

    hout.writeObject(dg.generateDate_0());
    hout.writeObject(dg.generateDate_1());
    hout.writeObject(dg.generateDate_2());

    hout.close();

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

    assertEquals(dg.generateDate_0(), hin.readObject());
    assertEquals(dg.generateDate_1(), hin.readObject());
    assertEquals(dg.generateDate_2(), hin.readObject());

    hin.close();
}
 
Example #13
Source File: SimpleTestO2GO.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
@org.junit.Test
public void testDate() throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(bout);
    hout.setSerializerFactory(new SerializerFactory());

    hout.writeObject(dg.generateDate_0());
    hout.writeObject(dg.generateDate_1());
    hout.writeObject(dg.generateDate_2());

    hout.close();

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

    assertEquals(dg.generateDate_0(), hin.readObject());
    assertEquals(dg.generateDate_1(), hin.readObject());
    assertEquals(dg.generateDate_2(), hin.readObject());

    hin.close();
}
 
Example #14
Source File: TestHessian2Servlet.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
/**
 * Invoke the object with the request from the input stream.
 *
 * @param in the Hessian input stream
 * @param out the Hessian output stream
 */
@Override
public void invoke(InputStream is, OutputStream os, String objectId,
                   SerializerFactory serializerFactory)
    throws Exception
{
    CharArrayWriter writer = new CharArrayWriter();

    _threadWriter.set(writer);

    PrintWriter dbg = new PrintWriter(writer);

    HessianDebugInputStream debug = new HessianDebugInputStream(is, dbg);
    debug.startTop2();

    super.invoke(debug, os, objectId, serializerFactory);
}
 
Example #15
Source File: SimpleTestO2GO.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
@org.junit.Test
public void testNull() throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(bout);
    hout.setSerializerFactory(new SerializerFactory());

    hout.writeObject(dg.generateNull());
    hout.close();

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

    assertNull(hin.readObject());

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

    hout.writeObject(dg.generateTrue());
    hout.writeObject(dg.generateFalse());
    hout.writeObject(dg.generateTrue());
    hout.close();

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

    assertEquals(true, hin.readObject());
    assertEquals(false, hin.readObject());
    assertEquals(true, hin.readObject());

    hin.close();
}
 
Example #17
Source File: SofaHessianSerializer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * Gets serializer factory.
 *
 * @param multipleClassLoader the multiple class loader
 * @param generic             the generic
 * @return the serializer factory
 */
protected SerializerFactory getSerializerFactory(boolean multipleClassLoader, boolean generic) {
    if (generic) {
        return multipleClassLoader ? new GenericMultipleClassLoaderSofaSerializerFactory() :
            new GenericSingleClassLoaderSofaSerializerFactory();
    } else {
        return multipleClassLoader ? new MultipleClassLoaderSofaSerializerFactory() :
            new SingleClassLoaderSofaSerializerFactory();
    }
}
 
Example #18
Source File: SimpleTestO2GO.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
@org.junit.Test
public void testString() throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(bout);
    hout.setSerializerFactory(new SerializerFactory());

    hout.writeObject(dg.generateString_0());
    hout.writeObject(dg.generateString_null());
    hout.writeObject(dg.generateString_1());
    hout.writeObject(dg.generateString_31());
    hout.writeObject(dg.generateString_32());
    hout.writeObject(dg.generateString_1023());
    hout.writeObject(dg.generateString_1024());
    hout.writeObject(dg.generateString_65536());

    hout.close();

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

    assertEquals(dg.generateString_0(), hin.readObject());
    assertEquals(dg.generateString_null(), hin.readObject());
    assertEquals(dg.generateString_1(), hin.readObject());
    assertEquals(dg.generateString_31(), hin.readObject());
    assertEquals(dg.generateString_32(), hin.readObject());
    assertEquals(dg.generateString_1023(), hin.readObject());
    assertEquals(dg.generateString_1024(), hin.readObject());
    assertEquals(dg.generateString_65536(), hin.readObject());

    hin.close();
}
 
Example #19
Source File: ComplexTestO2GO.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
@Test
public void testList() throws IOException {
    List list = (List) dg.generateListPerson_1();

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

    hout.writeObject(list);

    hout.close();

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

    List dlist = (List) hin.readObject();

    assertEquals(dlist.size(), list.size());
    ComplexTestGO2GO.cmpGPersonEqualPerson((GenericObject) dlist.get(0), (Person) list.get(0));
    ComplexTestGO2GO.cmpGPersonEqualPerson((GenericObject) dlist.get(1), (Person) list.get(1));

    List dlist2 = (List) dlist.get(2);
    List list2 = (List) list.get(2);
    assertEquals(dlist2.size(), list2.size());
    ComplexTestGO2GO
        .cmpGPersonEqualPerson((GenericObject) dlist2.get(0), (Person) list2.get(0));
    ComplexTestGO2GO
        .cmpGPersonEqualPerson((GenericObject) dlist2.get(1), (Person) list2.get(1));
    ComplexTestGO2GO
        .cmpGPersonEqualPerson((GenericObject) dlist2.get(2), (Person) list2.get(2));
    assertSame(dlist2.get(0), dlist2.get(2));

}
 
Example #20
Source File: ComplexTestO2GO.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
@Test
public void testMap() throws IOException {
    Map map = (Map) dg.generateMapPerson_1();

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

    hout.writeObject(map);

    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();

    assertEquals(dmap.size(), map.size());
    ComplexTestGO2GO.cmpGPersonEqualPerson((GenericObject) dmap.get(1), (Person) map.get(1));
    ComplexTestGO2GO.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());
    ComplexTestGO2GO.cmpGPersonEqualPerson((GenericObject) dmap2.get("lll"),
        (Person) map2.get("lll"));
    ComplexTestGO2GO.cmpGPersonEqualPerson((GenericObject) dmap2.get("qqq"),
        (Person) map2.get("qqq"));
    ComplexTestGO2GO.cmpGPersonEqualPerson((GenericObject) dmap2.get("www"),
        (Person) map2.get("www"));
    assertSame(dmap2.get("lll"), dmap2.get("www"));
}
 
Example #21
Source File: ComplexTestO2GO.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
public void testArray() throws IOException {
    Object[] arr = (Object[]) dg.generateArrayPerson_1();

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

    hout.writeObject(arr);

    hout.close();

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

    Object[] darr = (Object[]) hin.readObject();

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

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

}
 
Example #22
Source File: Hessian2BlackListTest.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void init() {
    NameBlackListFilter filter = new MockNameBlacklistFilter();
    ClassNameResolver resolver = new ClassNameResolver();
    resolver.addFilter(filter);
    serializerFactory = new SerializerFactory();
    serializerFactory.setClassNameResolver(resolver);
}
 
Example #23
Source File: SimpleTestO2GO.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
@org.junit.Test
public void testMap() throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(bout);
    hout.setSerializerFactory(new SerializerFactory());

    hout.writeObject(dg.generateUntypedMap_0());
    hout.writeObject(dg.generateUntypedMap_1());
    hout.writeObject(dg.generateUntypedMap_2());
    hout.writeObject(dg.generateUntypedMap_3());

    hout.writeObject(dg.generateTypedMap_0());
    hout.writeObject(dg.generateTypedMap_1());
    hout.writeObject(dg.generateTypedMap_2());
    hout.writeObject(dg.generateTypedMap_3());

    hout.close();

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

    assertEquals(dg.generateUntypedMap_0(), hin.readObject());
    assertEquals(dg.generateUntypedMap_1(), hin.readObject());
    assertEquals(dg.generateUntypedMap_2(), hin.readObject());
    assertEquals(dg.generateUntypedMap_3(), hin.readObject());

    assertEquals(dg.generateTypedMap_0(), hin.readObject());
    assertEquals(dg.generateTypedMap_1(), hin.readObject());
    assertEquals(dg.generateTypedMap_2(), hin.readObject());
    assertEquals(dg.generateTypedMap_3(), hin.readObject());

    hin.close();
}
 
Example #24
Source File: SimpleTestO2GO.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
@org.junit.Test
public void testList() throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(bout);
    hout.setSerializerFactory(new SerializerFactory());

    hout.writeObject(dg.generateUntypedFixedList_0());
    hout.writeObject(dg.generateUntypedFixedList_1());
    hout.writeObject(dg.generateUntypedFixedList_7());
    hout.writeObject(dg.generateUntypedFixedList_8());

    hout.writeObject(dg.generateTypedFixedList_0());
    hout.writeObject(dg.generateTypedFixedList_1());
    hout.writeObject(dg.generateTypedFixedList_7());
    hout.writeObject(dg.generateTypedFixedList_8());

    hout.close();

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

    assertEquals(dg.generateUntypedFixedList_0(), hin.readObject());
    assertEquals(dg.generateUntypedFixedList_1(), hin.readObject());
    assertEquals(dg.generateUntypedFixedList_7(), hin.readObject());
    assertEquals(dg.generateUntypedFixedList_8(), hin.readObject());

    assertTrue(Arrays.equals((Object[]) dg.generateTypedFixedList_0(),
        (Object[]) hin.readObject()));
    assertTrue(Arrays.equals((Object[]) dg.generateTypedFixedList_1(),
        (Object[]) hin.readObject()));
    assertTrue(Arrays.equals((Object[]) dg.generateTypedFixedList_7(),
        (Object[]) hin.readObject()));
    assertTrue(Arrays.equals((Object[]) dg.generateTypedFixedList_8(),
        (Object[]) hin.readObject()));

    hin.close();
}
 
Example #25
Source File: SimpleTestO2GO.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
@org.junit.Test
public void testBinary() throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(bout);
    hout.setSerializerFactory(new SerializerFactory());

    hout.writeObject(dg.generateBinary_0());
    hout.writeObject(dg.generateBinary_null());
    hout.writeObject(dg.generateBinary_1());
    hout.writeObject(dg.generateBinary_15());
    hout.writeObject(dg.generateBinary_16());
    hout.writeObject(dg.generateBinary_1023());
    hout.writeObject(dg.generateBinary_1024());
    hout.writeObject(dg.generateBinary_65536());

    hout.close();

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

    assertTrue(compareByteArray(dg.generateBinary_0(), hin.readObject()));
    assertNull(hin.readObject());
    assertTrue(compareByteArray(dg.generateBinary_1(), hin.readObject()));
    assertTrue(compareByteArray(dg.generateBinary_15(), hin.readObject()));
    assertTrue(compareByteArray(dg.generateBinary_16(), hin.readObject()));
    assertTrue(compareByteArray(dg.generateBinary_1023(), hin.readObject()));
    assertTrue(compareByteArray(dg.generateBinary_1024(), hin.readObject()));
    assertTrue(compareByteArray(dg.generateBinary_65536(), hin.readObject()));

    hin.close();
}
 
Example #26
Source File: HessianSerializerUtils.java    From zkdoctor with Apache License 2.0 5 votes vote down vote up
/**
 * 反序列化
 *
 * @param bytes 待反序列化字节数组
 * @return
 */
public static Object deserialize(byte[] bytes) {
    ByteArrayInputStream ips = new ByteArrayInputStream(bytes);
    AbstractHessianInput in = new Hessian2Input(ips);
    in.setSerializerFactory(new SerializerFactory());
    Object value;
    try {
        value = in.readObject();
        in.close();
    } catch (IOException e) {
        LOGGER.error("Hessian deserialize failed", e);
        throw new RuntimeException("Hessian deserialize failed");
    }
    return value;
}
 
Example #27
Source File: ComplexTestGO2O.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
@Test
public void testList() throws IOException {
    Object go = dg.generateListGenericPerson_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());

    List dlist = (List) hin.readObject();

    assertEquals(dlist.size(), ((List) go).size());
    ComplexTestGO2GO.cmpGPersonEqualPerson((GenericObject) ((List) go).get(0),
        (Person) dlist.get(0));
    ComplexTestGO2GO.cmpGPersonEqualPerson((GenericObject) ((List) go).get(1),
        (Person) dlist.get(1));

    List dlist2 = (List) dlist.get(2);
    List list2 = (List) ((List) go).get(2);

    assertEquals(dlist2.size(), list2.size());
    ComplexTestGO2GO
        .cmpGPersonEqualPerson((GenericObject) list2.get(0), (Person) dlist2.get(0));
    ComplexTestGO2GO
        .cmpGPersonEqualPerson((GenericObject) list2.get(1), (Person) dlist2.get(1));
    ComplexTestGO2GO
        .cmpGPersonEqualPerson((GenericObject) list2.get(2), (Person) dlist2.get(2));
    assertSame(dlist2.get(0), dlist2.get(2));

}
 
Example #28
Source File: ComplexTestGO2O.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
@Test
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();

    assertEquals(darr.length, ((Object[]) go).length);
    ComplexTestGO2GO
        .cmpGPersonEqualPerson((GenericObject) ((Object[]) go)[0], (Person) darr[0]);
    ComplexTestGO2GO
        .cmpGPersonEqualPerson((GenericObject) ((Object[]) go)[1], (Person) darr[1]);

    Object[] darr2 = (Object[]) darr[2];
    Object[] arr2 = (Object[]) ((Object[]) go)[2];

    assertEquals(darr2.length, arr2.length);
    ComplexTestGO2GO.cmpGPersonEqualPerson((GenericObject) arr2[0], (Person) darr2[0]);
    ComplexTestGO2GO.cmpGPersonEqualPerson((GenericObject) arr2[1], (Person) darr2[1]);
    ComplexTestGO2GO.cmpGPersonEqualPerson((GenericObject) arr2[2], (Person) darr2[2]);
    assertSame(darr2[0], darr2[2]);

}
 
Example #29
Source File: HessianSerializerUtils.java    From zkdoctor with Apache License 2.0 5 votes vote down vote up
/**
 * 将对象序列化为字节数组
 *
 * @param obj 待序列化对象
 * @return
 */
public static byte[] serialize(Object obj) {
    ByteArrayOutputStream ops = new ByteArrayOutputStream();
    AbstractHessianOutput out = new Hessian2Output(ops);
    out.setSerializerFactory(new SerializerFactory());
    try {
        out.writeObject(obj);
        out.close();
    } catch (IOException e) {
        LOGGER.error("Hessian serialize failed.", e);
        throw new RuntimeException("Hessian serialize failed");
    }
    return ops.toByteArray();
}
 
Example #30
Source File: Hessian1BlackListTest.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void init() {
    NameBlackListFilter filter = new MockNameBlacklistFilter();
    ClassNameResolver resolver = new ClassNameResolver();
    resolver.addFilter(filter);
    serializerFactory = new SerializerFactory();
    serializerFactory.setClassNameResolver(resolver);
}