Java Code Examples for com.baidu.bjf.remoting.protobuf.Codec#encode()

The following examples show how to use com.baidu.bjf.remoting.protobuf.Codec#encode() . 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: ProtobufMessageConverter.java    From common-project with Apache License 2.0 6 votes vote down vote up
@Override
public Message toMessage(Object obj, MessageProperties messageProperties) throws MessageConversionException {
    String messageType = obj.getClass().getSimpleName();
    Codec<Object> codec = codecMap.get(messageType);
    if (codec == null) {
        throw new MessageConversionException("不支持转换的消息类型:" + messageType);
    }
    messageProperties.setHeader("messageType", messageType);
    byte[] body;
    try {
        body = codec.encode(obj);
    } catch (Exception e) {
        throw new MessageConversionException("编码失败:" + messageType, e);
    }
    return new Message(body, messageProperties);
}
 
Example 2
Source File: JProtobufTest.java    From jforgame with Apache License 2.0 6 votes vote down vote up
@Test
public void testRequest() {
	ReqAccountLogin request = new ReqAccountLogin();
	request.setAccountId(123456L);
	request.setPassword("kingston");
	Codec<ReqAccountLogin> simpleTypeCodec = ProtobufProxy
			.create(ReqAccountLogin.class);
	try {
		// 序列化
		byte[] bb = simpleTypeCodec.encode(request);
		// 反序列化
		ReqAccountLogin request2 = simpleTypeCodec.decode(bb);
		Assert.assertTrue(request2.getAccountId() == request.getAccountId());
		Assert.assertTrue(request2.getPassword().equals(request.getPassword()));
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
Example 3
Source File: RequrieRepeatedNumberType3Test.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncodeDecode() {
    
    InterClassName icn = InterClassName.newBuilder().addList2(10000D).addList2(20000.1D).build();
    
    byte[] oldbb = icn.toByteArray();
    System.out.println(Arrays.toString(oldbb));
    
    
    RequrieRepeatedNumberTypePOJOClass3 type = new RequrieRepeatedNumberTypePOJOClass3();
    
    type.list2 = new ArrayList<Double>();
    type.list2.add(10000D);
    type.list2.add(20000.1D);
    
    
    Codec proxy = ProtobufProxy.create(RequrieRepeatedNumberTypePOJOClass3.class);
    try {
        byte[] bb = proxy.encode(type);
        System.out.println(Arrays.toString(bb));
        Assert.assertArrayEquals(oldbb, bb);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 4
Source File: AnyTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
/**
 * Test use any.
 * 
 * @throws IOException
 */
@Test
public void testDecodeUseOriginAny() throws IOException {

    StringTypePOJOClass pojo = new StringTypePOJOClass();
    pojo.setStr("hello world");

    com.baidu.bjf.remoting.protobuf.Any any =
            com.baidu.bjf.remoting.protobuf.Any.pack(pojo, StringMessage.class.getName());

    Codec<com.baidu.bjf.remoting.protobuf.Any> codec =
            ProtobufProxy.create(com.baidu.bjf.remoting.protobuf.Any.class);
    byte[] byteArray = codec.encode(any);

    AnyObject anyObject = AnyProtos.AnyObject.parseFrom(byteArray);

    List<Any> detailsList = anyObject.getDetailsList();

    for (Any any2 : detailsList) {
        if (any2.is(StringMessage.class)) {
            StringMessage unpack = any2.unpack(StringMessage.class);

            Assert.assertEquals(pojo.getStr(), unpack.getList());
        }
    }
}
 
Example 5
Source File: EnumClassTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnum2() throws IOException {

    Codec<EnumPOJOClass2> codec = ProtobufProxy.create(EnumPOJOClass2.class);
    EnumPOJOClass2 ec = new EnumPOJOClass2();
    ec.setEnumAttr(EnumAttrPOJO.INT);

    byte[] bytes = codec.encode(ec);
    EnumPOJOClass2 decode = codec.decode(bytes);
    Assert.assertEquals(EnumAttrPOJO.INT, decode.getEnumAttr());

    byte[] byteArray = EnumClassInternal.newBuilder()
            .setStatus(com.baidu.bjf.remoting.protobuf.enumeration.EnumClass.EnumAttr.INT).build().toByteArray();
    Assert.assertArrayEquals(bytes, byteArray);
    EnumClassInternal enumClass =
            com.baidu.bjf.remoting.protobuf.enumeration.EnumClass.EnumClassInternal.parseFrom(bytes);

    Assert.assertEquals(com.baidu.bjf.remoting.protobuf.enumeration.EnumClass.EnumAttr.INT, enumClass.getStatus());
}
 
Example 6
Source File: PackedValueTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void testIncludeEmptyList() {
    PackedProtosPOJO pojo = new PackedProtosPOJO();
    Codec<PackedProtosPOJO> codec = ProtobufProxy.create(PackedProtosPOJO.class);

    List<Integer> list = new ArrayList<Integer>(10000);
    int begin = Integer.MAX_VALUE - 10000;
    for (int i = 0; i < 1; i++) {
        list.add(i + begin);
    }
    pojo.setId(list);

    Builder b1 = Person.newBuilder();
    b1.addAllId(list);
    Person person = b1.build();


    try {
        byte[] byteArray1 = person.toByteArray();
        System.out.println(Arrays.toString(byteArray1));
        byte[] byteArray2 = codec.encode(pojo);
        System.out.println(Arrays.toString(byteArray2));
        Assert.assertArrayEquals(byteArray1, byteArray2);
        
        PackedProtosPOJO pojo2 = codec.decode(person.toByteArray());
        Assert.assertEquals(pojo.getId(), pojo2.getId());
        
        codec.decode(person.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 7
Source File: SubClassPOJOTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
/**
 * Test sub class POJO.
 */
@Test
public void testSubClassPOJO() {
    
    SubOne subOne = new SubOne();
    subOne.setSubOneName("stringOneName");
    subOne.setName("hello");
    subOne.setAge(100);
    
    ParentClassPOJO subClassPOJO = new ParentClassPOJO();
    subClassPOJO.add(subOne);
    
    
    Codec<ParentClassPOJO> codec = ProtobufProxy.create(ParentClassPOJO.class, false);
    
    Codec<SubClassPOJO> codec2 = ProtobufProxy.create(SubClassPOJO.class, false);
    try {
        byte[] encode = codec.encode(subClassPOJO);
        
        SubClassPOJO decode = codec2.decode(encode);
        
        Assert.assertEquals(subClassPOJO.parents, decode.parents);
    } catch (IOException e) {
        e.printStackTrace();
    }
    
}
 
Example 8
Source File: ByteTypeTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void testTypeClass3() throws IOException  {
    Codec<ByteTypeClass3> codec = ProtobufProxy.create(ByteTypeClass3.class);
    
    ByteTypeClass3 o = new ByteTypeClass3();
    o.setBytes(new byte[] {1, 2});
    byte[] bb = codec.encode(o);
    
    ByteTypeClass3 class1 = codec.decode(bb);
    Assert.assertArrayEquals(o.getBytes(), class1.getBytes());
}
 
Example 9
Source File: ByteTypeTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void testTypeClass2() throws IOException  {
    Codec<ByteTypeClass2> codec = ProtobufProxy.create(ByteTypeClass2.class);
    
    ByteTypeClass2 o = new ByteTypeClass2();
    o.bytes = new byte[] {1, 2};
    byte[] bb = codec.encode(o);
    
    ByteTypeClass2 class1 = codec.decode(bb);
    Assert.assertArrayEquals(o.bytes, class1.bytes);
}
 
Example 10
Source File: ByteTypeTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void testTypeClass1() throws IOException  {
    Codec<ByteTypeClass1> codec = ProtobufProxy.create(ByteTypeClass1.class);
    
    ByteTypeClass1 o = new ByteTypeClass1();
    byte[] bb = codec.encode(o);
    
    ByteTypeClass1 class1 = codec.decode(bb);
}
 
Example 11
Source File: RequrieRepeatedDojoClassWithLargeIndexTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
private byte[] decodeByJprotobuf() throws IOException {
    Codec codec = ProtobufProxy.create(RequrieRepeatedDojoClassWithLargeIndex.class);
    
    RequrieRepeatedDojoClassWithLargeIndex pojo = new RequrieRepeatedDojoClassWithLargeIndex();
    pojo.list = new ArrayList<String>(2);
    pojo.list.add("hello");
    pojo.list.add("world");
    
    System.out.println(codec.size(pojo));
    
    byte[] byteArray = codec.encode(pojo);
    
    return byteArray;
}
 
Example 12
Source File: DateTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
/**
 * Test date encode with orignal PB class.
 */
@Test
public void testDateEncodeWithOrignalPBClass() {

    long secs = System.currentTimeMillis() / 1000;
    int nanos = (int) (System.currentTimeMillis() % 1000) * 1000000;

    Timestamp ts = Timestamp.newBuilder().setSeconds(secs).setNanos(nanos).build();

    Person person = Person.newBuilder().setTs(ts).build();

    byte[] byteArray = person.toByteArray();

    String expected = Arrays.toString(byteArray);

    DatePOJO datePojo = new DatePOJO();
    com.baidu.bjf.remoting.protobuf.Timestamp ts2 = new com.baidu.bjf.remoting.protobuf.Timestamp();
    ts2.setSeconds(secs);
    ts2.setNanos(nanos);
    datePojo.setTimeStamp(ts2);

    Codec<DatePOJO> codec = ProtobufProxy.create(DatePOJO.class);
    try {
        byte[] encode = codec.encode(datePojo);
        String actual = Arrays.toString(encode);
        DatePOJO decode = codec.decode(byteArray);
        Assert.assertEquals(expected, actual);
        Assert.assertEquals(decode.getTimeStamp().getSeconds().longValue(), person.getTs().getSeconds());
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
}
 
Example 13
Source File: ByteTypeTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void testTypeClass1() throws IOException  {
    Codec codec = ProtobufProxy.create(ByteTypeClass1.class);
    
    ByteTypeClass1 o = new ByteTypeClass1();
    byte[] bb = codec.encode(o);
    
    ByteTypeClass1 class1 = (ByteTypeClass1) codec.decode(bb);
}
 
Example 14
Source File: RequrieRepeatedTypeTest.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
@Test
public void testEncodeListFields() throws IOException {
    
    
    Codec<RequrieRepeatedDojoClass> create = ProtobufProxy.create(RequrieRepeatedDojoClass.class);
    
    
    RequrieRepeatedDojoClass dc = new RequrieRepeatedDojoClass();
    dc.list = new ArrayList<String>();
    dc.list.add("abc");
    
    byte[] bb = create.encode(dc);
    
    InterClassName parseFrom = InterClassName.parseFrom(bb);
    List<String> listList = parseFrom.getListList();
    
    Assert.assertEquals(1, listList.size());
    Assert.assertEquals("abc", listList.get(0));
    
    
}
 
Example 15
Source File: RequrieRepeatedTypeTest.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
@Test
public void testEncodeListFields3() throws IOException {
    
    
    Codec<RequrieRepeatedDojoClass3> create = ProtobufProxy.create(RequrieRepeatedDojoClass3.class);
    
    
    RequrieRepeatedDojoClass3 dc = new RequrieRepeatedDojoClass3();
    dc.setList( new ArrayList<String>());
    dc.getList().add("abc");
    
    byte[] bb = create.encode(dc);
    
    InterClassName parseFrom = InterClassName.parseFrom(bb);
    List<String> listList = parseFrom.getListList();
    
    Assert.assertEquals(1, listList.size());
    Assert.assertEquals("abc", listList.get(0));
    
    
}
 
Example 16
Source File: PackedValueTest.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
/**
 * Test float value encode.
 */
@Test
public void testFloatValueEncode() {

    Builder b1 = Person.newBuilder();
    com.baidu.bjf.remoting.protobuf.packed.PackedProtosV2.Person.Builder b2 =
            com.baidu.bjf.remoting.protobuf.packed.PackedProtosV2.Person.newBuilder();

    PackedProtosPOJO pojo = new PackedProtosPOJO();
    PackedProtosPOJO2 pojo2 = new PackedProtosPOJO2();
    for (int i = 0; i < 10; i++) {
        b1.addId(i);
        b1.addName("name中文" + i);
        b1.addBoolF(i % 2 == 0);
        b1.addBytesF(ByteString.copyFrom(new byte[] { 'a', 'b', 'c' }));
        b1.addDoubleF(101.1d * i);
        b1.addEmail("xiemalin" + i + "@test.com");
        b1.addFloatF(102.1f * i);

        b2.addId(i);
        b2.addName("name中文" + i);
        b2.addBoolF(i % 2 == 0);
        b2.addBytesF(ByteString.copyFrom(new byte[] { 'a', 'b', 'c' }));
        b2.addDoubleF(101.1d * i);
        b2.addEmail("xiemalin" + i + "@test.com");
        b2.addFloatF(102.1f * i);

        pojo.getId().add(i);
        pojo.getName().add("name中文" + i);
        pojo.getBoolF().add(i % 2 == 0);
        pojo.getBytesF().add(new byte[] { 'a', 'b', 'c' });
        pojo.getDoubleF().add(101.1d * i);
        pojo.getEmail().add("xiemalin" + i + "@test.com");
        pojo.getFloatF().add(102.1f * i);

        pojo2.getId().add(i);
        pojo2.getName().add("name中文" + i);
        pojo2.getBoolF().add(i % 2 == 0);
        pojo2.getBytesF().add(new byte[] { 'a', 'b', 'c' });
        pojo2.getDoubleF().add(101.1d * i);
        pojo2.getEmail().add("xiemalin" + i + "@test.com");
        pojo2.getFloatF().add(102.1f * i);
    }
    Person person = b1.build();
    com.baidu.bjf.remoting.protobuf.packed.PackedProtosV2.Person person2;
    person2 = b2.build();
    Codec<PackedProtosPOJO> codec = ProtobufProxy.create(PackedProtosPOJO.class);
    Codec<PackedProtosPOJO2> codec2 = ProtobufProxy.create(PackedProtosPOJO2.class);

    try {
        byte[] bytes = codec.encode(pojo); // packed bytes
        Assert.assertArrayEquals(person.toByteArray(), bytes);

        byte[] bytes2 = codec2.encode(pojo2);
        Assert.assertArrayEquals(person2.toByteArray(), bytes2);

        PackedProtosPOJO pojoNew = codec.decode(bytes);
        Assert.assertEquals(pojo.getId(), pojoNew.getId());

    } catch (IOException e) {
        e.printStackTrace();
    }

}
 
Example 17
Source File: EnumClassTest.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
@Test
public void testEnum() throws IOException {
    
    
    Codec codec = ProtobufProxy.create(EnumPOJOClass.class);
    EnumPOJOClass ec = new EnumPOJOClass();
    ec.enumAttr = EnumAttrPOJO.INT;
    
    byte[] bytes = codec.encode(ec);
    
    EnumPOJOClass decode = (EnumPOJOClass) codec.decode(bytes);
    Assert.assertEquals(EnumAttrPOJO.INT, decode.enumAttr);
    
    byte[] byteArray = EnumClassInternal.newBuilder().setStatus(
            com.baidu.bjf.remoting.protobuf.enumeration.EnumClass.EnumAttr.INT).build().toByteArray();
    Assert.assertArrayEquals(bytes, byteArray);
    
    EnumClassInternal enumClass = com.baidu.bjf.remoting.protobuf.enumeration.EnumClass.EnumClassInternal.parseFrom(bytes);
 
    Assert.assertEquals(com.baidu.bjf.remoting.protobuf.enumeration.EnumClass.EnumAttr.INT, enumClass.getStatus());
}
 
Example 18
Source File: RequrieRepeatedTypeTest.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
@Test
public void testEncodeListFields() throws IOException {
    
    
    Codec create = ProtobufProxy.create(RequrieRepeatedDojoClass.class);
    
    
    RequrieRepeatedDojoClass dc = new RequrieRepeatedDojoClass();
    dc.list = new ArrayList<String>();
    dc.list.add("abc");
    
    byte[] bb = create.encode(dc);
    
    InterClassName parseFrom = InterClassName.parseFrom(bb);
    List<String> listList = parseFrom.getListList();
    
    Assert.assertEquals(1, listList.size());
    Assert.assertEquals("abc", listList.get(0));
    
    
}
 
Example 19
Source File: MapTypeIDLProxyTest.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
@Test
public void testMapWithEnum() throws IOException {
    
    Codec<EnumMapPOJO> codec = ProtobufProxy.create(EnumMapPOJO.class);
    
    EnumMapPOJO pojo = new EnumMapPOJO();
    Map<String, EnumAttrPOJO> map = new HashMap<String, EnumAttrPOJO>();
    map.put("abc", EnumAttrPOJO.STRING);
    pojo.setStringMap(map);
    
    
    byte[] bs = codec.encode(pojo);
    
    EnumMapPOJO pojo2 = codec.decode(bs);
    Assert.assertEquals(EnumAttrPOJO.STRING, pojo2.getStringMap().get("abc"));
}
 
Example 20
Source File: TestInnerRef.java    From jprotobuf with Apache License 2.0 3 votes vote down vote up
/**
 * Test inner ref class.
 */
@Test
public void testInnerRefClass() {

    System.out.println(TestInnerRef.Test1$$.class.getCanonicalName());

    Codec<TestInnerRef> codec = ProtobufProxy.create(TestInnerRef.class, false);

    TestInnerRef ref = new TestInnerRef();
    ref.test1 = new HashMap<Integer, TestInnerRef.Test1$$>();

    Test1$$ test1 = new Test1$$();
    test1.tast2 = new ArrayList<TestInnerRef.Test2>();

    Test2 test2 = new Test2();
    test2.name = 12;

    test1.tast2.add(test2);

    ref.test1.put(1, test1);

    try {
        byte[] bytes = codec.encode(ref);

        TestInnerRef decode = codec.decode(bytes);

        Assert.assertEquals(ref.test1.size(), decode.test1.size());

    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
}