From cbb17cf73f3d5edde489701a4401a36c8c1c6994 Mon Sep 17 00:00:00 2001 From: luxiaotao1123 <t1341870251@163.com> Date: 星期一, 14 九月 2020 12:53:55 +0800 Subject: [PATCH] # --- src/main/java/com/zy/common/utils/Struct.java | 28 ++++++++++++++++++++++------ 1 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/zy/common/utils/Struct.java b/src/main/java/com/zy/common/utils/Struct.java index 29baf5c..0ee0939 100644 --- a/src/main/java/com/zy/common/utils/Struct.java +++ b/src/main/java/com/zy/common/utils/Struct.java @@ -16,10 +16,15 @@ 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<>(); private transient Map<String,byte[]> encodeByteMapping = new HashMap<>(); + // 瑙g爜鍓嶇殑鍘熺敓瀛楄妭鏁扮粍 private transient byte[] dataBytes = null; private transient byte[] decodeBytes = null; private transient byte[] encodeBytes = null; @@ -28,11 +33,18 @@ 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); } @@ -77,7 +89,6 @@ * * @param dos * @param entity - * @throws Exception */ public void write(DataOutputStream dos, Struct entity) throws Exception { if(entity==null)return; @@ -237,6 +248,9 @@ return instance.decode(bytes); } + /** + * 瑙g爜锛� 瀛楄妭鏁扮粍 ====>> java瀵硅薄 + */ @SuppressWarnings({ "unchecked", "rawtypes" }) public <T extends Struct> T decode(byte[] bytes) throws Exception{ this.dataBytes = bytes; @@ -244,6 +258,7 @@ Class<? extends Struct> cls = this.getClass(); Field[] fields = cls.getFields(); if(this.decodeByteMapping==null)instance.decodeByteMapping=new HashMap(); + // spring el琛ㄨ揪寮� EvaluationContext context = new StandardEvaluationContext(); context.setVariable("_", bytes.length); int bited = 0;//宸茶浣嶆暟 @@ -252,9 +267,9 @@ bit bit = field.getAnnotation(bit.class); expr expr = field.getAnnotation(expr.class); flag flag = field.getAnnotation(flag.class); + // 娌℃湁size銆乥it銆乪xpr娉ㄨВ鐨勫彉閲忎笉杩涜瑙f瀽 if(size==null && bit==null && expr==null){ continue; - //throw new RuntimeException(cls.getSimpleName()+"瀛楁"+field.getName()+"鏈爣璁癅size|@bit|@expr"); } String name = field.getName(); Class<?> type = field.getType(); @@ -355,10 +370,12 @@ instance.unusedBytes = unusedBytes; return (T) instance; } + + // 瀛楄妭鏁扮粍 ===>> 杞负鍗佸叚杩涘埗瀛楃涓� public static String toHex(byte[] bytes) { StringBuilder buf = new StringBuilder(bytes.length * 2); for(byte b : bytes) { // 浣跨敤String鐨刦ormat鏂规硶杩涜杞崲 - buf.append(String.format("%02x", new Integer(b & 0xff))); + buf.append(String.format("%02x", b & 0xff)); } return buf.toString().toUpperCase(); } @@ -429,7 +446,6 @@ } return bbt; } - public static class TestMessage { public @size(1) byte begin = 0x7e;//璧峰绗� -- Gitblit v1.9.1