mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
add error handle; SizeT
This commit is contained in:
1 parent
de55943194
commit
d46b3c15cd
4 files changed
+76
-199
No files matched your search
@@ -43,9 +43,15 @@ public class Encoder {
|
|||||||
this.isInitial=true;
|
this.isInitial=true;
|
||||||
this.isEstimator=false;
|
this.isEstimator=false;
|
||||||
this.totalReserve=totalReserve;
|
this.totalReserve=totalReserve;
|
||||||
this.buffer=new byte[totalReserve.getVlIntValue2Int()]; //todo: 特殊情况下,可能越界。
|
int len=0;
|
||||||
this.left=totalReserve.subtract(reserveFromBack)-1;//todo: 特殊情况下,可能越界。
|
try {
|
||||||
this.right=totalReserve.subtract(reserveFromBack);//todo: 特殊情况下,可能越界。
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,12 +178,24 @@ public class Encoder {
|
|||||||
if(check(size)&&(size.compareTo(array.length)<=0)){
|
if(check(size)&&(size.compareTo(array.length)<=0)){
|
||||||
if(this.isEstimator){
|
if(this.isEstimator){
|
||||||
this.left-=array.length;
|
this.left-=array.length;
|
||||||
return size.getVlIntValue2Int();
|
try {
|
||||||
|
return size.getVlIntValue2Int();
|
||||||
|
} catch (VlIntException e) {
|
||||||
|
throw new Error("Encoder.prependByteArray: "+e.getMessage());
|
||||||
|
}
|
||||||
}else{
|
}else{
|
||||||
System.arraycopy(array,0,this.buffer,
|
try {
|
||||||
this.left-size.getVlIntValue2Int()+1,size.getVlIntValue2Int());
|
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;
|
this.left-=array.length;
|
||||||
return size.getVlIntValue2Int();
|
try {
|
||||||
|
return size.getVlIntValue2Int();
|
||||||
|
} catch (VlIntException e) {
|
||||||
|
throw new Error("Encoder.prependByteArray: "+e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@@ -192,12 +210,24 @@ public class Encoder {
|
|||||||
if(check(size)&&(size.compareTo(array.length)<=0)){
|
if(check(size)&&(size.compareTo(array.length)<=0)){
|
||||||
if(this.isEstimator){
|
if(this.isEstimator){
|
||||||
this.right+=array.length;
|
this.right+=array.length;
|
||||||
return size.getVlIntValue2Int();
|
try {
|
||||||
|
return size.getVlIntValue2Int();
|
||||||
|
} catch (VlIntException e) {
|
||||||
|
throw new Error("Encoder.appendByteArray: "+e.getMessage());
|
||||||
|
}
|
||||||
}else{
|
}else{
|
||||||
System.arraycopy(array,0,this.buffer,
|
try {
|
||||||
this.right,size.getVlIntValue2Int());
|
System.arraycopy(array,0,this.buffer,
|
||||||
|
this.right,size.getVlIntValue2Int());
|
||||||
|
} catch (VlIntException e) {
|
||||||
|
throw new Error("Encoder.appendByteArray: "+e.getMessage());
|
||||||
|
}
|
||||||
this.right+=array.length;
|
this.right+=array.length;
|
||||||
return size.getVlIntValue2Int();
|
try {
|
||||||
|
return size.getVlIntValue2Int();
|
||||||
|
} catch (VlIntException e) {
|
||||||
|
throw new Error("Encoder.appendByteArray: "+e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@@ -246,7 +276,11 @@ public class Encoder {
|
|||||||
*/
|
*/
|
||||||
public int appendByteArrayBlock(VlInt tlvType,byte[] array,SizeT size){
|
public int appendByteArrayBlock(VlInt tlvType,byte[] array,SizeT size){
|
||||||
int totalLength=this.appendByteArray(array,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);
|
totalLength+=this.appendVarNumber(tlvType);
|
||||||
return totalLength;
|
return totalLength;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,206 +7,29 @@ import static java.lang.Math.pow;
|
|||||||
/*
|
/*
|
||||||
* @Author: Wang Feng
|
* @Author: Wang Feng
|
||||||
* @Description:变长无符号整型,用于表示TLV中的T和L
|
* @Description:变长无符号整型,用于表示TLV中的T和L
|
||||||
* todo: 本类需要在VlInt经过彻底测试之后,再进行完善。或可直接继承vlint,或可内包装一个VlInt.
|
|
||||||
* @Version: 1.0.0
|
* @Version: 1.0.0
|
||||||
* @Date: 18:13 2021/3/1
|
* @Date: 18:13 2021/3/1
|
||||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||||
*/
|
*/
|
||||||
public class SizeT {
|
public class SizeT extends VlInt{
|
||||||
// 实际传输的数据
|
|
||||||
private byte[] VlIntBytes;
|
|
||||||
// 变长整数表示的真实数值
|
|
||||||
private BigInteger VlIntValue;
|
|
||||||
// 变长整数的长度: 1 3 5 9 ...
|
|
||||||
private int size;
|
|
||||||
|
|
||||||
/**
|
public SizeT(byte[] vlIntBytes) {
|
||||||
* 测量一个变长整型实际用几个字节表示
|
super(vlIntBytes);
|
||||||
* @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是否是一个合法的变长非负整型
|
public SizeT(BigInteger bigInteger) {
|
||||||
|
super(bigInteger);
|
||||||
//todo:判断biginteger是否合法
|
|
||||||
|
|
||||||
public boolean isInitial(){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SizeT(byte[] bytes){
|
|
||||||
this.VlIntBytes=bytes;
|
|
||||||
this.VlIntValue=vlintBytes2bigInteger(bytes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public SizeT(VlInt vlInt){
|
public SizeT(VlInt vlInt){
|
||||||
this.VlIntBytes=vlInt.getVlIntBytes();
|
super(vlInt);
|
||||||
this.VlIntValue=vlInt.getVlIntValue();
|
|
||||||
this.size = vlInt.size();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public SizeT(BigInteger bigInteger){
|
public SizeT(int i) {
|
||||||
this.VlIntValue=bigInteger;
|
super(i);
|
||||||
this.VlIntBytes=bigInteger2vlintBytes(bigInteger);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 传入一个变长无符号字节数组byte[],将它解析成一个VlInt
|
public SizeT(long l) {
|
||||||
public SizeT createVlInt(byte[] bytes){
|
super(l);
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -155,6 +155,15 @@ public class VlInt {
|
|||||||
this.size = sizeT.size();
|
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){
|
public VlInt(int i){
|
||||||
this.VlIntValue=BigInteger.valueOf(i);
|
this.VlIntValue=BigInteger.valueOf(i);
|
||||||
if(!isValidVlIntValue(this.VlIntValue)){
|
if(!isValidVlIntValue(this.VlIntValue)){
|
||||||
|
|||||||
@@ -14,6 +14,17 @@ import java.math.BigInteger;
|
|||||||
*/
|
*/
|
||||||
public class TestSizeT {
|
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
|
@Test
|
||||||
public void testIsEqual(){
|
public void testIsEqual(){
|
||||||
BigInteger bigInteger = new BigInteger("3");
|
BigInteger bigInteger = new BigInteger("3");
|
||||||
|
|||||||
Reference in new issue
Block a user