Java Code Examples for com.alibaba.dubbo.common.serialize.support.java.CompactedObjectOutputStream#writeObject()

The following examples show how to use com.alibaba.dubbo.common.serialize.support.java.CompactedObjectOutputStream#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: Builder.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Override
public void writeTo(Serializable obj, GenericObjectOutput out) throws IOException
{
	if( obj == null )
	{
		out.write0(OBJECT_NULL);
	}
	else
	{
		out.write0(OBJECT_STREAM);
		UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream();
		CompactedObjectOutputStream oos = new CompactedObjectOutputStream(bos);
		oos.writeObject(obj);
		oos.flush();
		bos.close();
		byte[] b = bos.toByteArray();
		out.writeUInt(b.length);
		out.write0(b, 0, b.length);
	}
}
 
Example 2
Source File: SerializationCompareTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testCompactedJavaOutputPerm() throws Exception
{
	Bean bean = new Bean();
	int len = 0;
	long now = System.currentTimeMillis();
	for(int i=0;i<500;i++)
	{
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		CompactedObjectOutputStream out = new CompactedObjectOutputStream(os);
		out.writeObject(bean);
		os.close();
		if( i == 0 )
			len = os.toByteArray().length;
		ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
		CompactedObjectInputStream in = new CompactedObjectInputStream(is);
		assertEquals(in.readObject().getClass(), Bean.class);
	}
	System.out.println("compacted java write and parse 500 times in " + (System.currentTimeMillis()-now)+"ms, size " + len);
}
 
Example 3
Source File: Builder.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@Override
public void writeTo(Serializable obj, GenericObjectOutput out) throws IOException
{
	if( obj == null )
	{
		out.write0(OBJECT_NULL);
	}
	else
	{
		out.write0(OBJECT_STREAM);
		UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream();
		CompactedObjectOutputStream oos = new CompactedObjectOutputStream(bos);
		oos.writeObject(obj);
		oos.flush();
		bos.close();
		byte[] b = bos.toByteArray();
		out.writeUInt(b.length);
		out.write0(b, 0, b.length);
	}
}
 
Example 4
Source File: SerializationCompareTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@Test
public void testCompactedJavaOutputPerm() throws Exception
{
	Bean bean = new Bean();
	int len = 0;
	long now = System.currentTimeMillis();
	for(int i=0;i<500;i++)
	{
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		CompactedObjectOutputStream out = new CompactedObjectOutputStream(os);
		out.writeObject(bean);
		os.close();
		if( i == 0 )
			len = os.toByteArray().length;
		ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
		CompactedObjectInputStream in = new CompactedObjectInputStream(is);
		assertEquals(in.readObject().getClass(), Bean.class);
	}
	System.out.println("compacted java write and parse 500 times in " + (System.currentTimeMillis()-now)+"ms, size " + len);
}
 
Example 5
Source File: Builder.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
@Override
public void writeTo(Serializable obj, GenericObjectOutput out) throws IOException {
    if (obj == null) {
        out.write0(OBJECT_NULL);
    } else {
        out.write0(OBJECT_STREAM);
        UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream();
        CompactedObjectOutputStream oos = new CompactedObjectOutputStream(bos);
        oos.writeObject(obj);
        oos.flush();
        bos.close();
        byte[] b = bos.toByteArray();
        out.writeUInt(b.length);
        out.write0(b, 0, b.length);
    }
}
 
Example 6
Source File: SerializationCompareTest.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testCompactedJavaOutputPerm() throws Exception
{
	Bean bean = new Bean();
	int len = 0;
	long now = System.currentTimeMillis();
	for(int i=0;i<500;i++)
	{
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		CompactedObjectOutputStream out = new CompactedObjectOutputStream(os);
		out.writeObject(bean);
		os.close();
		if( i == 0 )
			len = os.toByteArray().length;
		ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
		CompactedObjectInputStream in = new CompactedObjectInputStream(is);
		assertEquals(in.readObject().getClass(), Bean.class);
	}
	System.out.println("compacted java write and parse 500 times in " + (System.currentTimeMillis()-now)+"ms, size " + len);
}
 
Example 7
Source File: Builder.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Override
public void writeTo(Serializable obj, GenericObjectOutput out) throws IOException
{
	if( obj == null )
	{
		out.write0(OBJECT_NULL);
	}
	else
	{
		out.write0(OBJECT_STREAM);
		UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream();
		CompactedObjectOutputStream oos = new CompactedObjectOutputStream(bos);
		oos.writeObject(obj);
		oos.flush();
		bos.close();
		byte[] b = bos.toByteArray();
		out.writeUInt(b.length);
		out.write0(b, 0, b.length);
	}
}
 
Example 8
Source File: SerializationCompareTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testCompactedJavaOutputPerm() throws Exception
{
	Bean bean = new Bean();
	int len = 0;
	long now = System.currentTimeMillis();
	for(int i=0;i<500;i++)
	{
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		CompactedObjectOutputStream out = new CompactedObjectOutputStream(os);
		out.writeObject(bean);
		os.close();
		if( i == 0 )
			len = os.toByteArray().length;
		ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
		CompactedObjectInputStream in = new CompactedObjectInputStream(is);
		assertEquals(in.readObject().getClass(), Bean.class);
	}
	System.out.println("compacted java write and parse 500 times in " + (System.currentTimeMillis()-now)+"ms, size " + len);
}
 
Example 9
Source File: Builder.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Override
public void writeTo(Serializable obj, GenericObjectOutput out) throws IOException
{
	if( obj == null )
	{
		out.write0(OBJECT_NULL);
	}
	else
	{
		out.write0(OBJECT_STREAM);
		UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream();
		CompactedObjectOutputStream oos = new CompactedObjectOutputStream(bos);
		oos.writeObject(obj);
		oos.flush();
		bos.close();
		byte[] b = bos.toByteArray();
		out.writeUInt(b.length);
		out.write0(b, 0, b.length);
	}
}
 
Example 10
Source File: SerializationCompareTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testCompactedJavaOutputPerm() throws Exception
{
	Bean bean = new Bean();
	int len = 0;
	long now = System.currentTimeMillis();
	for(int i=0;i<500;i++)
	{
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		CompactedObjectOutputStream out = new CompactedObjectOutputStream(os);
		out.writeObject(bean);
		os.close();
		if( i == 0 )
			len = os.toByteArray().length;
		ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
		CompactedObjectInputStream in = new CompactedObjectInputStream(is);
		assertEquals(in.readObject().getClass(), Bean.class);
	}
	System.out.println("compacted java write and parse 500 times in " + (System.currentTimeMillis()-now)+"ms, size " + len);
}
 
Example 11
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 12
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 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());
}