com.caucho.hessian.io.Hessian2Output Java Examples

The following examples show how to use com.caucho.hessian.io.Hessian2Output. 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: CollectionTest.java    From sofa-hessian with 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 #2
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 #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: 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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
Source File: TestPerformance.java    From sofa-hessian with 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 #12
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 #13
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 #14
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 #15
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 #16
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 #17
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 #18
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 #19
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 #20
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 #21
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 #22
Source File: RaftServerHandler.java    From sofa-registry with 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 #23
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 #24
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 #25
Source File: HessianSerializer.java    From AutoLoadCache with 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 #26
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 #27
Source File: ComplexTestGO2GO.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 GenericSerializerFactory());

    List dlist = (List) hin.readObject();
    List list = (List) dg.generateListPerson_1();

    assertEquals(dlist.size(), list.size());
    cmpGPersonEqualPerson((GenericObject) dlist.get(0), (Person) list.get(0));
    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());
    cmpGPersonEqualPerson((GenericObject) dlist2.get(0), (Person) list2.get(0));
    cmpGPersonEqualPerson((GenericObject) dlist2.get(1), (Person) list2.get(1));
    cmpGPersonEqualPerson((GenericObject) dlist2.get(2), (Person) list2.get(2));
    assertSame(dlist2.get(0), dlist2.get(2));

}
 
Example #28
Source File: HessianSerialize.java    From Lottor with MIT License 5 votes vote down vote up
@Override
public void serialize(OutputStream output, Object object) {
    Hessian2Output ho = new Hessian2Output(output);
    try {
        ho.startMessage();
        ho.writeObject(object);
        ho.completeMessage();
        ho.close();
        output.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #29
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 #30
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();
}