add error handle; VlInt

This commit is contained in:
free will committed 2021-03-17 10:34:32 +08:00
1 parent 17e35f10d0
commit de55943194
6 files changed
+131 -48

No files matched your search

+7 -1
View File
@@ -155,7 +155,13 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
int elementSize=block.getSubElements().length();
for (int i=0;i<elementSize;i++) {
Block cacheBlock=block.getSubElements().getBlock(i);
switch (cacheBlock.getType().getVlIntValue2Int()){
int type= 0;
try {
type = cacheBlock.getType().getVlIntValue2Int();
} catch (VlIntException e) {
e.printStackTrace();
}
switch (type){
case TLV.TlvSignatureType:
if(!this.signatureType.wireDecode(cacheBlock)){
return false;
+4
View File
@@ -45,6 +45,10 @@ public class SizeT {
//todo:判断biginteger是否合法
public boolean isInitial(){
return true;
}
public SizeT(byte[] bytes){
this.VlIntBytes=bytes;
this.VlIntValue=vlintBytes2bigInteger(bytes);
+105 -46
View File
@@ -15,14 +15,13 @@ import static java.lang.Math.pow;
* @Date: 18:13 2021/3/1
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class VlInt {
// 实际传输的数据
private byte[] VlIntBytes;
// 变长整数表示的真实数值
private BigInteger VlIntValue;
// 变长整数的长度: 1 3 5 9 ...
private int size;
// 变长整数的长度: 1 3 5 9 ...; 可以通过它来判断是否已经初始化VlInt
private int size=-1;
// 一些int常量
// @Description:
@@ -48,6 +47,9 @@ public class VlInt {
return this.size;
}
private static int SizeOfVarNumber(BigInteger bigInteger){
if(bigInteger==null){
throw new Error("VlInt.SizeOfVarNumber: VlInt value is null.");
}
if(bigInteger.compareTo(BigInteger.valueOf(241)) < 0){
return 1;
}else if(bigInteger.compareTo(BigInteger.valueOf(
@@ -56,33 +58,98 @@ public class VlInt {
}else if(bigInteger.compareTo(BigInteger.valueOf(
new Double(pow(2, 32)).longValue())) < 0){
return 5;
}else{ //MIN1.0中,VlInt的size最大为9
}else if(bigInteger.compareTo(BigInteger.valueOf(
new Double(pow(2, 64)).longValue())) < 0){
return 9;
}else{
//MIN1.0中,VlInt的size最大为9
throw new Error("VlInt.SizeOfVarNumber: VlInt value is out of bound, it should be less than 2^64.");
}
}
//todo:判断bytes是否是一个合法的变长非负整型
private boolean isValidVlIntBytes(){
/**
* 判断bytes是否是一个合法的变长非负整型
* @param vlIntBytes
* @return
*/
private boolean isValidVlIntBytes(byte[] vlIntBytes){
if(vlIntBytes.length<1){
return false;
}
int firstByteValue=ByteHelper.uint8ToInt(vlIntBytes[0]);
if((vlIntBytes.length==1)&&(firstByteValue<=240)){
return true;
}else if((vlIntBytes.length==3)&&(firstByteValue==241)){
return true;
}else if((vlIntBytes.length==5)&&(firstByteValue==242)){
return true;
}else if((vlIntBytes.length==9)&&(firstByteValue==243)){
return true;
}else {
return false;
}
}
public boolean isValidVlIntBytes(){
return isValidVlIntBytes(this.VlIntBytes);
}
/**
* 判断biginteger是否合法
* @param bigInteger
* @return
*/
private boolean isValidVlIntValue(BigInteger bigInteger){
if(bigInteger.compareTo(BigInteger.valueOf(0))<0){
return false;
}
if(bigInteger.compareTo(BigInteger.valueOf(
new Double(pow(2, 64)).longValue())) >= 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());
}
}
+1 -1
View File
@@ -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
+11
View File
@@ -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() {
+3
View File
@@ -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