diff --git a/src/main/java/component/SignatureInfo.java b/src/main/java/component/SignatureInfo.java index 19a863a..64b7ded 100644 --- a/src/main/java/component/SignatureInfo.java +++ b/src/main/java/component/SignatureInfo.java @@ -155,7 +155,13 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble { int elementSize=block.getSubElements().length(); for (int i=0;i= 0){ + return false; + } return true; } - - //todo:判断biginteger是否合法 - - public VlInt(byte[] bytes){ - this.VlIntBytes=bytes; - this.VlIntValue=vlintBytes2bigInteger(bytes); - if(this.VlIntValue != null) { - this.size = SizeOfVarNumber(this.VlIntValue); - } + public boolean isValidVlIntValue(){ + return isValidVlIntValue(this.VlIntValue); } + /** + * 判断是否已经初始化 + * @return + */ + public boolean isInitial(){ + return this.size>0; + } + + /** + * 传入的应是一个有效的变长整型数组 + * @param vlIntBytes + */ + public VlInt(byte[] vlIntBytes){ + if(!isValidVlIntBytes(vlIntBytes)){ + throw new Error("VlInt(byte[] bytes): VlIntBytes is invalid."); + } + this.VlIntBytes=vlIntBytes; + this.VlIntValue=vlintBytes2bigInteger(vlIntBytes); + this.size = SizeOfVarNumber(this.VlIntValue); + } + + /** + * 传入的应是一个有效的变长整型value + * @param bigInteger + */ public VlInt(BigInteger bigInteger){ + if(!isValidVlIntValue(bigInteger)){ + throw new Error("VlInt(BigInteger bigInteger): VlIntValue is invalid."); + } this.VlIntValue=bigInteger; this.VlIntBytes=bigInteger2vlintBytes(bigInteger); this.size=SizeOfVarNumber(this.VlIntValue); } public VlInt(SizeT sizeT){ + if(!sizeT.isInitial()){ + throw new Error("VlInt(SizeT sizeT): The sizeT is invalid, it should be initial."); + } this.VlIntBytes=sizeT.getVlIntBytes(); this.VlIntValue=sizeT.getVlIntValue(); this.size = sizeT.size(); @@ -90,32 +157,32 @@ public class VlInt { public VlInt(int i){ this.VlIntValue=BigInteger.valueOf(i); + if(!isValidVlIntValue(this.VlIntValue)){ + throw new Error("VlInt(int i): The i value is invalid, it is out of the bound."); + } this.VlIntBytes=bigInteger2vlintBytes(this.VlIntValue); this.size=SizeOfVarNumber(this.VlIntValue); } public VlInt(long l){ this.VlIntValue=BigInteger.valueOf(l); + if(!isValidVlIntValue(this.VlIntValue)){ + throw new Error("VlInt(long l): The l value is invalid, it is out of the bound."); + } this.VlIntBytes=bigInteger2vlintBytes(this.VlIntValue); this.size=SizeOfVarNumber(this.VlIntValue); } - // 传入一个变长无符号字节数组byte[],将它解析成一个VlInt - public VlInt createVlInt(byte[] bytes){ - return new VlInt(bytes); - } - - // 传入一个BigInteger,将它解析成一个VlInt - public VlInt createVlIntByBigInteger(BigInteger bigInteger){ - return new VlInt(bigInteger); - } - + /** + * 获取bytes + * @return + */ public byte[] getVlIntBytes(){ return this.VlIntBytes; } /** - * 将vlint转为一个int输出:需要判断size是不是小于4(1 or 3) + * 将vlint转为一个int输出:需要判断size是不是1 or 3 * @return */ public int getVlIntValue2Int() throws VlIntException { @@ -126,8 +193,8 @@ public class VlInt { } /** - * 将vlint转为一个long输出:需要判断size是不是小于8(5) - * + * 将vlint转为一个long输出 + * todo: 理论上存在越界问题 * @return */ public long getVlIntValue2Long() throws VlIntException { @@ -188,7 +255,6 @@ public class VlInt { /** * 减去另一个VlInt - * todo:暂没考虑两者差值越界int最大值 * @param vlInt * @return */ @@ -215,21 +281,24 @@ public class VlInt { */ private byte[] bigInteger2vlintBytes(BigInteger bigInteger){ byte[] vlintbytes; - if(SizeOfVarNumber(bigInteger)==1){ + int size=SizeOfVarNumber(bigInteger); + if(size==1){ vlintbytes=new byte[1]; vlintbytes[0]= ByteHelper.uint8ToByte(bigInteger.intValue()); - }else if(SizeOfVarNumber(bigInteger)==3){ + }else if(size==3){ vlintbytes=new byte[3]; vlintbytes[0]=ByteHelper.uint8ToByte(VlIntExtend2); System.arraycopy(ByteHelper.uint16ToByteArray(bigInteger.intValue()),0,vlintbytes,1,2); - }else if(SizeOfVarNumber(bigInteger)==5){ + }else if(size==5){ vlintbytes=new byte[5]; vlintbytes[0]=ByteHelper.uint8ToByte(VlIntExtend4); System.arraycopy(ByteHelper.uint32ToByteArray(bigInteger.longValue()),0,vlintbytes,1,4); - }else{ + }else if(size==9){ vlintbytes=new byte[9]; vlintbytes[0]=ByteHelper.uint8ToByte(VlIntExtend8); System.arraycopy(ByteHelper.uint64ToByteArray(bigInteger.longValue()),0,vlintbytes,1,8); + }else{ + throw new Error("VlInt.bigInteger2vlintBytes: VlInt value is invalid, it should be in [0, 2^64)."); } return vlintbytes; } @@ -260,7 +329,7 @@ public class VlInt { long value=ByteHelper.uint64ToLong(valueByte); vlintValue=BigInteger.valueOf(value); }else{ - return null; + throw new Error("VlInt.vlintBytes2bigInteger: VlInt bytes length is invalid, it should be in [1,3,5,9]"); } return vlintValue; } @@ -268,21 +337,11 @@ public class VlInt { @Override public String toString() { - return "VlInt{" + + return "VlInt{ " + "VlIntBytes=" + Arrays.toString(VlIntBytes) + ", VlIntValue=" + VlIntValue + ", size=" + size + - '}'; + " }"; } - // 测试 - public static void main(String[] args){ -// BigInteger bigInteger=new BigInteger("3"); -// VlInt vlInt=new VlInt(bigInteger); -// System.out.println(vlInt.isEqual(2)); - byte[] b1 = new byte[]{(byte) 0xf1,0x00,0x7f};//241,0,127 - VlInt start1 = new VlInt(b1); - System.out.println("VlIntBytes:"+start1.getVlIntBytes().length); - System.out.println("VlIntValue:"+start1.getVlIntValue()); - } } diff --git a/src/main/java/util/ByteHelper.java b/src/main/java/util/ByteHelper.java index 9590a52..d234b78 100644 --- a/src/main/java/util/ByteHelper.java +++ b/src/main/java/util/ByteHelper.java @@ -8,7 +8,7 @@ import java.lang.reflect.Array; import java.util.Arrays; /* - * @Author: Wang Feng + * @Author: Wang Feng、Zhao Feng、Guo Hongyu * @Description:常用的数据类型转换。转无符号byte是重点 * @Version: 1.0.0 * @Date: 15:40 2021/3/2 diff --git a/src/test/java/encoding/VlIntTest.java b/src/test/java/encoding/VlIntTest.java index 036133a..0543702 100644 --- a/src/test/java/encoding/VlIntTest.java +++ b/src/test/java/encoding/VlIntTest.java @@ -21,6 +21,17 @@ import static org.junit.Assert.*; public class VlIntTest { private static final org.slf4j.Logger logger = LoggerFactory.getLogger(VlIntTest.class); + // 测试 + public static void main(String[] args){ +// BigInteger bigInteger=new BigInteger("3"); +// VlInt vlInt=new VlInt(bigInteger); +// System.out.println(vlInt.isEqual(2)); + byte[] b1 = new byte[]{(byte) 0xf1, (byte) 0xff, (byte) 0xff};//241,0,127 + VlInt start1 = new VlInt(b1); + System.out.println("VlIntBytes:"+start1.getVlIntBytes().length); + System.out.println("VlIntValue:"+start1.getVlIntValue()); + System.out.println(start1.toString()); + } // @Test // public void size() { diff --git a/src/test/java/util/ByteHelperTest.java b/src/test/java/util/ByteHelperTest.java index ae4c529..c66a537 100644 --- a/src/test/java/util/ByteHelperTest.java +++ b/src/test/java/util/ByteHelperTest.java @@ -214,18 +214,21 @@ import static org.junit.Assert.*; byte b3 = 0x7f; byte b4 = (byte) 0x80; byte b5 = (byte) 0xff; + byte b6=(byte)0xf1; ByteHelper byteHelper = new ByteHelper(); int a1 = byteHelper.uint8ToInt(b1); int a2 = byteHelper.uint8ToInt(b2); int a3 = byteHelper.uint8ToInt(b3); int a4 = byteHelper.uint8ToInt(b4); int a5 = byteHelper.uint8ToInt(b5); + int a6=byteHelper.uint8ToInt(b6); assertEquals(a1,0); assertEquals(a2,1); assertEquals(a3,127); assertEquals(a4,128); assertEquals(a5,255); + assertEquals(a6,241); } @Test