java对象转成byte_java对象转成byte数组的三种⽅法import ioty.buffer.ByteBuf;
import ioty.buffer.Unpooled;
import flect.Field;
import flect.Method;
import java.nio.charset.Charset;
import java.util.List;
public class PayloadDecoder {
public static T resolve(byte[] src, Class clazz) {
T instance = null;
try {
instance = wInstance();
} catch (Exception e) {
throw new RuntimeException("实例化类失败", e);
}
List fieldWrapperList = FieldWrapperList();
ByteBuf buffer = Unpooled.buffer().writeBytes(src);
for (FieldWrapper fieldWrapper : fieldWrapperList) {
fillData(fieldWrapper, instance, buffer);
}
return instance;
}
private static void fillData(FieldWrapper fieldWrapper, Object instance, ByteBuf buffer) {
Field field = Field();
field.setAccessible(true);
String typeName = Type().getName();
try {
switch (typeName) {
case "java.lang.Boolean":
case "boolean":
boolean b = adBoolean();
field.set(instance, b);
break;java switch case string
case "java.lang.Character":
case "char":
CharSequence charSequence = CodecProprety().length(), Charset.forName("UTF-8"));
field.set(instance, charSequence);
break;
case "java.lang.Byte":
case "byte":
byte b1 = adByte();
field.set(instance, b1);
break;
case "java.lang.Short":
case "short":
short readShort = adShort();
field.set(instance, readShort);
break;
case "java.lang.Integer":
case "int":
int readInt = adInt();
field.set(instance, readInt);
break;
case "java.lang.Long":
case "long":
long l = adLong();
field.set(instance, l);
break;
case "java.lang.Float":
case "float":
float readFloat = adFloat();
field.set(instance, readFloat);
break;
case "java.lang.Double":
case "double":
double readDouble = adDouble();
field.set(instance, readDouble);
break;
case "java.lang.String":
String readString = CodecProprety().length(), Charset.forName("UTF-8")).toString();
field.set(instance, readString);
break;
default:
throw new RuntimeException(typeName + "不⽀持,bug");
}
} catch (Exception e) {
throw new RuntimeException(typeName + "读取失败,field:" + Name(), e);
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论