mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
maven管理项目 + junit单元测试
This commit is contained in:
1 parent
b07bf8a9f9
commit
5938be8893
9 files changed
+47
-1
No files matched your search
@@ -0,0 +1,181 @@
|
||||
package encoding;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
import static java.lang.Math.pow;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:变长无符号整型,用于表示TLV中的T和L
|
||||
* @Version: 1.0.0
|
||||
* @Date: 18:13 2021/3/1
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class SizeT {
|
||||
// 实际传输的数据
|
||||
private byte[] VlIntBytes;
|
||||
// 变长整数表示的真实数值
|
||||
private BigInteger VlIntValue;
|
||||
|
||||
/**
|
||||
* 测量一个变长整型实际用几个字节表示
|
||||
* @return
|
||||
*/
|
||||
public int SizeOfVarNumber(){
|
||||
return SizeOfVarNumber(this.VlIntValue);
|
||||
}
|
||||
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 SizeT(byte[] bytes){
|
||||
this.VlIntBytes=bytes;
|
||||
this.VlIntValue=vlintBytes2bigInteger(bytes);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// todo:传入一个int,将它解析成一个VlInt
|
||||
public SizeT(int i){
|
||||
this.VlIntValue=new BigInteger(String.valueOf(i));
|
||||
this.VlIntBytes=bigInteger2vlintBytes(this.VlIntValue);
|
||||
}
|
||||
|
||||
// todo:传入一个long,将它解析成一个VlInt
|
||||
|
||||
// 将vlint转为一个int输出:需要判断size是不是小于4(1 or 3)
|
||||
public Integer getVlIntValue2Int(){
|
||||
if(this.SizeOfVarNumber()<4){
|
||||
return VlIntValue.intValue();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// 将vlint转为一个long输出:需要判断size是不是小于8(5)
|
||||
public Long getVlIntValue2Long(){
|
||||
if(this.SizeOfVarNumber()<8){
|
||||
return VlIntValue.longValue();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// 将vlint转为一个biginteger输出:需要判断size是不是等于9(9)
|
||||
public BigInteger getVlIntValue(){
|
||||
if(this.SizeOfVarNumber()>=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 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));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user