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

The following examples show how to use com.alibaba.com.caucho.hessian.io.Hessian2Output#writeObject() . 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: DubboRequestBody.java    From brpc-java with Apache License 2.0 6 votes vote down vote up
public byte[] encodeRequestBody() throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Hessian2Output hessian2Output = new Hessian2Output(outputStream);
    hessian2Output.setSerializerFactory(SERIALIZER_FACTORY);
    hessian2Output.writeString(dubboProtocolVersion);
    hessian2Output.writeString(path);
    hessian2Output.writeString(version);
    hessian2Output.writeString(methodName);
    hessian2Output.writeString(ReflectUtils.getDesc(parameterTypes));
    if (arguments != null) {
        for (int i = 0; i < arguments.length; i++) {
            hessian2Output.writeObject(arguments[i]);
        }
    }
    if (attachments != null) {
        hessian2Output.writeObject(attachments);
    }
    hessian2Output.flushBuffer();
    return outputStream.toByteArray();
}
 
Example 2
Source File: SerializationCompareTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testH2oPerm() throws Exception
{
	Bean bean = new Bean();
	int len = 0;
	long now = System.currentTimeMillis();
	for(int i=0;i<500;i++)
	{
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		Hessian2Output out = new Hessian2Output(os);
		out.writeObject(bean);
		out.flushBuffer();
		os.close();
		if( i == 0 )
			len = os.toByteArray().length;
		ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
		Hessian2Input in = new Hessian2Input(is);
		assertEquals(in.readObject().getClass(), Bean.class);
	}
	System.out.println("Hessian2 write and parse 500 times in " + (System.currentTimeMillis()-now)+"ms, size " + len);
}
 
Example 3
Source File: SerializationCompareTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@Test
public void testH2oPerm() throws Exception
{
	Bean bean = new Bean();
	int len = 0;
	long now = System.currentTimeMillis();
	for(int i=0;i<500;i++)
	{
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		Hessian2Output out = new Hessian2Output(os);
		out.writeObject(bean);
		out.flushBuffer();
		os.close();
		if( i == 0 )
			len = os.toByteArray().length;
		ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
		Hessian2Input in = new Hessian2Input(is);
		assertEquals(in.readObject().getClass(), Bean.class);
	}
	System.out.println("Hessian2 write and parse 500 times in " + (System.currentTimeMillis()-now)+"ms, size " + len);
}
 
Example 4
Source File: SerializationCompareTest.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testH2oPerm() throws Exception
{
	Bean bean = new Bean();
	int len = 0;
	long now = System.currentTimeMillis();
	for(int i=0;i<500;i++)
	{
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		Hessian2Output out = new Hessian2Output(os);
		out.writeObject(bean);
		out.flushBuffer();
		os.close();
		if( i == 0 )
			len = os.toByteArray().length;
		ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
		Hessian2Input in = new Hessian2Input(is);
		assertEquals(in.readObject().getClass(), Bean.class);
	}
	System.out.println("Hessian2 write and parse 500 times in " + (System.currentTimeMillis()-now)+"ms, size " + len);
}
 
Example 5
Source File: SerializationCompareTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testH2oPerm() throws Exception
{
	Bean bean = new Bean();
	int len = 0;
	long now = System.currentTimeMillis();
	for(int i=0;i<500;i++)
	{
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		Hessian2Output out = new Hessian2Output(os);
		out.writeObject(bean);
		out.flushBuffer();
		os.close();
		if( i == 0 )
			len = os.toByteArray().length;
		ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
		Hessian2Input in = new Hessian2Input(is);
		assertEquals(in.readObject().getClass(), Bean.class);
	}
	System.out.println("Hessian2 write and parse 500 times in " + (System.currentTimeMillis()-now)+"ms, size " + len);
}
 
Example 6
Source File: SerializationCompareTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testH2oPerm() throws Exception
{
	Bean bean = new Bean();
	int len = 0;
	long now = System.currentTimeMillis();
	for(int i=0;i<500;i++)
	{
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		Hessian2Output out = new Hessian2Output(os);
		out.writeObject(bean);
		out.flushBuffer();
		os.close();
		if( i == 0 )
			len = os.toByteArray().length;
		ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
		Hessian2Input in = new Hessian2Input(is);
		assertEquals(in.readObject().getClass(), Bean.class);
	}
	System.out.println("Hessian2 write and parse 500 times in " + (System.currentTimeMillis()-now)+"ms, size " + len);
}
 
Example 7
Source File: DubboResponseBody.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
public byte[] encodeResponseBody() throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(DubboConstants.DEFAULT_OUTPUT_BUFFER_SIZE);
    Hessian2Output hessian2Output = new Hessian2Output(outputStream);
    hessian2Output.setSerializerFactory(DubboRequestBody.SERIALIZER_FACTORY);
    hessian2Output.writeInt(responseType);
    hessian2Output.writeObject(result);
    if (attachments != null && attachments.size() > 0) {
        hessian2Output.writeObject(attachments);
    }
    hessian2Output.flush();
    return outputStream.toByteArray();
}
 
Example 8
Source File: Java8TimeSerializerTest.java    From dubbo-hessian-lite 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 9
Source File: SerializeTestBase.java    From dubbo-hessian-lite with Apache License 2.0 5 votes vote down vote up
/**
 * hessian2 serialize util
 *
 * @param data
 * @param <T>
 * @return
 * @throws IOException
 */
protected <T> T baseHessian2Serialize(T data) throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Hessian2Output out = new Hessian2Output(bout);

    out.writeObject(data);
    out.flush();

    ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
    Hessian2Input input = new Hessian2Input(bin);
    return (T) input.readObject();
}
 
Example 10
Source File: SerializationCompareTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void test_CompareSerializeLength() throws Exception
{
	long[] data = new long[]{ -1l, 2l, 3l, 4l, 5l };
	ByteArrayOutputStream os;

	os = new ByteArrayOutputStream();
	ObjectOutputStream jos = new ObjectOutputStream(os);
	jos.writeObject(data);
	System.out.println("java:"+Bytes.bytes2hex(os.toByteArray())+":"+os.size());

	os = new ByteArrayOutputStream();
	CompactedObjectOutputStream oos = new CompactedObjectOutputStream(os);
	oos.writeObject(data);
	System.out.println("compacted java:"+Bytes.bytes2hex(os.toByteArray())+":"+os.size());

	os = new ByteArrayOutputStream();
	Hessian2Output h2o = new Hessian2Output(os);
	h2o.writeObject(data);
	h2o.flushBuffer();
	System.out.println("hessian:"+Bytes.bytes2hex(os.toByteArray())+":"+os.size());

	os = new ByteArrayOutputStream();
	Builder<long[]> lb = Builder.register(long[].class);
	lb.writeTo(data, os);
	System.out.println("DataOutput:"+Bytes.bytes2hex(os.toByteArray())+":"+os.size());
}
 
Example 11
Source File: SerializationCompareTest.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@Test
public void test_CompareSerializeLength() throws Exception
{
	long[] data = new long[]{ -1l, 2l, 3l, 4l, 5l };
	ByteArrayOutputStream os;

	os = new ByteArrayOutputStream();
	ObjectOutputStream jos = new ObjectOutputStream(os);
	jos.writeObject(data);
	System.out.println("java:"+Bytes.bytes2hex(os.toByteArray())+":"+os.size());

	os = new ByteArrayOutputStream();
	CompactedObjectOutputStream oos = new CompactedObjectOutputStream(os);
	oos.writeObject(data);
	System.out.println("compacted java:"+Bytes.bytes2hex(os.toByteArray())+":"+os.size());

	os = new ByteArrayOutputStream();
	Hessian2Output h2o = new Hessian2Output(os);
	h2o.writeObject(data);
	h2o.flushBuffer();
	System.out.println("hessian:"+Bytes.bytes2hex(os.toByteArray())+":"+os.size());

	os = new ByteArrayOutputStream();
	Builder<long[]> lb = Builder.register(long[].class);
	lb.writeTo(data, os);
	System.out.println("DataOutput:"+Bytes.bytes2hex(os.toByteArray())+":"+os.size());
}
 
Example 12
Source File: HessianUtil.java    From j360-dubbo-app-all with Apache License 2.0 5 votes vote down vote up
public static byte[] encode(Object obj) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
    Hessian2Output h2o = new Hessian2Output(os);
    h2o.setSerializerFactory(serializerFactory);

    try {
        h2o.writeObject(obj);
    } finally {
        closeableQuietly(os);
        closeQuietly(h2o);
    }

    return os.toByteArray();
}
 
Example 13
Source File: SerializationCompareTest.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
@Test
public void test_CompareSerializeLength() throws Exception
{
	long[] data = new long[]{ -1l, 2l, 3l, 4l, 5l };
	ByteArrayOutputStream os;

	os = new ByteArrayOutputStream();
	ObjectOutputStream jos = new ObjectOutputStream(os);
	jos.writeObject(data);
	System.out.println("java:"+Bytes.bytes2hex(os.toByteArray())+":"+os.size());

	os = new ByteArrayOutputStream();
	CompactedObjectOutputStream oos = new CompactedObjectOutputStream(os);
	oos.writeObject(data);
	System.out.println("compacted java:"+Bytes.bytes2hex(os.toByteArray())+":"+os.size());

	os = new ByteArrayOutputStream();
	Hessian2Output h2o = new Hessian2Output(os);
	h2o.writeObject(data);
	h2o.flushBuffer();
	System.out.println("hessian:"+Bytes.bytes2hex(os.toByteArray())+":"+os.size());

	os = new ByteArrayOutputStream();
	Builder<long[]> lb = Builder.register(long[].class);
	lb.writeTo(data, os);
	System.out.println("DataOutput:"+Bytes.bytes2hex(os.toByteArray())+":"+os.size());
}
 
Example 14
Source File: SerializationCompareTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void test_CompareSerializeLength() throws Exception
{
	long[] data = new long[]{ -1l, 2l, 3l, 4l, 5l };
	ByteArrayOutputStream os;

	os = new ByteArrayOutputStream();
	ObjectOutputStream jos = new ObjectOutputStream(os);
	jos.writeObject(data);
	System.out.println("java:"+Bytes.bytes2hex(os.toByteArray())+":"+os.size());

	os = new ByteArrayOutputStream();
	CompactedObjectOutputStream oos = new CompactedObjectOutputStream(os);
	oos.writeObject(data);
	System.out.println("compacted java:"+Bytes.bytes2hex(os.toByteArray())+":"+os.size());

	os = new ByteArrayOutputStream();
	Hessian2Output h2o = new Hessian2Output(os);
	h2o.writeObject(data);
	h2o.flushBuffer();
	System.out.println("hessian:"+Bytes.bytes2hex(os.toByteArray())+":"+os.size());

	os = new ByteArrayOutputStream();
	Builder<long[]> lb = Builder.register(long[].class);
	lb.writeTo(data, os);
	System.out.println("DataOutput:"+Bytes.bytes2hex(os.toByteArray())+":"+os.size());
}
 
Example 15
Source File: SerializationCompareTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void test_CompareSerializeLength() throws Exception
{
	long[] data = new long[]{ -1l, 2l, 3l, 4l, 5l };
	ByteArrayOutputStream os;

	os = new ByteArrayOutputStream();
	ObjectOutputStream jos = new ObjectOutputStream(os);
	jos.writeObject(data);
	System.out.println("java:"+Bytes.bytes2hex(os.toByteArray())+":"+os.size());

	os = new ByteArrayOutputStream();
	CompactedObjectOutputStream oos = new CompactedObjectOutputStream(os);
	oos.writeObject(data);
	System.out.println("compacted java:"+Bytes.bytes2hex(os.toByteArray())+":"+os.size());

	os = new ByteArrayOutputStream();
	Hessian2Output h2o = new Hessian2Output(os);
	h2o.writeObject(data);
	h2o.flushBuffer();
	System.out.println("hessian:"+Bytes.bytes2hex(os.toByteArray())+":"+os.size());

	os = new ByteArrayOutputStream();
	Builder<long[]> lb = Builder.register(long[].class);
	lb.writeTo(data, os);
	System.out.println("DataOutput:"+Bytes.bytes2hex(os.toByteArray())+":"+os.size());
}