Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterChangRay committed Oct 10, 2024
2 parents bdbf77e + e925468 commit 3e4ca92
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 33 deletions.
12 changes: 10 additions & 2 deletions src/main/java/com/github/misterchangray/core/Packer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import com.github.misterchangray.core.clazz.ClassMetaInfo;
import com.github.misterchangray.core.clazz.FieldMetaInfo;
import com.github.misterchangray.core.clazz.MessageManager;
import com.github.misterchangray.core.enums.ByteOrder;
import com.github.misterchangray.core.exception.InvalidCheckCodeException;
import com.github.misterchangray.core.exception.InvalidLengthException;
import com.github.misterchangray.core.exception.MagicByteException;
import com.github.misterchangray.core.exception.MagicParseException;
import com.github.misterchangray.core.util.ExceptionUtil;
import com.github.misterchangray.core.util.ConverterUtil;
import com.github.misterchangray.core.util.DynamicByteBuffer;
import com.github.misterchangray.core.util.ExceptionUtil;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
Expand Down Expand Up @@ -54,7 +55,7 @@ public <T> T packObject(DynamicByteBuffer data, Class<?> clazz, MagicChecker ch


public <T> T doPackObject(DynamicByteBuffer data, ClassMetaInfo classMetaInfo, MagicChecker checker) throws MagicByteException {
data.order(classMetaInfo.getByteOrder());
// data.order(classMetaInfo.getByteOrder());
T object = null;

try {
Expand All @@ -66,6 +67,13 @@ public <T> T doPackObject(DynamicByteBuffer data, ClassMetaInfo classMetaInfo,
object = (T) classMetaInfo.getClazz().getDeclaredConstructor().newInstance();
data.setPackObj(object);
for (FieldMetaInfo fieldMetaInfo : classMetaInfo.getFields()) {
// 如果对属性额外配置了端序则以属性使用的端序为准,否则以全局/类配置的为准
if (fieldMetaInfo.getByteOrder() == ByteOrder.AUTO) {
data.order(classMetaInfo.getByteOrder());
} else {
data.order(fieldMetaInfo.getByteOrder().getBytes());
}

encodeField(object, fieldMetaInfo, data, checker);
}
} catch (MagicParseException ae) {
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/github/misterchangray/core/UnPacker.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import com.github.misterchangray.core.clazz.ClassManager;
import com.github.misterchangray.core.clazz.ClassMetaInfo;
import com.github.misterchangray.core.clazz.FieldMetaInfo;
import com.github.misterchangray.core.enums.ByteOrder;
import com.github.misterchangray.core.exception.MagicParseException;
import com.github.misterchangray.core.util.ExceptionUtil;
import com.github.misterchangray.core.util.DynamicByteBuffer;
import com.github.misterchangray.core.util.ExceptionUtil;

import java.util.Objects;

Expand Down Expand Up @@ -40,6 +41,7 @@ public <T> DynamicByteBuffer unpackObject(T object, MagicChecker checker) {
if(classMetaInfo.isDynamic()) {
res = DynamicByteBuffer.allocate().order(classMetaInfo.getByteOrder());
} else {

res = DynamicByteBuffer.allocate(classMetaInfo.getElementBytes()).order(classMetaInfo.getByteOrder());
}
res.setPackObj(object);
Expand All @@ -63,6 +65,13 @@ public <T> DynamicByteBuffer unpackObject(DynamicByteBuffer res, T object, Clas
}

for(FieldMetaInfo fieldMetaInfo : classMetaInfo.getFields()) {
// 如果对属性额外配置了端序则以属性使用的端序为准,否则以全局/类配置的为准
if (fieldMetaInfo.getByteOrder() == ByteOrder.AUTO) {
res.order(classMetaInfo.getByteOrder());
} else {
res.order(fieldMetaInfo.getByteOrder().getBytes());
}

decodeField(fieldMetaInfo, object, res, root);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.misterchangray.core.annotation;

import com.github.misterchangray.core.enums.ByteOrder;
import com.github.misterchangray.core.enums.TimestampFormatter;

import java.lang.annotation.*;
Expand Down Expand Up @@ -94,4 +95,11 @@
*/
String formatPattern() default "yyyyMMddHHmmss";

/**
* 字节序
*
* @return 默认返回 AUTO,应用全局配置,如果类配置了使用类配置的端序
*/
ByteOrder byteOrder() default ByteOrder.AUTO;

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.misterchangray.core.clazz;

import com.github.misterchangray.core.annotation.MagicField;
import com.github.misterchangray.core.enums.ByteOrder;
import com.github.misterchangray.core.enums.TimestampFormatter;
import com.github.misterchangray.core.enums.TypeEnum;
import com.github.misterchangray.core.exception.InvalidParameterException;
Expand Down Expand Up @@ -156,6 +157,13 @@ public class FieldMetaInfo implements MField {
*/
private boolean hasCustomConverter;

/**
* 字节序
*
* <p> 注:默认返回 AUTO,应用全局配置,如果类配置了使用类配置的端序
*/
private ByteOrder byteOrder;

public boolean isHasCustomConverter() {
return hasCustomConverter;
}
Expand Down Expand Up @@ -394,6 +402,14 @@ public void setOrderId(int orderId) {
this.orderId = orderId;
}

public ByteOrder getByteOrder() {
return byteOrder;
}

public void setByteOrder(ByteOrder byteOrder) {
this.byteOrder = byteOrder;
}

@Override
public MReader getReader() {
return this.reader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import com.github.misterchangray.core.annotation.MagicConverter;
import com.github.misterchangray.core.annotation.MagicField;
import com.github.misterchangray.core.enums.TypeEnum;
import com.github.misterchangray.core.exception.*;
import com.github.misterchangray.core.exception.InvalidParameterException;
import com.github.misterchangray.core.exception.InvalidTypeException;
import com.github.misterchangray.core.exception.MagicParseException;
import com.github.misterchangray.core.intf.MConverter;
import com.github.misterchangray.core.util.AnnotationUtil;
import com.github.misterchangray.core.util.ExceptionUtil;
Expand Down Expand Up @@ -278,6 +280,7 @@ private void copyConfiguration(Field field, FieldMetaInfo fieldMetaInfo, ClassMe
fieldMetaInfo.setSize(magicField.size());
fieldMetaInfo.setDynamicSizeOf(magicField.dynamicSizeOf());
fieldMetaInfo.setCmdField(magicField.cmdField());
fieldMetaInfo.setByteOrder(magicField.byteOrder());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Object doReadFormBuffer(DynamicByteBuffer buffer, Object entity) throws I

}
} else {
timestamp = ConverterUtil.byteToNumber(tmp);
timestamp = ConverterUtil.byteToNumber(tmp, buffer.getOrder());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.github.misterchangray.core.util.DateUtil;
import com.github.misterchangray.core.util.DynamicByteBuffer;

import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.time.*;
import java.util.Arrays;
Expand Down Expand Up @@ -55,7 +56,7 @@ public void doWriteToBuffer(DynamicByteBuffer buffer, Object val, Object parent)
}
timestamp = DateUtil.timestampConvert(timestamp, TimestampFormatter.TO_TIMESTAMP_MILLIS, fieldMetaInfo.getTimestampFormatter());
}
byte[] res = new byte[0];
byte[] res;
if(this.fieldMetaInfo.getTimestampFormatter() == TimestampFormatter.TO_TIMESTAMP_STRING ){
if(clazz.isAssignableFrom(LocalTime.class)){
res = DateUtil.localTimeToTxt(timestamp, this.fieldMetaInfo.getFormatPattern()).getBytes(StandardCharsets.UTF_8);
Expand All @@ -64,11 +65,18 @@ public void doWriteToBuffer(DynamicByteBuffer buffer, Object val, Object parent)

}
} else {
res = ConverterUtil.numberToByte(timestamp);

res = ConverterUtil.numberToByte(timestamp, buffer.getOrder());
}
for (int i = data.length - 1, j=res.length - 1; i>=0 & j>=0; i--, j--) {
data[i] = res[j];

// 如果是大端序,从后往前赋值,在数据前方补零;小端序从前往后赋值,在数据后方补零
if (buffer.getOrder() == ByteOrder.BIG_ENDIAN) {
for (int i = data.length - 1, j = res.length - 1; i >= 0 & j >= 0; i--, j--) {
data[i] = res[j];
}
} else {
for (int i = 0, j = 0; i < data.length & j < res.length; i++, j++) {
data[i] = res[j];
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.github.misterchangray.core.util;

import com.github.misterchangray.core.clazz.warpper.*;
import com.github.misterchangray.core.clazz.GlobalConfigs;
import com.github.misterchangray.core.clazz.warpper.UByte;
import com.github.misterchangray.core.clazz.warpper.UInt;
import com.github.misterchangray.core.clazz.warpper.ULong;
import com.github.misterchangray.core.clazz.warpper.UShort;
import com.github.misterchangray.core.enums.TypeEnum;
import com.github.misterchangray.core.exception.MagicByteException;

import java.lang.reflect.Type;
import java.math.BigInteger;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -18,42 +22,82 @@ public class ConverterUtil {
* 将无符号数转为字节
* 负数则会先转为无符号数再转为字节
*
* @param p
* @return
* @param p
* @return 对应的二进制
*/
public static byte[] numberToByte(long p) {
return numberToByte(p, GlobalConfigs.getGlobalDefaultByteOrder().getBytes());
}

/**
* 将无符号数转为字节
* 负数则会先转为无符号数再转为字节
*
* @param p 数
* @param byteOrder 端序
* @return 对应的二进制
*/
public static byte[] numberToByte(long p, ByteOrder byteOrder) {
int len = 8;

if(p < 0) {
if (p < 0) {
throw new MagicByteException("invalid number,this is unsigned number! try bigIntegerToByte(BigInteger)");
}

byte[] res = new byte[len];
int i = len - 1;
for (; i >= 0 && p>0 ; i--) {
res[i] = (byte)(p & 0xFF);
p = p >> len ;
if (ByteOrder.LITTLE_ENDIAN == byteOrder) {
int i = 0;
for (; i < len && p > 0; i++) {
res[i] = (byte) (p & 0xFF);
p >>= Byte.SIZE;
}
return Arrays.copyOfRange(res, 0, i);
} else {
int i = len - 1;
for (; i >= 0 && p > 0; i--) {
res[i] = (byte) (p & 0xFF);
p = p >> len;
}
return Arrays.copyOfRange(res, i + 1, len);
}

return Arrays.copyOfRange(res, i+1, len);
}


/**
* 数组转为正整数
*
* <p>
* 只支持正整数
* @param p
* @return
*
* @param p 二进制数据
* @return 十进制数
*/
public static long byteToNumber(byte[] p) {
if(p.length > 8){
return byteToNumber(p, GlobalConfigs.getGlobalDefaultByteOrder().getBytes());
}

/**
* 数组转为正整数
* <p>
* 只支持正整数
*
* @param p 二进制数据
* @param byteOrder 端序
* @return 十进制数
*/
public static long byteToNumber(byte[] p, ByteOrder byteOrder) {
if (p.length > 8) {
throw new MagicByteException("invalid bytes, byte data too large, can't convert to long, try byteToBigInteger!");
}
long res = 0;
for (byte b : p) {
res <<= 8;
res |= (b & 0xFF);
if (ByteOrder.LITTLE_ENDIAN == byteOrder) {
for (int i = p.length - 1; i >= 0; i--) {
res <<= Byte.SIZE;
res |= (p[i] & 0xff);
}
} else {
for (byte b : p) {
res <<= 8;
res |= (b & 0xFF);
}
}
return res;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import com.github.misterchangray.core.enums.TypeEnum;
import com.github.misterchangray.core.exception.MagicParseException;

import java.lang.reflect.Type;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.*;
import java.util.stream.Collectors;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* @description:
Expand Down Expand Up @@ -80,7 +81,10 @@ public DynamicByteBuffer order(ByteOrder order) {
return this;
}


public ByteOrder getOrder() {
return this.byteBuffer.order();
}

public byte get() {
if(this.byteBuffer.capacity() - this.byteBuffer.position() < TypeEnum.BYTE.getBytes()) {
throw new MagicParseException("Missing data");
Expand Down
Loading

0 comments on commit 3e4ca92

Please sign in to comment.