com.alibaba.dubbo.common.io.Bytes Java Examples

The following examples show how to use com.alibaba.dubbo.common.io.Bytes. 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: BuilderTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #2
Source File: BuilderTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@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");
}
 
Example #3
Source File: BuilderTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@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");
}
 
Example #4
Source File: BuilderTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@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");
}
 
Example #5
Source File: BuilderTest.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
@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");
}
 
Example #6
Source File: BuilderTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@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");
}
 
Example #7
Source File: BuilderTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #8
Source File: BuilderTest.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #9
Source File: BuilderTest.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #10
Source File: BuilderTest.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #11
Source File: BuilderTest.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #12
Source File: BuilderTest.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #13
Source File: ExchangeCodec.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
protected void encodeRequest(Channel channel, ChannelBuffer buffer, Request req) throws IOException {
    Serialization serialization = getSerialization(channel);
    // 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.
    int savedWriteIndex = buffer.writerIndex();
    buffer.writerIndex(savedWriteIndex + HEADER_LENGTH);
    ChannelBufferOutputStream bos = new ChannelBufferOutputStream(buffer);
    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();
    int len = bos.writtenBytes();
    checkPayload(channel, len);
    Bytes.int2bytes(len, header, 12);

    // write
    buffer.writerIndex(savedWriteIndex);
    buffer.writeBytes(header); // write header.
    buffer.writerIndex(savedWriteIndex + HEADER_LENGTH + len);
}
 
Example #14
Source File: DeprecatedExchangeCodec.java    From dubbox with Apache License 2.0 5 votes vote down vote up
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.
}
 
Example #15
Source File: ExchangeCodecTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void test_Decode_Check_Payload() throws IOException {
    byte[] header = new byte[]{MAGIC_HIGH, MAGIC_LOW, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
    byte[] request = assemblyDataProtocol(header);
    try {
        testDecode_assertEquals(request, TelnetCodec.DecodeResult.NEED_MORE_INPUT);
        fail();
    } catch (IOException expected) {
        Assert.assertTrue(expected.getMessage().startsWith("Data length too large: " + Bytes.bytes2int(new byte[]{1, 1, 1, 1})));
    }
}
 
Example #16
Source File: DeprecatedExchangeCodec.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
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.
}
 
Example #17
Source File: ExchangeCodecTest.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #18
Source File: BuilderTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #19
Source File: RandomAccessByteArrayOutputStream.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
public void write( int b ) {

        int newcount = count + 1;
        if ( newcount > buffer.length )
            buffer = Bytes.copyOf( buffer, Math.max( buffer.length << 1, newcount ) );
        buffer[count] = ( byte ) b;
        count = newcount;
    }
 
Example #20
Source File: BuilderTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #21
Source File: ExchangeCodecTest.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #22
Source File: BuilderTest.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #23
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 #24
Source File: BuilderTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #25
Source File: BuilderTest.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #26
Source File: BuilderTest.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #27
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 #28
Source File: BuilderTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #29
Source File: BuilderTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #30
Source File: RandomAccessByteArrayOutputStream.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
public void write( byte b[], int off, int len ) {

        if ( ( off < 0 ) || ( off > b.length ) || ( len < 0 ) || ( ( off + len ) > b.length ) || ( ( off + len ) < 0 ) )
            throw new IndexOutOfBoundsException();
        if ( len == 0 )
            return;
        int newcount = count + len;
        if ( newcount > buffer.length )
            buffer = Bytes.copyOf( buffer, Math.max( buffer.length << 1, newcount ) );
        System.arraycopy( b, off, buffer, count, len );
        count = newcount;
    }