mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
finished encoder
This commit is contained in:
1 parent
4c1887bbc5
commit
6fddbff833
4 files changed
+131
-16
No files matched your search
@@ -20,6 +20,8 @@ public class VlInt {
|
||||
private byte[] VlIntBytes;
|
||||
// 变长整数表示的真实数值
|
||||
private BigInteger VlIntValue;
|
||||
// 变长整数的长度: 1 3 5 9 ...
|
||||
private int size;
|
||||
|
||||
// 一些int常量
|
||||
// @Description:
|
||||
@@ -32,12 +34,17 @@ public class VlInt {
|
||||
public static final int VlIntExtend4=242;
|
||||
public static final int VlIntExtend8=243;
|
||||
|
||||
public static final int VlIntSize1=1;
|
||||
public static final int VlIntSize3=3;
|
||||
public static final int VlIntSize5=5;
|
||||
public static final int VlIntSize9=9;
|
||||
|
||||
/**
|
||||
* 测量一个变长整型实际用几个字节表示
|
||||
* @return
|
||||
*/
|
||||
public int SizeOfVarNumber(){
|
||||
return SizeOfVarNumber(this.VlIntValue);
|
||||
public int size(){
|
||||
return this.size;
|
||||
}
|
||||
private static int SizeOfVarNumber(BigInteger bigInteger){
|
||||
if(bigInteger.compareTo(BigInteger.valueOf(241)) < 0){
|
||||
@@ -54,27 +61,42 @@ public class VlInt {
|
||||
}
|
||||
|
||||
//todo:判断bytes是否是一个合法的变长非负整型
|
||||
private boolean isValidVlIntBytes(){
|
||||
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 VlInt(BigInteger bigInteger){
|
||||
this.VlIntValue=bigInteger;
|
||||
this.VlIntBytes=bigInteger2vlintBytes(bigInteger);
|
||||
this.size=SizeOfVarNumber(this.VlIntValue);
|
||||
}
|
||||
|
||||
public VlInt(SizeT sizeT){
|
||||
this.VlIntBytes=sizeT.getVlIntBytes();
|
||||
this.VlIntValue=sizeT.getVlIntValue();
|
||||
this.size = sizeT.size();
|
||||
}
|
||||
|
||||
public VlInt(int i){
|
||||
this.VlIntValue=BigInteger.valueOf(i);
|
||||
this.VlIntBytes=bigInteger2vlintBytes(this.VlIntValue);
|
||||
this.size=SizeOfVarNumber(this.VlIntValue);
|
||||
}
|
||||
|
||||
public VlInt(long l){
|
||||
this.VlIntValue=BigInteger.valueOf(l);
|
||||
this.VlIntBytes=bigInteger2vlintBytes(this.VlIntValue);
|
||||
this.size=SizeOfVarNumber(this.VlIntValue);
|
||||
}
|
||||
|
||||
// 传入一个变长无符号字节数组byte[],将它解析成一个VlInt
|
||||
@@ -87,12 +109,16 @@ public class VlInt {
|
||||
return new VlInt(bigInteger);
|
||||
}
|
||||
|
||||
public byte[] getVlIntBytes(){
|
||||
return this.VlIntBytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将vlint转为一个int输出:需要判断size是不是小于4(1 or 3)
|
||||
* @return
|
||||
*/
|
||||
public int getVlIntValue2Int(){
|
||||
if(this.SizeOfVarNumber()<4){
|
||||
if(this.size()<4){
|
||||
return VlIntValue.intValue();
|
||||
}
|
||||
return Integer.parseInt(null);
|
||||
@@ -103,7 +129,7 @@ public class VlInt {
|
||||
* @return
|
||||
*/
|
||||
public long getVlIntValue2Long(){
|
||||
if(this.SizeOfVarNumber()<8){
|
||||
if(this.size()<8){
|
||||
return VlIntValue.longValue();
|
||||
}
|
||||
return Long.parseLong(null);
|
||||
@@ -114,7 +140,7 @@ public class VlInt {
|
||||
* @return
|
||||
*/
|
||||
public BigInteger getVlIntValue(){
|
||||
if(this.SizeOfVarNumber()>=9){
|
||||
if(this.size()>=9){
|
||||
return VlIntValue;
|
||||
}
|
||||
return null;
|
||||
|
||||
Reference in new issue
Block a user