当前位置: 首页>>代码示例>>Java>>正文


Java UnsafeByteArrayOutputStream.toByteArray方法代码示例

本文整理汇总了Java中com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream.toByteArray方法的典型用法代码示例。如果您正苦于以下问题:Java UnsafeByteArrayOutputStream.toByteArray方法的具体用法?Java UnsafeByteArrayOutputStream.toByteArray怎么用?Java UnsafeByteArrayOutputStream.toByteArray使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream的用法示例。


在下文中一共展示了UnsafeByteArrayOutputStream.toByteArray方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: writeTo

import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream; //导入方法依赖的package包/类
@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);
	}
}
 
开发者ID:dachengxi,项目名称:EatDubbo,代码行数:21,代码来源:Builder.java

示例2: testBuilder_MyList

import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream; //导入方法依赖的package包/类
@Ignore
   @Test
   @SuppressWarnings({ "rawtypes", "unchecked" })
   public void testBuilder_MyList() throws Exception
{
       Builder<MyList> b1 = Builder.register(MyList.class);
	MyList list = new MyList();
	list.add(new boolean[]{ true,false });
	list.add(new int[]{ 1,2,3,4,5 });
	list.add("String");
	list.add(4);
	list.code = 4321;
	
	UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
	b1.writeTo(list, os);
	byte[] b = os.toByteArray();
	System.out.println(b.length+":"+Bytes.bytes2hex(b));
	MyList result = b1.parseFrom(b);

	assertEquals(4, result.size());
	assertEquals(result.code, 4321);
	assertEquals(result.id, "feedback");
}
 
开发者ID:yunhaibin,项目名称:dubbox-hystrix,代码行数:24,代码来源:BuilderTest.java

示例3: testObjectArrayBuilder

import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream; //导入方法依赖的package包/类
@Test
public void testObjectArrayBuilder() throws Exception
{
	UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
	Builder<Object[]> builder = Builder.register(Object[].class);

	Object[] obj = new Object[5];
	obj[0] = "1234";
	obj[1] = new Double(109.23);
	obj[2] = "3455";
	obj[3] = null;
	obj[4] = Boolean.TRUE;

	builder.writeTo(obj, os);
	byte[] b = os.toByteArray();
	System.out.println("Object array:"+b.length+":"+Bytes.bytes2hex(b));

	Assert.assertArrayEquals(obj, builder.parseFrom(b));
}
 
开发者ID:zhuxiaolei,项目名称:dubbo2,代码行数:20,代码来源:BuilderTest.java

示例4: testBuilder_MyMap

import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream; //导入方法依赖的package包/类
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testBuilder_MyMap() throws Exception
{
    UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
    Builder<MyMap> b2 = Builder.register(MyMap.class);
    MyMap map = new MyMap();
    map.put("name", "qianlei");
    map.put("displayName", "钱磊");
    map.code = 4321;
    b2.writeTo(map, os);
    byte[] b = os.toByteArray();
    System.out.println(b.length+":"+Bytes.bytes2hex(b));
    
    map = b2.parseFrom(b);
    
    assertEquals(map.size(), 2);
    assertEquals(map.code, 4321);
    assertEquals(map.id, "feedback");
}
 
开发者ID:zhuxiaolei,项目名称:dubbo2,代码行数:21,代码来源:BuilderTest.java

示例5: testThrowableBuilder

import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream; //导入方法依赖的package包/类
@Test
public void testThrowableBuilder() throws Exception
{
	Builder<Throwable> builder = Builder.register(Throwable.class);
	Throwable th = new Throwable();
	UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
	builder.writeTo(th, os);
	byte[] b = os.toByteArray();
	System.out.println(b.length+":"+Bytes.bytes2hex(b));

	th = builder.parseFrom(b);
}
 
开发者ID:flychao88,项目名称:dubbocloud,代码行数:13,代码来源:BuilderTest.java

示例6: getRequestBytes

import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream; //导入方法依赖的package包/类
private byte[] getRequestBytes(Object obj, byte[] header) throws IOException{
    // encode request data.
    UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(1024);
    ObjectOutput out = serialization.serialize(url, bos);
    out.writeObject(obj);
    
    out.flushBuffer();
    bos.flush();
    bos.close();
    byte[] data = bos.toByteArray();
    byte[] len = Bytes.int2bytes(data.length);
    System.arraycopy(len, 0, header, 12, 4);
    byte[] request = join(header, data);
    return request;
}
 
开发者ID:yunhaibin,项目名称:dubbox-hystrix,代码行数:16,代码来源:ExchangeCodecTest.java

示例7: testSerializableBean

import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream; //导入方法依赖的package包/类
@Test
@SuppressWarnings("unchecked")
public void testSerializableBean() throws Exception
{
	System.out.println("testSerializableBean");
	UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();

	SerializableBean sb = new SerializableBean();
	Builder<SerializableBean> sbb = Builder.register(SerializableBean.class);
	sbb.writeTo(sb, os);

	byte[] b = os.toByteArray();
	System.out.println(b.length+":"+Bytes.bytes2hex(b));
	assertEquals(sbb.parseFrom(os.toByteArray()), sb);
}
 
开发者ID:flychao88,项目名称:dubbocloud,代码行数:16,代码来源:BuilderTest.java

示例8: testObjectBuilder

import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream; //导入方法依赖的package包/类
@Test
public void testObjectBuilder() throws Exception
{
	UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
	Builder<Bean> BeanBuilder = Builder.register(Bean.class);

	Bean bean = new Bean();
	bean.name = "ql";
	bean.type = Type.High;
	bean.types = new Type[]{ Type.High, Type.High };
	BeanBuilder.writeTo(bean, os);

	byte[] b = os.toByteArray();
	System.out.println(b.length+":"+Bytes.bytes2hex(b));

	bean = BeanBuilder.parseFrom(b);
	assertNull(bean.time);
	assertEquals(bean.i, 123123);
	assertEquals(bean.ni, -12344);
	assertEquals(bean.d, 12.345);
	assertEquals(bean.nd, -12.345);
	assertEquals(bean.l, 1281447759383l);
	assertEquals(bean.nl, -13445l);
	assertEquals(bean.vl, 100l);
	assertEquals(bean.type, Type.High);
	assertEquals(bean.types.length, 2);
	assertEquals(bean.types[0], Type.High);
	assertEquals(bean.types[1], Type.High);
	assertEquals(bean.list.size(), 3);
	assertEquals(bean.list.get(0), 1);
	assertEquals(bean.list.get(1), 2);
	assertEquals(bean.list.get(2), 1308147);
}
 
开发者ID:yunhaibin,项目名称:dubbox-hystrix,代码行数:34,代码来源:BuilderTest.java

示例9: testPrimaryTypeBuilder

import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream; //导入方法依赖的package包/类
@Test
public void testPrimaryTypeBuilder() throws Exception
{
	System.out.println((new byte[2]).hashCode());
	Builder<String> builder = Builder.register(String.class);
	UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
	String v = "123";
	builder.writeTo(v, os);
	byte[] b = os.toByteArray();
	System.out.println(b.length+":"+Bytes.bytes2hex(b));
	v = builder.parseFrom(b);
	builder.writeTo(v, os);
	b = os.toByteArray();
	System.out.println(b.length+":"+Bytes.bytes2hex(b));
}
 
开发者ID:zhuxiaolei,项目名称:dubbo2,代码行数:16,代码来源:BuilderTest.java

示例10: testEnumBuilder

import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream; //导入方法依赖的package包/类
@Test
public void testEnumBuilder() throws Exception
{
	Builder<Type> builder = Builder.register(Type.class);
	UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
	Type v = Type.High;
	builder.writeTo(v, os);
	byte[] b = os.toByteArray();
	System.out.println(b.length+":"+Bytes.bytes2hex(b));
	v = builder.parseFrom(b);
}
 
开发者ID:dachengxi,项目名称:EatDubbo,代码行数:12,代码来源:BuilderTest.java

示例11: encodeRequest

import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream; //导入方法依赖的package包/类
protected void encodeRequest(Channel channel, OutputStream os, Request req) throws IOException {
    Serialization serialization = CodecSupport.getSerialization(channel.getUrl());
    // header.
    byte[] header = new byte[HEADER_LENGTH];
    // set magic number.
    Bytes.short2bytes(MAGIC, header);

    // set request and serialization flag.
    header[2] = (byte) (FLAG_REQUEST | serialization.getContentTypeId());

    if (req.isTwoWay()) header[2] |= FLAG_TWOWAY;
    if (req.isEvent()) header[2] |= FLAG_EVENT;

    // set request id.
    Bytes.long2bytes(req.getId(), header, 4);

    // encode request data.
    UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(1024);
    ObjectOutput out = serialization.serialize(channel.getUrl(), bos);
    if (req.isEvent()) {
        encodeEventData(channel, out, req.getData());
    } else {
        encodeRequestData(channel, out, req.getData());
    }
    out.flushBuffer();
    bos.flush();
    bos.close();
    byte[] data = bos.toByteArray();
    checkPayload(channel, data.length);
    Bytes.int2bytes(data.length, header, 12);

    // write
    os.write(header); // write header.
    os.write(data); // write data.
}
 
开发者ID:yunhaibin,项目名称:dubbox-hystrix,代码行数:36,代码来源:DeprecatedExchangeCodec.java

示例12: testMain

import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream; //导入方法依赖的package包/类
public void testMain() throws Exception
{
	// write.
	UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
	DataOutput cos = new GenericDataOutput(os);
	writeTest(cos);

	// read.
	byte[] b = os.toByteArray();
	DataInput cis = new GenericDataInput(new UnsafeByteArrayInputStream(b));
	readTest(cis);
}
 
开发者ID:linux-china,项目名称:dubbo3,代码行数:13,代码来源:DataInputOutputTest.java

示例13: serialize

import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream; //导入方法依赖的package包/类
public byte[] serialize(Object data) throws java.io.IOException
{
    UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(10240);
    GenericObjectOutput objectOutput = new GenericObjectOutput(os);
    objectOutput.writeObject(data);
    objectOutput.flushBuffer();
    return os.toByteArray();
}
 
开发者ID:spccold,项目名称:dubbo-comments,代码行数:9,代码来源:Dubbo.java

示例14: encodeResponse

import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream; //导入方法依赖的package包/类
protected void encodeResponse(Channel channel, OutputStream os, Response res) throws IOException {
    try {
        Serialization serialization = CodecSupport.getSerialization(channel.getUrl());
        // header.
        byte[] header = new byte[HEADER_LENGTH];
        // set magic number.
        Bytes.short2bytes(MAGIC, header);
        // set request and serialization flag.
        header[2] = serialization.getContentTypeId();
        if (res.isHeartbeat()) header[2] |= FLAG_EVENT;
        // set response status.
        byte status = res.getStatus();
        header[3] = status;
        // set request id.
        Bytes.long2bytes(res.getId(), header, 4);

        UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(1024);
        ObjectOutput out = serialization.serialize(channel.getUrl(), bos);
        // encode response data or error message.
        if (status == Response.OK) {
            if (res.isHeartbeat()) {
                encodeHeartbeatData(channel, out, res.getResult());
            } else {
                encodeResponseData(channel, out, res.getResult());
            }
        } else out.writeUTF(res.getErrorMessage());
        out.flushBuffer();
        bos.flush();
        bos.close();

        byte[] data = bos.toByteArray();
        checkPayload(channel, data.length);
        Bytes.int2bytes(data.length, header, 12);
        // write
        os.write(header); // write header.
        os.write(data); // write data.
    } catch (Throwable t) {
        // 发送失败信息给Consumer,否则Consumer只能等超时了
        if (!res.isEvent() && res.getStatus() != Response.BAD_RESPONSE) {
            try {
                // FIXME 在Codec中打印出错日志?在IoHanndler的caught中统一处理?
                logger.warn("Fail to encode response: " + res + ", send bad_response info instead, cause: " + t.getMessage(), t);

                Response r = new Response(res.getId(), res.getVersion());
                r.setStatus(Response.BAD_RESPONSE);
                r.setErrorMessage("Failed to send response: " + res + ", cause: " + StringUtils.toString(t));
                channel.send(r);

                return;
            } catch (RemotingException e) {
                logger.warn("Failed to send bad_response info back: " + res + ", cause: " + e.getMessage(), e);
            }
        }

        // 重新抛出收到的异常
        if (t instanceof IOException) {
            throw (IOException) t;
        } else if (t instanceof RuntimeException) {
            throw (RuntimeException) t;
        } else if (t instanceof Error) {
            throw (Error) t;
        } else {
            throw new RuntimeException(t.getMessage(), t);
        }
    }
}
 
开发者ID:yunhaibin,项目名称:dubbox-hystrix,代码行数:67,代码来源:DeprecatedExchangeCodec.java


注:本文中的com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream.toByteArray方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。