Java Code Examples for com.caucho.hessian.io.Hessian2Output

The following examples show how to use com.caucho.hessian.io.Hessian2Output. These examples are extracted from open source projects. 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 Project: sofa-registry   Source File: RaftServerHandler.java    License: Apache License 2.0 6 votes vote down vote up
protected Task createTask(LeaderTaskClosure closure, ProcessRequest request) {

        final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        Hessian2Output hessianOutput = new Hessian2Output(byteStream);
        SerializerFactory serializerFactory = new SerializerFactory();
        hessianOutput.setSerializerFactory(serializerFactory);
        try {
            hessianOutput.writeObject(request);
            hessianOutput.close();
        } catch (IOException e) {
            LOGGER.error("Raft receive message serialize error!", e);
        }

        byte[] cmdBytes = byteStream.toByteArray();

        ByteBuffer data = ByteBuffer.allocate(cmdBytes.length);
        data.put(cmdBytes);
        data.flip();
        return new Task(data, closure);
    }
 
Example 2
@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 3
Source Project: sofa-hessian   Source File: SimpleTestGO2O.java    License: 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
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 Project: sofa-hessian   Source File: SimpleTestGO2O.java    License: 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 6
Source Project: sofa-hessian   Source File: LongArrayTest.java    License: 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 7
Source Project: sofa-hessian   Source File: SimpleTestO2GO.java    License: 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 8
Source Project: sofa-hessian   Source File: SimpleTestO2GO.java    License: 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 9
Source Project: sofa-hessian   Source File: SimpleTestO2GO.java    License: 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 10
Source Project: Jupiter   Source File: HessianSerializer.java    License: 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 11
Source Project: sofa-hessian   Source File: SimpleTesGO2GO.java    License: 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 12
Source Project: sofa-hessian   Source File: SimpleTesGO2GO.java    License: 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 13
Source Project: sofa-hessian   Source File: Hessian2BlackListTest.java    License: 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 14
Source Project: sofa-hessian   Source File: Hessian2BlackListTest.java    License: 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 15
Source Project: sofa-hessian   Source File: SerializerExample.java    License: 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 16
Source Project: sofa-hessian   Source File: SpecialClassTest.java    License: 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 17
Source Project: sofa-hessian   Source File: SpecialClassTest.java    License: 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 18
Source Project: Jupiter   Source File: HessianSerializer.java    License: 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 19
Source Project: AutoLoadCache   Source File: HessianSerializer.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public byte[] serialize(final Object obj) throws Exception {
    if (obj == null) {
        return null;
    }

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    AbstractHessianOutput output = new Hessian2Output(outputStream);
    output.setSerializerFactory(SERIALIZER_FACTORY);
    // 将对象写到流里
    output.writeObject(obj);
    output.flush();
    byte[] val = outputStream.toByteArray();
    output.close();
    return val;
}
 
Example 20
@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 21
Source Project: sofa-hessian   Source File: CollectionTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testStringArray() throws Exception {

    Object[] strs = new String[3];
    strs[0] = "11111";
    strs[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;
    assertTrue(3 == s.length);
    assertEquals("11111", s[0]);
    assertEquals("22222", s[1]);
    assertEquals(null, s[2]);
}
 
Example 22
Source Project: sofa-hessian   Source File: CollectionTest.java    License: 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 23
Source Project: sofa-hessian   Source File: TestArray.java    License: 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 24
Source Project: sofa-hessian   Source File: TestArray.java    License: 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 25
Source Project: sofa-hessian   Source File: TestPerformance.java    License: 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 26
Source Project: sofa-hessian   Source File: TestPerformance.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGenericHessianSerialize() throws IOException {
    Object person = dg.generateMixGenericObject();

    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 GenericSerializerFactory());
        hout.writeObject(person);
        hout.close();
    }
    long endTime = System.currentTimeMillis();

    bout.close();

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

}
 
Example 27
Source Project: jvm-sandbox-repeater   Source File: X509Encryption.java    License: Apache License 2.0 5 votes vote down vote up
public Hessian2Output wrap(Hessian2Output out)
  throws IOException
{
  if (_cert == null)
    throw new IOException("X509Encryption.wrap requires a certificate");
  
  OutputStream os = new EncryptOutputStream(out);
  
  Hessian2Output filterOut = new Hessian2Output(os);

  filterOut.setCloseStreamOnClose(true);
  
  return filterOut;
}
 
Example 28
Source Project: jvm-sandbox-repeater   Source File: X509Encryption.java    License: Apache License 2.0 5 votes vote down vote up
public void close()
  throws IOException
{
  Hessian2Output out = _out;
  _out = null;

  if (out != null) {
    _cipherOut.close();
    _bodyOut.close();

    out.writeInt(0);
    out.completeEnvelope();
    out.close();
  }
}
 
Example 29
private byte[] encode(Object object) {
    UnsafeByteArrayOutputStream byteArray = new UnsafeByteArrayOutputStream();
    Hessian2Output output = new Hessian2Output(byteArray);
    try {
        output.setSerializerFactory(serializerFactory);
        output.writeObject(object);
        output.close();
        return byteArray.toByteArray();
    } catch (Exception e) {
        throw new GrpcException("sofa-hessian serialize fail: " + e.getMessage());
    }
}
 
Example 30
Source Project: zkdoctor   Source File: HessianSerializerUtils.java    License: 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();
}