Java Code Examples for com.caucho.hessian.io.Hessian2Output#flush()

The following examples show how to use com.caucho.hessian.io.Hessian2Output#flush() . 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: HessianSerializer.java    From Jupiter with Apache License 2.0 6 votes vote down vote up
@Override
public <T> byte[] writeObject(T obj) {
    ByteArrayOutputStream buf = OutputStreams.getByteArrayOutputStream();
    Hessian2Output output = Outputs.getOutput(buf);
    try {
        output.writeObject(obj);
        output.flush();
        return buf.toByteArray();
    } catch (IOException e) {
        ThrowUtil.throwException(e);
    } finally {
        try {
            output.close();
        } catch (IOException ignored) {}

        OutputStreams.resetBuf(buf);
    }
    return null; // never get here
}
 
Example 3
Source File: HessianSerializer.java    From Jupiter with Apache License 2.0 6 votes vote down vote up
@Override
public <T> OutputBuf writeObject(OutputBuf outputBuf, T obj) {
    Hessian2Output output = Outputs.getOutput(outputBuf);
    try {
        output.writeObject(obj);
        output.flush();
        return outputBuf;
    } catch (IOException e) {
        ThrowUtil.throwException(e);
    } finally {
        try {
            output.close();
        } catch (IOException ignored) {}
    }
    return null; // never get here
}
 
Example 4
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 5
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 6
Source File: Hessian2BlackListTest.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
@Test
public void testMapSerialize() throws IOException {
    TestBlackBean blackBean = new TestBlackBean().setString("sss");
    Map<TestBlackBean, TestBlackBean> map = new HashMap<TestBlackBean, TestBlackBean>();
    map.put(blackBean, blackBean);

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(output);
    hout.setSerializerFactory(serializerFactory);

    try {
        hout.writeObject(map);
        hout.flush();
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e instanceof IOException);
    }
}
 
Example 7
Source File: Hessian2BlackListTest.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
@Test
public void testArraySerialize() throws IOException {
    TestBlackBean blackBean = new TestBlackBean().setString("sss");
    Object[] array = new Object[] { blackBean };

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(output);
    hout.setSerializerFactory(serializerFactory);

    try {
        hout.writeObject(array);
        hout.flush();
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e instanceof IOException);
    }
}
 
Example 8
Source File: Hessian2BlackListTest.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
@Test
public void testListSerialize() throws IOException {
    TestBlackBean blackBean = new TestBlackBean().setString("sss");
    List list = new ArrayList<TestBlackBean>();
    list.add(blackBean);

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(output);
    hout.setSerializerFactory(serializerFactory);

    try {
        hout.writeObject(list);
        hout.flush();
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e instanceof IOException);
    }
}
 
Example 9
Source File: Hessian2BlackListTest.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
@Test
public void testBeanSerialize() throws IOException {
    TestBlackBean blackBean = new TestBlackBean().setString("sss");

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    Hessian2Output hout = new Hessian2Output(output);
    hout.setSerializerFactory(serializerFactory);

    try {
        hout.writeObject(blackBean);
        hout.flush();
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e instanceof IOException);
    }
}
 
Example 10
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 11
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 12
Source File: Java8TimeSerializerTest.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
private void testJava8Time(Object expected) throws IOException {
    os.reset();

    Hessian2Output output = new Hessian2Output(os);
    output.setSerializerFactory(factory);
    output.writeObject(expected);
    output.flush();

    ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
    Hessian2Input input = new Hessian2Input(is);
    input.setSerializerFactory(factory);
    Object actual = input.readObject();

    TestCase.assertEquals(expected, actual);
}
 
Example 13
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 14
Source File: HessianSerialization.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] serialize(Object data) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Hessian2Output out = new Hessian2Output(bos);
    out.writeObject(data);
    out.flush();
    return bos.toByteArray();
}
 
Example 15
Source File: HessianSerializer.java    From voyage with Apache License 2.0 5 votes vote down vote up
@Override
public void encodeResponse(OutputStream outputStream, RpcResponse result)
		throws IOException {
        Hessian2Output out = new Hessian2Output(outputStream);  
        out.writeObject(result);  
        out.flush();
}
 
Example 16
Source File: HessianSerializer.java    From voyage with Apache License 2.0 5 votes vote down vote up
@Override
public void encodeRequest(OutputStream outputStream, RpcRequest request)
		throws IOException {
       Hessian2Output out = new Hessian2Output(outputStream);  
       out.writeObject(request);
       out.flush();
}
 
Example 17
Source File: HessianSerializer.java    From tx-lcn with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] serialize(Object obj) throws SerializerException {
    ByteArrayOutputStream baos;
    try {
        baos = new ByteArrayOutputStream();
        Hessian2Output hos = new Hessian2Output(baos);
        hos.writeObject(obj);
        hos.flush();
        hos.close();
    } catch (IOException ex) {
        throw new SerializerException("Hessian serialize error " + ex.getMessage());
    }
    return baos.toByteArray();
}
 
Example 18
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]);
}