#
vincent
2020-06-03 eb01c925f7de641e0b02ddeb66b7af97c21ea426
#
1个文件已修改
21 ■■■■ 已修改文件
src/main/java/com/zy/common/utils/Struct.java 21 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/common/utils/Struct.java
@@ -13,6 +13,7 @@
import java.io.DataOutputStream;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.util.*;
public class Struct implements java.io.Serializable {
@@ -37,7 +38,7 @@
    }
    
    public Map<String,String> toDecodeHexMapping() throws Exception{
        if(this.decodeByteMapping==null)this.decodeByteMapping=new HashMap();
        if(this.decodeByteMapping==null)this.decodeByteMapping=new HashMap<>();
        Map<String, String> mapping = getHex(this.decodeByteMapping);
        if(this.decodeBytes!=null){
            mapping.put("byte[]", Struct.toHex(this.decodeBytes));
@@ -46,13 +47,13 @@
    }
    public Map<String,String> toEncodeHexMapping() throws Exception{
        if(this.encodeBytes==null)this.encode(this);
        if(this.encodeByteMapping==null)this.encodeByteMapping=new HashMap();
        if(this.encodeByteMapping==null)this.encodeByteMapping=new HashMap<>();
        Map<String, String> mapping = getHex(this.encodeByteMapping);
        mapping.put("byte[]", Struct.toHex(this.encodeBytes));
        return mapping;
    }
    public Map<String,String> getHex(Map<String,byte[]> bytemapping) throws Exception {
        Map<String,String> map = new LinkedHashMap();
        Map<String,String> map = new LinkedHashMap<>();
        Field[] fields = this.getClass().getFields();
        for (Field field : fields) {
            byte[] bytes = bytemapping.get(field.getName());
@@ -80,7 +81,7 @@
     */
    public void write(DataOutputStream dos, Struct entity) throws Exception {
        if(entity==null)return;
        if(entity.encodeByteMapping==null)entity.encodeByteMapping=new HashMap();
        if(entity.encodeByteMapping==null)entity.encodeByteMapping=new HashMap<>();
        Field[] fields = entity.getClass().getFields();
        EvaluationContext context = new StandardEvaluationContext();
        for (Field field : Objects.requireNonNull(ReflectUtils.removeStaticField(fields))) {
@@ -313,7 +314,7 @@
            }else if (typeName.equals("byte[]")){
                field.set(instance, bts);
            }else if (typeName.equals("String")) {
                String strStr = new String(bts,"UTF-8");
                String strStr = new String(bts, StandardCharsets.UTF_8);
                if(flag!=null && flag.value().equals("BCD")){
                    strStr = bcd2Str(bts);
                }else if(flag!=null && flag.value().equals("ASCII")){
@@ -471,7 +472,7 @@
  //字符串转换为ascii
    public static byte[] StrToAsc(String content){
      try {
        return content.getBytes("US-ASCII");
        return content.getBytes(StandardCharsets.US_ASCII);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
@@ -480,10 +481,10 @@
   
    //ascii转换为string
    public static String AscToStr(byte[] bytes){
        StringBuffer sbu = new StringBuffer();
        for(int i=0;i<bytes.length;i++){
            sbu.append(Character.toString((char)bytes[i]));
        }
        StringBuffer sbu = new StringBuffer();
        for (byte aByte : bytes) {
            sbu.append(Character.toString((char) aByte));
        }
        return sbu.toString(); 
    }