#
vincent
2020-06-03 0972442009f05ffdf8ce44862260d880431c989e
src/main/java/com/zy/common/utils/Struct.java
@@ -13,8 +13,13 @@
import java.io.DataOutputStream;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.util.*;
/**
 * Tcp协议报文基类
 * @author vincent
 */
public class Struct implements java.io.Serializable {
   
   private transient Map<String,byte[]> decodeByteMapping = new HashMap<>();
@@ -27,17 +32,24 @@
   public Class getClassType(String name){
      return null;
   }
   /**
    * 判断大小端
    * @return true: 小端 / false: 大端
    */
   public boolean isReverse() {
      return this.getClass().getAnnotation(little.class)!=null;
   }
   /**
    * 获取字节数组
    */
   public byte[] toBytes() throws Exception {
      return encode(this);
   }
   
   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 +58,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());
@@ -76,11 +88,10 @@
    * 
    * @param dos
    * @param entity
    * @throws Exception
    */
   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 +324,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 +482,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 +491,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(); 
    }