fixed some test error: Encoder

This commit is contained in:
free will committed 2021-03-23 17:40:17 +08:00
1 parent 5785f29ec3
commit 3daf0156e0
5 files changed
+97 -48

No files matched your search

+10 -9
View File
@@ -26,7 +26,7 @@ public class Encoder {
this.right=1;
this.isInitial=true;
this.isEstimator=true;
this.totalReserve=new SizeT(Long.MAX_VALUE); //todo: 这里可能越界。
this.totalReserve=new SizeT(Long.MAX_VALUE);
}
/**
@@ -138,8 +138,8 @@ public class Encoder {
public byte[] buildNonNegativeIntegerArr(long uint64){
VlInt vlInt=new VlInt(uint64);
int len=vlInt.getVlIntBytes().length;
if(len==1){
return vlInt.getVlIntBytes();
if(uint64<=255){
return new byte[]{(byte) uint64};
}else {
byte[] res = new byte[len - 1];
System.arraycopy(vlInt.getVlIntBytes(),1,res,0,len-1);
@@ -275,13 +275,14 @@ public class Encoder {
* @return
*/
public int appendByteArrayBlock(VlInt tlvType,byte[] array,SizeT size) throws EncoderException {
int totalLength=this.appendByteArray(array,size);
try {
totalLength+=this.appendVarNumber(new VlInt(size.getVlIntValue2Long()));
} catch (VlIntException e) {
throw new EncoderException("appendByteArrayBlock: "+e.getMessage());
}
int totalLength=0;
// Append TLV-TYPE
totalLength+=this.appendVarNumber(tlvType);
// Append TLV-LENGTH
totalLength+=this.appendVarNumber(new VlInt(size));
// Append TLV-VALUE
totalLength+=this.appendByteArray(array,size);
return totalLength;
}
+1 -1
View File
@@ -28,7 +28,7 @@ public class VlInt {
// 1. x < 240 => 1字节表示 => 230
// 2. 241 =< x < uint16::Max => 第一个字节为241,后面连着2字节表示实际数值,总共用3个字节表示 => 246
// 3. uint16::Max =< x < uint32::Max => 第一个字节为242,后面连着4字节表示实际数值,总共用5个字节表示
// 4. int32::Max =< x < int64::Max => 第一个字节为243,后面连着8字节表示实际数值,总共用9个字节表示
// 4. int32::Max =< x < uint64::Max => 第一个字节为243,后面连着8字节表示实际数值,总共用9个字节表示
// 5. 后续还留有12个字节可以进行指数级扩展,目前留空
public static final int VlIntExtend2=241;
public static final int VlIntExtend4=242;
+18 -5
View File
@@ -331,7 +331,8 @@ public class ByteHelper {
// 便于测试,新添加两个public方法:
// 无符号byte[]转int
public static int new_byteArrayToInt(byte[]bytes){
// 传入的是长度为2或4的byte[]
public static int new_byteArrayToInt(byte[] bytes){
int value= 0;
//由高位到低位
for (int i = 0; i < bytes.length; i++) {
@@ -340,13 +341,25 @@ public class ByteHelper {
}
return value;
}
// 便于测试,新添加两个public方法:
// 无符号byte[]转int(溢出部分)
// 传入的是长度为4的byte[]
public static long new_byteArrayToAllInt(byte[] bytes){
int v=new_byteArrayToInt(bytes);
if(v<0){
return (long) (v+Math.pow(2,32));
}else{
return v;
}
}
// 无符号byte[]转long
public static long new_byteArrayToLong(byte[]bytes){
int value= 0;
// 传入的是长度为8的byte[]
public static long new_byteArrayToLong(byte[] bytes){
long value= 0;
//由高位到低位
for (int i = 0; i < 8; i++) {
int offset= (bytes.length - 1 - i) * 8;
value +=(bytes[i] & 0x000000000000ff) << offset; //往高位游
int offset= (8 - 1 - i) * 8;
value +=((long)bytes[i] & 0xff) << offset; //往高位游
}
return value;
}