package encoding; import java.math.BigInteger; 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; /** * 测量一个变长整型实际用几个字节表示 * @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; } } //todo:判断bytes是否是一个合法的变长非负整型 //todo:判断biginteger是否合法 public boolean isInitial(){ return true; } public SizeT(byte[] bytes){ this.VlIntBytes=bytes; this.VlIntValue=vlintBytes2bigInteger(bytes); } public SizeT(VlInt vlInt){ this.VlIntBytes=vlInt.getVlIntBytes(); this.VlIntValue=vlInt.getVlIntValue(); this.size = vlInt.size(); } public SizeT(BigInteger bigInteger){ this.VlIntValue=bigInteger; this.VlIntBytes=bigInteger2vlintBytes(bigInteger); } // 传入一个变长无符号字节数组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)); } }