10 HXCodec

2023-12-31 20:30:35

前言

这个主要是提供了日常的相关?编码解码体系 的工具包,?依赖于fastjson 和?netty-all

里面主要包含了一些?摘要的生成,?base64编码解码,?ByteBuf操作api, Des编码解码,?文件操作,?十六进制操作,?io操作,?md5操作,?属性操作,?rsa操作,?socket操作

对象?序列化为字节序列,?字节序列?反序列化为对象

https://github.com/970655147/HXCodec

这里核心关注的是?对象 和 字节序列 的相互转换, 以下为?基础类型的转换, 数组的转换,?集合的转换,?字符串的转换,?实体的转换,?Map 的转换?等等

BYTE 为有符号单字节

UNSIGNED_BYTE 为无符号单字节

WORD?为有符号双字节

UNSIGNED_?WORD?为无符号双字节

DWORD?为有符号四字节

QWORD?为有符号八字节

*_ARRAY 为对应类型的数组

*_COLLECTION 为对应类型的集合

*_WITH_LEN : 表示在编码集合之前会先编码长度

*_WITH_ELE_LEN : 指定元素数量,?如果数量不够,?补充?padding 字节到目标字节数?

*_WITH_FIXED_LEN : 指定编码之后的长度,?如果数量不够,?补充?padding 字节到目标字节数?

*_WITH_EXACTLY_LEN : 指定元素数量,?并确保元素数量?和 配置数量一致

B8421 相关,?指的是将目标字符串按照?B8421 进行编码?

CHARSET_ENCODING 相关指的是?字符串按照给定的编码集进行编码解码

GENERIC_BEAN 相关指的是,?按照给定的实体的字段属性配置 构建一个 BeanCodec 来处理当前实体的?编码解码

GENERIC_MAP 相关指的是,?按照给定的key, value属性配置 构建一个 MapCodec 来处理当前实体的?编码解码

// ------------------------------------------ basic types ------------------------------------------

BYTE,
UNSIGNED_BYTE,
WORD,
UNSIGNED_WORD,
DWORD,
QWORD,

// ------------------------------------------ array types ------------------------------------------

BYTE_ARRAY,
UNSIGNED_BYTE_ARRAY,
WORD_ARRAY,
UNSIGNED_WORD_ARRAY,
DWORD_ARRAY,
QWORD_ARRAY,
GENERIC_BEAN_ARRAY,

BYTE_ARRAY_WITH_LEN,
UNSIGNED_BYTE_ARRAY_WITH_LEN,
WORD_ARRAY_WITH_LEN,
UNSIGNED_WORD_ARRAY_WITH_LEN,
DWORD_ARRAY_WITH_LEN,
QWORD_ARRAY_WITH_LEN,
GENERIC_BEAN_ARRAY_WITH_LEN,

BYTE_ARRAY_WITH_ELE_LEN,
UNSIGNED_BYTE_ARRAY_WITH_ELE_LEN,
WORD_ARRAY_WITH_ELE_LEN,
UNSIGNED_WORD_ARRAY_WITH_ELE_LEN,
DWORD_ARRAY_WITH_ELE_LEN,
QWORD_ARRAY_WITH_ELE_LEN,
GENERIC_BEAN_ARRAY_WITH_ELE_LEN,

BYTE_ARRAY_WITH_FIXED_LEN,
UNSIGNED_BYTE_ARRAY_WITH_FIXED_LEN,
WORD_ARRAY_WITH_FIXED_LEN,
UNSIGNED_WORD_ARRAY_WITH_FIXED_LEN,
DWORD_ARRAY_WITH_FIXED_LEN,
QWORD_ARRAY_WITH_FIXED_LEN,
GENERIC_BEAN_ARRAY_WITH_FIXED_LEN,

BYTE_ARRAY_WITH_EXACTLY_LEN,
UNSIGNED_BYTE_ARRAY_WITH_EXACTLY_LEN,
WORD_ARRAY_WITH_EXACTLY_LEN,
UNSIGNED_WORD_ARRAY_WITH_EXACTLY_LEN,
DWORD_ARRAY_WITH_EXACTLY_LEN,
QWORD_ARRAY_WITH_EXACTLY_LEN,
GENERIC_BEAN_ARRAY_WITH_EXACTLY_LEN,

// ------------------------------------------ collection types ------------------------------------------

BYTE_COLLECTION,
UNSIGNED_BYTE_COLLECTION,
WORD_COLLECTION,
UNSIGNED_WORD_COLLECTION,
DWORD_COLLECTION,
QWORD_COLLECTION,
GENERIC_BEAN_COLLECTION,
SCHEMA_REGISTRY_BASED_COLLECTION,

BYTE_COLLECTION_WITH_LEN,
UNSIGNED_BYTE_COLLECTION_WITH_LEN,
WORD_COLLECTION_WITH_LEN,
UNSIGNED_WORD_COLLECTION_WITH_LEN,
DWORD_COLLECTION_WITH_LEN,
QWORD_COLLECTION_WITH_LEN,
GENERIC_BEAN_COLLECTION_WITH_LEN,
SCHEMA_REGISTRY_BASED_COLLECTION_WITH_LEN,

BYTE_COLLECTION_WITH_ELE_LEN,
UNSIGNED_BYTE_COLLECTION_WITH_ELE_LEN,
WORD_COLLECTION_WITH_ELE_LEN,
UNSIGNED_WORD_COLLECTION_WITH_ELE_LEN,
DWORD_COLLECTION_WITH_ELE_LEN,
QWORD_COLLECTION_WITH_ELE_LEN,
GENERIC_BEAN_COLLECTION_WITH_ELE_LEN,

BYTE_COLLECTION_WITH_FIXED_LEN,
UNSIGNED_BYTE_COLLECTION_WITH_FIXED_LEN,
WORD_COLLECTION_WITH_FIXED_LEN,
UNSIGNED_WORD_COLLECTION_WITH_FIXED_LEN,
DWORD_COLLECTION_WITH_FIXED_LEN,
QWORD_COLLECTION_WITH_FIXED_LEN,
GENERIC_BEAN_COLLECTION_WITH_FIXED_LEN,

BYTE_COLLECTION_WITH_EXACTLY_LEN,
UNSIGNED_BYTE_COLLECTION_WITH_EXACTLY_LEN,
WORD_COLLECTION_WITH_EXACTLY_LEN,
UNSIGNED_WORD_COLLECTION_WITH_EXACTLY_LEN,
DWORD_COLLECTION_WITH_EXACTLY_LEN,
QWORD_COLLECTION_WITH_EXACTLY_LEN,
GENERIC_BEAN_COLLECTION_WITH_EXACTLY_LEN,
SCHEMA_REGISTRY_BASED_COLLECTION_WITH_EXACTLY_LEN,

// ------------------------------------------ string types ------------------------------------------

BCD_8421_STRING,
BCD_8421_STRING_WITH_LEN,
BCD_8421_STRING_WITH_FIXED_LEN,
BCD_8421_STRING_WITH_EXACTLY_LEN,
BCD_8421_BIG_DECIMAL_WITH_ONE_BYTE_LEN,
CHARSET_ENCODING_STRING,
CHARSET_ENCODING_WITH_LEN_STRING,
CHARSET_ENCODING_WITH_FIXED_LEN_STRING,

// ------------------------------------------ entity types ------------------------------------------

GENERIC_BEAN,
CUSTOM_BEAN,

// ------------------------------------------ map types ------------------------------------------

GENERIC_MAP,
GENERIC_MAP_WITH_LEN,
GENERIC_MAP_WITH_EXACTLY_LEN,
SCHEMA_REGISTRY_BASED_MAP,
SCHEMA_REGISTRY_BASED_MAP_WITH_LEN,
SCHEMA_REGISTRY_BASED_MAP_WITH_EXACTLY_LEN,

// ------------------------------------------ other types ------------------------------------------

PADDING_BYTE,
PADDING_BYTES,

测试用例

一个简单的测试用例如下?

/**
 * Test14GenericBeanCodec
 *
 * @author Jerry.X.He
 * @version 1.0
 * @date 2021/9/28 14:31
 */
@FixMethodOrder(value = MethodSorters.NAME_ASCENDING)
public class Test14GenericBeanCodec extends Test00BaseTests {

    @Test
    public void test01UpConnectReq() {
        GenericBeanSchema<UpConnectReq> beanSchema = new GenericBeanSchema<>(UpConnectReq.class, 2019);
        GenericBeanCodec<UpConnectReq> codec = new GenericBeanCodec<>(beanSchema);

        UpConnectReq entity = new UpConnectReq();
        entity.setDummyField("dummyField");
        entity.setUserid(0x1234);
        entity.setPassword("pass");
        entity.setDownLinkIp("127.0.0.2");
        entity.setDownLinkPort(0x7788);
        entity.setMsgGnsscenterid(0x1111);

        ByteBuf encodedBuf = Unpooled.buffer(128);
        codec.encode(entity, encodedBuf);
        String encodedBufHexStr = ByteBufUtil.hexDump(encodedBuf);
        UpConnectReq decodedEntity = codec.decode(encodedBuf);

        LOGGER.info(" encodedBufHexStr : " + encodedBufHexStr);
        AssertUtils.state(encodedBufHexStr.equals("07070707070707070707000012347061737300000000000011113132372e302e302e3200000000000000000000000000000000000000000000007788"), " unexpected value ");
        AssertUtils.state(entity.getUserid().equals(decodedEntity.getUserid()), " unexpected value ");
        AssertUtils.state(entity.getPassword().equals(decodedEntity.getPassword()), " unexpected value ");
        AssertUtils.state(entity.getDownLinkIp().equals(decodedEntity.getDownLinkIp()), " unexpected value ");
        AssertUtils.state(entity.getDownLinkPort().equals(decodedEntity.getDownLinkPort()), " unexpected value ");
        AssertUtils.state(entity.getMsgGnsscenterid().equals(decodedEntity.getMsgGnsscenterid()), " unexpected value ");

    }

    @Test
    public void test02UpExgMsgRealLocation() {
        GenericBeanSchema<Jt809Header> headerBeanSchema = new GenericBeanSchema<>(Jt809Header.class, 2019);
        GenericBeanCodec<Jt809Header> headerCodec = new GenericBeanCodec<>(headerBeanSchema);
        GenericBeanSchema<UpExgMsgRealLocation> entityBeanSchema = new GenericBeanSchema<>(UpExgMsgRealLocation.class, 2019);
        GenericBeanCodec<UpExgMsgRealLocation> entityCodec = new GenericBeanCodec<>(entityBeanSchema);
        GenericBeanSchema<GnssDataItem> gnssDataItemBeanSchema = new GenericBeanSchema<>(GnssDataItem.class, 2019);
        GenericBeanCodec<GnssDataItem> gnssDataItemCodec = new GenericBeanCodec<>(gnssDataItemBeanSchema);

        Jt809Header header = new Jt809Header();
        header.setMsgLength(98);
        header.setMsgSn(0x1);
        header.setMsgId(0x2);
        header.setMsgGNSSCenterId(0x3);
        header.setVersionFlag(new Integer[]{5, 0, 0});
        header.setEncryptFlag(0x0);
        header.setEncryptKey(0x5);
        header.setTime(0x6);
        header.setMsgId(0x4100);

        UpExgMsgRealLocation entity = new UpExgMsgRealLocation();
        entity.setVehicleNo("1");
        entity.setVehicleColor(0x01);
        GnssDataItem gnssDataItem = new GnssDataItem(3, new Integer[]{0x1, 0x02, 0x03, 0x04}, new Integer[]{0x1, 0x02, 0x03},
                4, 5, 6, 7, 8, 9, 10, 11, 12);
        entity.setGnssDataItem(gnssDataItem);
        entity.setSubDataType(0x4102);
        entity.setSubDataLength(gnssDataItemCodec.length());

        ByteBuf encodedBuf = Unpooled.buffer(128);
        headerCodec.encode(header, encodedBuf);
        entityCodec.encode(entity, encodedBuf);
        String encodedBufHexStr = ByteBufUtil.hexDump(encodedBuf);
        Jt809Header decodedHeader = headerCodec.decode(encodedBuf);
        UpExgMsgRealLocation decodedEntity = entityCodec.decode(encodedBuf);

        LOGGER.info(" encodedBufHexStr : " + encodedBufHexStr);
        AssertUtils.state(encodedBufHexStr.equals("000000620000000141000000000305000000000000050000000000000006310000000000000000000000000000000000000000014102000000240301020304010203000000040000000500060007000000080009000a0000000b0000000c"), " unexpected value ");
        AssertUtils.state(header.getMsgLength().equals(decodedHeader.getMsgLength()), " unexpected value ");
        AssertUtils.state(header.getMsgSn().equals(decodedHeader.getMsgSn()), " unexpected value ");
        AssertUtils.state(header.getMsgId().equals(decodedHeader.getMsgId()), " unexpected value ");
        AssertUtils.state(header.getMsgGNSSCenterId().equals(decodedHeader.getMsgGNSSCenterId()), " unexpected value ");
        AssertUtils.state(header.getEncryptFlag().equals(decodedHeader.getEncryptFlag()), " unexpected value ");
        AssertUtils.state(header.getEncryptKey().equals(decodedHeader.getEncryptKey()), " unexpected value ");
        AssertUtils.state(entity.getVehicleNo().equals(decodedEntity.getVehicleNo()), " unexpected value ");
        AssertUtils.state(entity.getVehicleColor().equals(decodedEntity.getVehicleColor()), " unexpected value ");
        AssertUtils.state(entity.getSubDataType().equals(decodedEntity.getSubDataType()), " unexpected value ");
        AssertUtils.state(entity.getSubDataLength().equals(decodedEntity.getSubDataLength()), " unexpected value ");
        AssertUtils.state(entity.getGnssDataItem().getEncrypt().equals(decodedEntity.getGnssDataItem().getEncrypt()), " unexpected value ");
        AssertUtils.state(entity.getGnssDataItem().getLon().equals(decodedEntity.getGnssDataItem().getLon()), " unexpected value ");
        AssertUtils.state(entity.getGnssDataItem().getLat().equals(decodedEntity.getGnssDataItem().getLat()), " unexpected value ");
        AssertUtils.state(entity.getGnssDataItem().getVec1().equals(decodedEntity.getGnssDataItem().getVec1()), " unexpected value ");
        AssertUtils.state(entity.getGnssDataItem().getVec2().equals(decodedEntity.getGnssDataItem().getVec2()), " unexpected value ");
        AssertUtils.state(entity.getGnssDataItem().getVec3().equals(decodedEntity.getGnssDataItem().getVec3()), " unexpected value ");
        AssertUtils.state(entity.getGnssDataItem().getDirection().equals(decodedEntity.getGnssDataItem().getDirection()), " unexpected value ");
        AssertUtils.state(entity.getGnssDataItem().getAltitude().equals(decodedEntity.getGnssDataItem().getAltitude()), " unexpected value ");
        AssertUtils.state(entity.getGnssDataItem().getState().equals(decodedEntity.getGnssDataItem().getState()), " unexpected value ");
        AssertUtils.state(entity.getGnssDataItem().getAlarm().equals(decodedEntity.getGnssDataItem().getAlarm()), " unexpected value ");

    }

}

文章来源:https://blog.csdn.net/u011039332/article/details/129900976
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。