Tools that
Binary data processing tools, such as Socket and Bluetooth, can convert all field values in Object into binary values, and then put them together to form a binary packet, and can fill the binary data stream into the entity Object, forming a set of tools for serialization and deserialization
-
Support byte, char(Charactor), short(short), INT (Integer), long(long) and other types, the above basic data type into binary is directly expressed binary data, such as int is 4 bits that; [] results = {0, 0, 0, 1}
-
The String type is supported. When converted to binary data, the String type is expressed as the binary length of the String + binary data of the String, for example, String values = “ab”. Byte [] result = {0, 2, 97, 98}; The first two characters indicate that the string length is 2 and the content is 97 and 98.
-
Support for custom object types. Because custom data types are special, they can be divided into two forms
- If each field in the object is guaranteed not to be empty, one byte is not needed to identify whether the custom object is empty.
- If a custom object is likely to be empty, use 1 byte to indicate whether the object is likely to be empty. In this way, you can save traffic. You don’t need to flush content for each object, just fill it as needed
The use case words below illustrate both cases and provide interfaces to set which way to handle them
- Support the List
type, the generics in List support all types 1, 2, and 3 above
Using the example
TestModel = new TestModel(); // Basic data type testModel.setNoserialize ("111"); testModel.setTestChar('1'); testModel.setTestInteger(100); testModel.setTestLong(100l); testModel.setTestShort((short)12); testModel.setTestString("aaaaaxa"); TestModel2 testModel3 = new TestModel2(); testModel3.setValue(100); testModel.setTestModel2(testModel3); Testmodel.getintegerlist ().add(1); testModel.getIntegerList().add(2); Testmodel.getstringlist ().add("123"); testModel.getStringList().add("456"); TestModel2 TestModel2 = new TestModel2(); testModel2.setValue(100); testModel.getTestModel2List().add(testModel2); testModel.getTestModel2List().add(testModel2); Byte [] resultBytes = Packageutil. object2Byte(testModel); TestModel serializeObject = Packageutil.byte2Object (testModel.class, new BinaryBuffer(resultBytes)); Assert.assertEquals(testModel.getTestChar(), serializeObject.getTestChar()); Assert.assertEquals(testModel.getTestInteger(), serializeObject.getTestInteger()); Assert.assertEquals(testModel.getTestLong(), serializeObject.getTestLong()); Assert.assertEquals(testModel.getTestString(), serializeObject.getTestString()); Assert.assertEquals(testModel.getTestShort(), serializeObject.getTestShort()); @fieldinfo (order = 1) private TestModel2 TestModel2 = new TestModel2(); @FieldInfo(order = 10) private String testString; @FieldInfo(order = 20) private short testShort; @FieldInfo(order = 30) private char testChar; @FieldInfo(order = 50) private long testLong; Public String noSerialize; // All unidentified fields will not be serialized. @FieldInfo(order = 71) private List<Integer> integerList = new ArrayList<>(); @FieldInfo(order = 81) private List<String> stringList = new ArrayList<>(); @FieldInfo(order = 91) private List<Long> longArrayList = new ArrayList<>(); @FieldInfo(order = 101) private List<TestModel2> testModel2List = new ArrayList<>(); @FieldInfo(order = 101) private int testInteger; . Public class TestModel2 {@fieldinfo (order = 1) private int value; public class TestModel2 {@fieldinfo (order = 1) private int value; . Omit get and set methods}
Copy the code
Here is the object data before serialization:
Note that TestModel2 does not specify whether TestModel2 is null or not. The following shows how the user can enable the support for null
Here is a screenshot of the result of serializing binary data into objects
The following is the case where the object is empty. It is recommended that users use only one object, because there can be no dynamic configuration during network data transmission, unless users make their own transmission protocols
Public static void onOpenSupportNullObject() {ByteUtil.LENGTH_OF_BYTE = 1; ByteUtil.NULL_BYTE = new byte[]{0}; Byteutil. NOT_NULL_BYTE = new byte[]{1}; } public static void onCloseSupportNullObject() {ByteUtil.LENGTH_OF_BYTE = 0; ByteUtil.NULL_BYTE = new byte[]{}; // 1 indicates that the object is not null byteutil. NOT_NULL_BYTE = new byte[]{}; }
Copy the code
Table 1 indicates that TestModel is not null, and table 1 indicates that TestModel2 is not null
Here is a screenshot of the result of serializing binary data into objects