| | |
| | | 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 { |
| | |
| | | } |
| | | |
| | | 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)); |
| | |
| | | } |
| | | 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()); |
| | |
| | | */ |
| | | 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))) { |
| | |
| | | }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")){ |
| | |
| | | //字符串转换为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; |
| | |
| | | //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])); |
| | | for (byte aByte : bytes) { |
| | | sbu.append(Character.toString((char) aByte)); |
| | | } |
| | | return sbu.toString(); |
| | | } |