com.alibaba.com.caucho.hessian.io.Hessian2Output Java Examples

The following examples show how to use com.alibaba.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: 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 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: X509Signature.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
public void close()
        throws IOException {
    Hessian2Output out = _out;
    _out = null;

    if (out == null)
        return;

    _bodyOut.close();

    byte[] sig = _mac.doFinal();

    out.writeInt(1);
    out.writeString("signature");
    out.writeBytes(sig);

    out.completeEnvelope();
    out.close();
}
 
Example #5
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 #6
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 #7
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 #8
Source File: X509Signature.java    From dubbo-hessian-lite with Apache License 2.0 6 votes vote down vote up
@Override
public void close()
        throws IOException {
    Hessian2Output out = _out;
    _out = null;

    if (out == null)
        return;

    _bodyOut.close();

    byte[] sig = _mac.doFinal();

    out.writeInt(1);
    out.writeString("signature");
    out.writeBytes(sig);

    out.completeEnvelope();
    out.close();
}
 
Example #9
Source File: X509Signature.java    From dubbo-hessian-lite with Apache License 2.0 6 votes vote down vote up
@Override
public Hessian2Output wrap(Hessian2Output out)
        throws IOException {
    if (_privateKey == null)
        throw new IOException("X509Signature.wrap requires a private key");

    if (_cert == null)
        throw new IOException("X509Signature.wrap requires a certificate");

    OutputStream os = new SignatureOutputStream(out);

    Hessian2Output filterOut = new Hessian2Output(os);

    filterOut.setCloseStreamOnClose(true);

    return filterOut;
}
 
Example #10
Source File: DubboResponseBody.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
public static byte[] encodeHeartbeatResponseBody() throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Hessian2Output hessian2Output = new Hessian2Output(outputStream);
    hessian2Output.setSerializerFactory(DubboRequestBody.SERIALIZER_FACTORY);
    hessian2Output.writeString(DubboConstants.HEARTBEAT_EVENT);
    hessian2Output.flush();
    byte[] bodyBytes = outputStream.toByteArray();
    return bodyBytes;
}
 
Example #11
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 #12
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 #13
Source File: X509Encryption.java    From dubbo3 with 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 #14
Source File: DubboPacket.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
public static byte[] encodeHeartbeatBody() throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Hessian2Output hessian2Output = new Hessian2Output(outputStream);
    hessian2Output.writeString(DubboConstants.HEARTBEAT_EVENT);
    hessian2Output.flushBuffer();
    byte[] bodyBytes = outputStream.toByteArray();
    return bodyBytes;
}
 
Example #15
Source File: DubboResponseBody.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
public static byte[] encodeErrorResponseBody(String errorMessage) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Hessian2Output hessian2Output = new Hessian2Output(outputStream);
    hessian2Output.setSerializerFactory(DubboRequestBody.SERIALIZER_FACTORY);
    hessian2Output.writeString(errorMessage);
    hessian2Output.flush();
    byte[] bodyBytes = outputStream.toByteArray();
    return bodyBytes;
}
 
Example #16
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 #17
Source File: HessianUtil.java    From j360-dubbo-app-all with Apache License 2.0 5 votes vote down vote up
private static void closeQuietly(Hessian2Output hessian2Output) {
    try {
        if (hessian2Output != null) {
            hessian2Output.close();
        }
    } catch (IOException e) {
        logger.warn("close hessian2Output failed");
    }
}
 
Example #18
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 #19
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 #20
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 #21
Source File: X509Encryption.java    From dubbo-hessian-lite with Apache License 2.0 5 votes vote down vote up
@Override
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 #22
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 #23
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 #24
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 #25
Source File: X509Encryption.java    From dubbo-hessian-lite with Apache License 2.0 5 votes vote down vote up
@Override
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 #26
Source File: Hessian2ObjectOutput.java    From dubbo-2.6.5 with Apache License 2.0 4 votes vote down vote up
public Hessian2ObjectOutput(OutputStream os) {
    mH2o = new Hessian2Output(os);
    mH2o.setSerializerFactory(Hessian2SerializerFactory.SERIALIZER_FACTORY);
}
 
Example #27
Source File: Hessian2ObjectOutput.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public Hessian2ObjectOutput(OutputStream os)
{
	mH2o = new Hessian2Output(os);
	mH2o.setSerializerFactory(Hessian2SerializerFactory.SERIALIZER_FACTORY);
}
 
Example #28
Source File: Hessian2ObjectOutput.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public Hessian2ObjectOutput(OutputStream os)
{
	mH2o = new Hessian2Output(os);
	mH2o.setSerializerFactory(Hessian2SerializerFactory.SERIALIZER_FACTORY);
}
 
Example #29
Source File: X509Signature.java    From dubbo3 with Apache License 2.0 4 votes vote down vote up
public Hessian2Output wrap(Hessian2Output out)
        throws IOException {
    if (_privateKey == null)
        throw new IOException("X509Signature.wrap requires a private key");

    if (_cert == null)
        throw new IOException("X509Signature.wrap requires a certificate");

    OutputStream os = new SignatureOutputStream(out);

    Hessian2Output filterOut = new Hessian2Output(os);

    filterOut.setCloseStreamOnClose(true);

    return filterOut;
}
 
Example #30
Source File: Hessian2ObjectOutput.java    From dubbo3 with Apache License 2.0 4 votes vote down vote up
public Hessian2ObjectOutput(OutputStream os)
{
	mH2o = new Hessian2Output(os);
	mH2o.setSerializerFactory(Hessian2SerializerFactory.SERIALIZER_FACTORY);
}