From f96a2246074ec51d0930836390fa6e3433141d28 Mon Sep 17 00:00:00 2001
From: luxiaotao1123 <t1341870251@63.com>
Date: 星期一, 22 八月 2022 15:17:51 +0800
Subject: [PATCH] #
---
src/main/java/com/zy/common/utils/Struct.java | 49 +++++++++++++++++++++++++++++++++----------------
1 files changed, 33 insertions(+), 16 deletions(-)
diff --git a/src/main/java/com/zy/common/utils/Struct.java b/src/main/java/com/zy/common/utils/Struct.java
index 5f53e28..0ee0939 100644
--- a/src/main/java/com/zy/common/utils/Struct.java
+++ b/src/main/java/com/zy/common/utils/Struct.java
@@ -13,12 +13,18 @@
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<>();
private transient Map<String,byte[]> encodeByteMapping = new HashMap<>();
+ // 瑙g爜鍓嶇殑鍘熺敓瀛楄妭鏁扮粍
private transient byte[] dataBytes = null;
private transient byte[] decodeBytes = null;
private transient byte[] encodeBytes = null;
@@ -27,17 +33,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 +59,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 +89,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))) {
@@ -236,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;
@@ -243,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;//宸茶浣嶆暟
@@ -251,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();
@@ -313,7 +329,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")){
@@ -354,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 +447,6 @@
return bbt;
}
-
public static class TestMessage {
public @size(1) byte begin = 0x7e;//璧峰绗�
public @size(2) short type = 0;//娑堟伅ID
@@ -471,7 +488,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 +497,10 @@
//ascii杞崲涓簊tring
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();
}
--
Gitblit v1.9.1