All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.yingzhuo.fastdfs.springboot.domain.proto.mapper.ParamMapperUtils Maven / Gradle / Ivy

The newest version!
package com.github.yingzhuo.fastdfs.springboot.domain.proto.mapper;

import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * param对象与byte映射器
 *
 * @author tobato
 */
public final class ParamMapperUtils {

    private static Map mapCache = new HashMap<>();

    private ParamMapperUtils() {
    }

    public static  T map(byte[] content, Class genericType, Charset charset) {
        // 获取映射对象
        ObjectMetaData objectMap = getObjectMap(genericType);

        try {
            return mapByIndex(content, genericType, objectMap, charset);
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
            throw new FastDFSColumnMappingException(e);
        }
    }

    public static  ObjectMetaData getObjectMap(Class genericType) {
        if (null == mapCache.get(genericType.getName())) {
            // 还未缓存过
            mapCache.put(genericType.getName(), new ObjectMetaData(genericType));
        }
        return mapCache.get(genericType.getName());
    }

    private static  T mapByIndex(byte[] content, Class genericType, ObjectMetaData objectMap, Charset charset)
            throws InstantiationException, IllegalAccessException, InvocationTargetException {

        List mappingFields = objectMap.getFieldList();
        T obj = genericType.newInstance();
        for (int i = 0; i < mappingFields.size(); i++) {
            FieldMetaData field = mappingFields.get(i);
            // 设置属性值
            BeanUtils.setProperty(obj, field.getFieldName(), field.getValue(content, charset));
        }
        return obj;
    }

    public static byte[] toByte(Object object, Charset charset) {
        ObjectMetaData objectMap = getObjectMap(object.getClass());
        try {
            return convertFieldToByte(objectMap, object, charset);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ie) {
            throw new FastDFSColumnMappingException(ie);
        }
    }

    private static byte[] convertFieldToByte(ObjectMetaData objectMap, Object object, Charset charset)
            throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        List mappingFields = objectMap.getFieldList();
        // 获取报文长度 (固定长度+动态长度)
        int size = objectMap.getFieldsSendTotalByteSize(object, charset);
        byte[] result = new byte[size];
        int offsize = 0;
        for (int i = 0; i < mappingFields.size(); i++) {
            FieldMetaData field = mappingFields.get(i);
            byte[] fieldByte = field.toByte(object, charset);
            if (null != fieldByte) {
                System.arraycopy(fieldByte, 0, result, offsize, fieldByte.length);
                offsize += fieldByte.length;
            }
        }
        return result;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy