com.caucho.hessian.io.Hessian2Input Java Examples

The following examples show how to use com.caucho.hessian.io.Hessian2Input. 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: 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 #2
Source File: ExceptionConstructorTest.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
@Test
public void npe() throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output h2out = new Hessian2Output(bout);

    h2out.writeObject(new A(new Object()));

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

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

    Object copy = h2in.readObject();

    assertTrue(copy instanceof A);
}
 
Example #3
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 #4
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 #5
Source File: X509Encryption.java    From jvm-sandbox-repeater with Apache License 2.0 6 votes vote down vote up
public Hessian2Input unwrapHeaders(Hessian2Input in)
  throws IOException
{
  if (_privateKey == null)
    throw new IOException("X509Encryption.unwrap requires a private key");
  
  if (_cert == null)
    throw new IOException("X509Encryption.unwrap requires a certificate");
  
  InputStream is = new EncryptInputStream(in);

  Hessian2Input filter = new Hessian2Input(is);
  
  filter.setCloseStreamOnClose(true);
  
  return filter;
}
 
Example #6
Source File: X509Encryption.java    From jvm-sandbox-repeater with Apache License 2.0 6 votes vote down vote up
public void close()
  throws IOException
{
  Hessian2Input in = _in;
  _in = null;

  if (in != null) {
    _cipherIn.close();
    _bodyIn.close();

    int len = in.readInt();

    if (len != 0)
      throw new IOException("Unexpected footer");

    in.completeEnvelope();

    in.close();
  }
}
 
Example #7
Source File: SingleClassLoaderSofaSerializerFactoryTest.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 #8
Source File: LongArrayTest.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
@Test
public void oneArray() throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output h2out = new Hessian2Output(bout);

    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 #9
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 #10
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 #11
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 #12
Source File: SimpleTesGO2GO.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 GenericSerializerFactory());

    assertNull(hin.readObject());

    hin.close();
}
 
Example #13
Source File: SimpleTesGO2GO.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 GenericSerializerFactory());

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

    hin.close();
}
 
Example #14
Source File: Hessian2BlackListTest.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
@Test
public void testListDeserialize() throws IOException {
    byte[] bs = new byte[] { 86, 110, 1, 79, -81, 99, 111, 109, 46, 97, 108, 105, 112, 97, 121, 46, 115,
            116, 99, 46, 98, 108, 46, 84, 101, 115, 116, 66, 108, 97, 99, 107, 66, 101, 97, 110, -111,
            6, 115, 116, 114, 105, 110, 103, 111, -112, 3, 115, 115, 115, 122 };

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

    try {
        hin.readObject();
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e instanceof IOException);
    }
}
 
Example #15
Source File: SimpleTesGO2GO.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 GenericSerializerFactory());

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

    hin.close();
}
 
Example #16
Source File: TestArray.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenericObjectArray() throws IOException {
    GenericObject[] gpArr = dg.generateGenericObjectArray();

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

    hout.writeObject(gpArr);

    hout.close();

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

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

    assertEquals(gpArr.length, pArr.length);
    cmpGPersonEqualPerson(gpArr[0], (Person) pArr[0]);
    cmpGPersonEqualPerson(gpArr[1], (Person) pArr[1]);
    cmpGPersonEqualPerson(gpArr[2], (Person) pArr[2]);
}
 
Example #17
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 #18
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 #19
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 #20
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 #21
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 #22
Source File: SpecialClassTest.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
/**
 * 这个用例说明hessian写入的InputStram,反序列化时是byte[], 不兼容
 * @throws Exception
 */
@Test
public void testInputStream() throws Exception {
    InputStream inputStream = new FileInputStream("pom.xml");
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(bout);
    hout.setSerializerFactory(new SerializerFactory());
    hout.writeObject(inputStream);
    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(byte[].class, o.getClass());
}
 
Example #23
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 #24
Source File: Hessian2BlackListTest.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
@Test
public void testMapDeserialize() throws IOException {
    byte[] bs = new byte[] { 77, 79, -81, 99, 111, 109, 46, 97, 108, 105, 112, 97, 121, 46, 115, 116, 99, 46, 98,
            108, 46, 84, 101, 115, 116, 66, 108, 97, 99, 107, 66, 101, 97, 110, -111, 6, 115, 116, 114, 105,
            110, 103, 111, -112, 3, 115, 115, 115, 74, 1, 122 };

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

    try {
        hin.readObject();
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e instanceof IOException);
    }
}
 
Example #25
Source File: Hessian2BlackListTest.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
@Test
public void testBeanDeserialize() throws IOException {
    byte[] bs = new byte[] { 79, -81, 99, 111, 109, 46, 97, 108, 105, 112, 97, 121, 46, 115, 116, 99, 46, 98
            , 108, 46, 84, 101, 115, 116, 66, 108, 97, 99, 107, 66, 101, 97, 110,
            -111, 6, 115, 116, 114, 105, 110, 103, 111, -112, 3, 115, 115, 115 };

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

    try {
        hin.readObject();
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e instanceof IOException);
    }
}
 
Example #26
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 #27
Source File: HessianSerialize.java    From Raincat with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Object deserialize(final InputStream input) {
    Object result = null;
    try {
        Hessian2Input hi = new Hessian2Input(input);
        hi.startMessage();
        result = hi.readObject();
        hi.completeMessage();
        hi.close();
        input.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}
 
Example #28
Source File: SimpleTesGO2GO.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 GenericSerializerFactory());

    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 #29
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 #30
Source File: SofaRequestHessianSerializer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public void decodeObjectByTemplate(AbstractByteBuf data, Map<String, String> context, SofaRequest template)
    throws SofaRpcException {
    try {
        UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream(data.array());
        Hessian2Input input = new Hessian2Input(inputStream);
        input.setSerializerFactory(serializerFactory);
        Object object = input.readObject();
        SofaRequest tmp = (SofaRequest) object;
        String targetServiceName = tmp.getTargetServiceUniqueName();
        if (targetServiceName == null) {
            throw buildDeserializeError("Target service name of request is null!");
        }
        // copy values to template
        template.setMethodName(tmp.getMethodName());
        template.setMethodArgSigs(tmp.getMethodArgSigs());
        template.setTargetServiceUniqueName(tmp.getTargetServiceUniqueName());
        template.setTargetAppName(tmp.getTargetAppName());
        template.addRequestProps(tmp.getRequestProps());

        String interfaceName = ConfigUniqueNameGenerator.getInterfaceName(targetServiceName);
        template.setInterfaceName(interfaceName);

        // decode args
        String[] sig = template.getMethodArgSigs();
        Class<?>[] classSig = ClassTypeUtils.getClasses(sig);
        final Object[] args = new Object[sig.length];
        for (int i = 0; i < template.getMethodArgSigs().length; ++i) {
            args[i] = input.readObject(classSig[i]);
        }
        template.setMethodArgs(args);
        input.close();
    } catch (IOException e) {
        throw buildDeserializeError(e.getMessage(), e);
    }
}