From d46b3c15cd3d49dd039db16dfe188d2e0ffb6753 Mon Sep 17 00:00:00 2001 From: free will <2647778488@qq.com> Date: Wed, 17 Mar 2021 10:52:01 +0800 Subject: [PATCH] add error handle; SizeT --- src/main/java/encoding/Encoder.java | 58 ++++++-- src/main/java/encoding/SizeT.java | 197 ++------------------------ src/main/java/encoding/VlInt.java | 9 ++ src/test/java/encoding/TestSizeT.java | 11 ++ 4 files changed, 76 insertions(+), 199 deletions(-) diff --git a/src/main/java/encoding/Encoder.java b/src/main/java/encoding/Encoder.java index cb7d16d..f38fe34 100644 --- a/src/main/java/encoding/Encoder.java +++ b/src/main/java/encoding/Encoder.java @@ -43,9 +43,15 @@ public class Encoder { this.isInitial=true; this.isEstimator=false; this.totalReserve=totalReserve; - this.buffer=new byte[totalReserve.getVlIntValue2Int()]; //todo: 特殊情况下,可能越界。 - this.left=totalReserve.subtract(reserveFromBack)-1;//todo: 特殊情况下,可能越界。 - this.right=totalReserve.subtract(reserveFromBack);//todo: 特殊情况下,可能越界。 + int len=0; + try { + len=totalReserve.getVlIntValue2Int(); + } catch (VlIntException e) { + throw new Error("Encoder.encoderReset: "+e.getMessage()); + } + this.buffer=new byte[len]; //todo: 特殊情况下,可能越界。 + this.left= (int) (totalReserve.subtract(reserveFromBack)-1);//todo: 特殊情况下,可能越界。 + this.right= (int) totalReserve.subtract(reserveFromBack);//todo: 特殊情况下,可能越界。 return true; } @@ -172,12 +178,24 @@ public class Encoder { if(check(size)&&(size.compareTo(array.length)<=0)){ if(this.isEstimator){ this.left-=array.length; - return size.getVlIntValue2Int(); + try { + return size.getVlIntValue2Int(); + } catch (VlIntException e) { + throw new Error("Encoder.prependByteArray: "+e.getMessage()); + } }else{ - System.arraycopy(array,0,this.buffer, - this.left-size.getVlIntValue2Int()+1,size.getVlIntValue2Int()); + try { + System.arraycopy(array,0,this.buffer, + this.left-size.getVlIntValue2Int()+1,size.getVlIntValue2Int()); + } catch (VlIntException e) { + throw new Error("Encoder.prependByteArray: "+e.getMessage()); + } this.left-=array.length; - return size.getVlIntValue2Int(); + try { + return size.getVlIntValue2Int(); + } catch (VlIntException e) { + throw new Error("Encoder.prependByteArray: "+e.getMessage()); + } } } return 0; @@ -192,12 +210,24 @@ public class Encoder { if(check(size)&&(size.compareTo(array.length)<=0)){ if(this.isEstimator){ this.right+=array.length; - return size.getVlIntValue2Int(); + try { + return size.getVlIntValue2Int(); + } catch (VlIntException e) { + throw new Error("Encoder.appendByteArray: "+e.getMessage()); + } }else{ - System.arraycopy(array,0,this.buffer, - this.right,size.getVlIntValue2Int()); + try { + System.arraycopy(array,0,this.buffer, + this.right,size.getVlIntValue2Int()); + } catch (VlIntException e) { + throw new Error("Encoder.appendByteArray: "+e.getMessage()); + } this.right+=array.length; - return size.getVlIntValue2Int(); + try { + return size.getVlIntValue2Int(); + } catch (VlIntException e) { + throw new Error("Encoder.appendByteArray: "+e.getMessage()); + } } } return 0; @@ -246,7 +276,11 @@ public class Encoder { */ public int appendByteArrayBlock(VlInt tlvType,byte[] array,SizeT size){ int totalLength=this.appendByteArray(array,size); - totalLength+=this.appendVarNumber(new VlInt(size.getVlIntValue2Long())); + try { + totalLength+=this.appendVarNumber(new VlInt(size.getVlIntValue2Long())); + } catch (VlIntException e) { + throw new Error("Encoder.appendByteArrayBlock: "+e.getMessage()); + } totalLength+=this.appendVarNumber(tlvType); return totalLength; } diff --git a/src/main/java/encoding/SizeT.java b/src/main/java/encoding/SizeT.java index bc20341..8797205 100644 --- a/src/main/java/encoding/SizeT.java +++ b/src/main/java/encoding/SizeT.java @@ -7,206 +7,29 @@ import static java.lang.Math.pow; /* * @Author: Wang Feng * @Description:变长无符号整型,用于表示TLV中的T和L - * todo: 本类需要在VlInt经过彻底测试之后,再进行完善。或可直接继承vlint,或可内包装一个VlInt. * @Version: 1.0.0 * @Date: 18:13 2021/3/1 * @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室 */ -public class SizeT { - // 实际传输的数据 - private byte[] VlIntBytes; - // 变长整数表示的真实数值 - private BigInteger VlIntValue; - // 变长整数的长度: 1 3 5 9 ... - private int size; +public class SizeT extends VlInt{ - /** - * 测量一个变长整型实际用几个字节表示 - * @return - */ - public int size(){ - return this.size; - } - private static int SizeOfVarNumber(BigInteger bigInteger){ - if(bigInteger.compareTo(BigInteger.valueOf(Long.parseLong("241"))) < 0){ - return 1; - }else if(bigInteger.compareTo(BigInteger.valueOf( - new Double(pow(2, 16)).longValue())) < 0){ - return 3; - }else if(bigInteger.compareTo(BigInteger.valueOf( - new Double(pow(2, 32)).longValue())) < 0){ - return 5; - }else{ //MIN1.0中,VlInt的size最大为9 - return 9; - } + public SizeT(byte[] vlIntBytes) { + super(vlIntBytes); } - //todo:判断bytes是否是一个合法的变长非负整型 - - //todo:判断biginteger是否合法 - - public boolean isInitial(){ - return true; - } - - public SizeT(byte[] bytes){ - this.VlIntBytes=bytes; - this.VlIntValue=vlintBytes2bigInteger(bytes); + public SizeT(BigInteger bigInteger) { + super(bigInteger); } public SizeT(VlInt vlInt){ - this.VlIntBytes=vlInt.getVlIntBytes(); - this.VlIntValue=vlInt.getVlIntValue(); - this.size = vlInt.size(); + super(vlInt); } - public SizeT(BigInteger bigInteger){ - this.VlIntValue=bigInteger; - this.VlIntBytes=bigInteger2vlintBytes(bigInteger); + public SizeT(int i) { + super(i); } - // 传入一个变长无符号字节数组byte[],将它解析成一个VlInt - public SizeT createVlInt(byte[] bytes){ - return new SizeT(bytes); - } - - // 传入一个BigInteger,将它解析成一个VlInt - public SizeT createVlIntByBigInteger(BigInteger bigInteger){ - return new SizeT(bigInteger); - } - - public byte[] getVlIntBytes(){ - return this.VlIntBytes; - } - - // todo:传入一个int,将它解析成一个VlInt - public SizeT(int i){ - this.VlIntValue=new BigInteger(String.valueOf(i)); - this.VlIntBytes=bigInteger2vlintBytes(this.VlIntValue); - } - - public SizeT(long l){ - this.VlIntValue=BigInteger.valueOf(l); - this.VlIntBytes=bigInteger2vlintBytes(this.VlIntValue); - } - - // todo:传入一个long,将它解析成一个VlInt - - // 将vlint转为一个int输出:需要判断size是不是小于4(1 or 3) - public int getVlIntValue2Int(){ - if(this.size()<4){ - return VlIntValue.intValue(); - } - return Integer.parseInt(null); - } - - // 将vlint转为一个long输出:需要判断size是不是小于8(5) - public long getVlIntValue2Long(){ - if(this.size()<8){ - return VlIntValue.longValue(); - } - return Long.parseLong(null); - } - - // 将vlint转为一个biginteger输出:需要判断size是不是等于9(9) - public BigInteger getVlIntValue(){ - if(this.size()>=9){ - return VlIntValue; - } - return null; - } - - // 判断vlint是否与某个int值相等 - public boolean isEqual(Integer vlint){ - return isEqual(vlint.intValue()); - } - public boolean isEqual(int i){ - if(this.VlIntValue.compareTo(new BigInteger(String.valueOf(i)))==0){ - return true; - } - return false; - } - public boolean isEqual(SizeT vlInt){ - if(this.VlIntValue.compareTo(vlInt.VlIntValue)==0){ - return true; - } - return false; - } - - // todo: 判断vlint是否与某个long值相等 - - /** - * 增加VlInt - * @param vlInt - * @return - */ - public int add(SizeT vlInt){ - return this.VlIntValue.add(vlInt.VlIntValue).intValue(); - } - - /** - * 判断与另一个VlInt的数值孰大孰小 - * @param vlInt - * @return - */ - public int compareTo(SizeT vlInt){ - return this.VlIntValue.compareTo(vlInt.VlIntValue); - } - public int compareTo(int i){ - BigInteger bigInteger=new BigInteger(String.valueOf(i)); - return this.VlIntValue.compareTo(bigInteger); - } - - /** - * 减去另一个VlInt - * todo:暂没考虑两者差值越界int最大值 - * @param vlInt - * @return - */ - public int subtract(SizeT vlInt){ - return this.VlIntValue.subtract(vlInt.VlIntValue).intValue(); - } - - // 将BigInteger转为byte[按照变长无符号整数方式转] - private byte[] bigInteger2vlintBytes(BigInteger bigInteger){ - byte[] vlintbytes; - if(SizeOfVarNumber(bigInteger)==1){ - vlintbytes=new byte[1]; - //todo - }else if(SizeOfVarNumber(bigInteger)==3){ - vlintbytes=new byte[3]; - //todo - }else if(SizeOfVarNumber(bigInteger)==5){ - vlintbytes=new byte[5]; - //todo - }else{ - vlintbytes=new byte[9]; - //todo - } - return vlintbytes; - } - - // 将变长无符号整数数组转为BigInteger - private BigInteger vlintBytes2bigInteger(byte[] vlIntBytes){ - BigInteger vlintValue=null; - if(vlIntBytes.length==1){ - //todo - }else if(vlIntBytes.length==3){ - //todo - }else if(vlIntBytes.length==5){ - //todo - }else if(vlIntBytes.length==9){ - //todo - }else{ - return null; - } - return vlintValue; - } - - // 测试 - public static void main(String[] args){ - BigInteger bigInteger=new BigInteger("3"); - SizeT vlInt=new SizeT(bigInteger); - System.out.println(vlInt.isEqual(2)); + public SizeT(long l) { + super(l); } } diff --git a/src/main/java/encoding/VlInt.java b/src/main/java/encoding/VlInt.java index 3da790a..c97a871 100644 --- a/src/main/java/encoding/VlInt.java +++ b/src/main/java/encoding/VlInt.java @@ -155,6 +155,15 @@ public class VlInt { this.size = sizeT.size(); } + public VlInt(VlInt vlInt){ + if(!vlInt.isInitial()){ + throw new Error("VlInt vlInt: The vlInt is invalid, it should be initial."); + } + this.VlIntBytes=vlInt.getVlIntBytes(); + this.VlIntValue=vlInt.getVlIntValue(); + this.size = vlInt.size(); + } + public VlInt(int i){ this.VlIntValue=BigInteger.valueOf(i); if(!isValidVlIntValue(this.VlIntValue)){ diff --git a/src/test/java/encoding/TestSizeT.java b/src/test/java/encoding/TestSizeT.java index 24e459c..e0ed0de 100644 --- a/src/test/java/encoding/TestSizeT.java +++ b/src/test/java/encoding/TestSizeT.java @@ -14,6 +14,17 @@ import java.math.BigInteger; */ public class TestSizeT { + 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 + SizeT start1 = new SizeT(b1); + System.out.println("VlIntBytes:"+start1.getVlIntBytes().length); + System.out.println("VlIntValue:"+start1.getVlIntValue()); + System.out.println(start1.toString()); + } + @Test public void testIsEqual(){ BigInteger bigInteger = new BigInteger("3");