finished encoder

This commit is contained in:
free will committed 2021-03-06 16:20:17 +08:00
1 parent 4c1887bbc5
commit 6fddbff833
4 files changed
+131 -16

No files matched your search

+4 -4
View File
@@ -50,14 +50,14 @@ public class Block {
}
// 读取length
VlInt tlvLength=TLV.readVarNumber(buffer,new VlInt(tlvType.SizeOfVarNumber()));
VlInt tlvLength=TLV.readVarNumber(buffer,new VlInt(tlvType.size()));
if(tlvLength==null){
return null;
}
return createBlockByTypeLengthBuffer(tlvType,tlvLength,
ByteHelper.getLenBytes(buffer,new VlInt(tlvType.SizeOfVarNumber()+tlvLength.SizeOfVarNumber()),
buffer.length-(tlvType.SizeOfVarNumber()+tlvLength.SizeOfVarNumber())),verifyLength);
ByteHelper.getLenBytes(buffer,new VlInt(tlvType.size()+tlvLength.size()),
buffer.length-(tlvType.size()+tlvLength.size())),verifyLength);
}
/**
@@ -130,7 +130,7 @@ public class Block {
if(!this.isValid()){
return null;
}
return new SizeT(this.tlvType.SizeOfVarNumber()+this.length.SizeOfVarNumber()+this.value.length);
return new SizeT(this.tlvType.size()+this.length.size()+this.value.length);
}
/**
+78 -1
View File
@@ -162,6 +162,83 @@ public class Encoder {
return 0;
}
/**
* 在前向插入一个可变长度的数字
* @param varNumber
* @return
*/
public int prependVarNumber(VlInt varNumber){
return this.prependByteArray(varNumber.getVlIntBytes(),new SizeT(varNumber.size()));
}
/**
* 在后向插入一个可变长度的数字
* @param varNumber
* @return
*/
public int appendVarNumber(VlInt varNumber){
return this.appendByteArray(varNumber.getVlIntBytes(),new SizeT(varNumber.size()));
}
//todo: 在前向&后向插入一个非负整数
/**
* 根据TLV-TYPE和一个字节数组,在前向插入一个 TLV Block
* @param tlvType
* @param array
* @param size
* @return
*/
public int prependByteArrayBlock(VlInt tlvType,byte[] array,SizeT size){
int totalLength=this.prependByteArray(array,size);
totalLength+=this.prependVarNumber(new VlInt(size));
totalLength+=this.prependVarNumber(tlvType);
return totalLength;
}
/**
* 根据TLV-TYPE和一个字节数组,在后向插入一个 TLV Block
* @param tlvType
* @param array
* @param size
* @return
*/
public int appendByteArrayBlock(VlInt tlvType,byte[] array,SizeT size){
int totalLength=this.appendByteArray(array,size);
totalLength+=this.appendVarNumber(new VlInt(size.getVlIntValue2Long()));
totalLength+=this.appendVarNumber(tlvType);
return totalLength;
}
/**
* 在前向插入一个 TLV Block
* @param block
* @return
*/
public int prependBlock(Block block){
return this.prependByteArrayBlock(block.getTlvType(),block.getValue(),new SizeT(block.getLength()));
}
/**
* 在后向插入一个 TLV Block
* @param block
* @return
*/
public int appendBlock(Block block){
return this.appendByteArrayBlock(block.getTlvType(),block.getValue(),new SizeT(block.getLength()));
}
/**
* 获取编码后的字节数组
* @return
*/
public byte[] getBuffer() {
if((!this.isInitial)||this.isEstimator){
return null;
}
int len=this.right-this.left-1;
byte[] res=new byte[len];
System.arraycopy(this.buffer,this.left+1,res,0,len);
return res;
}
}
+18 -6
View File
@@ -7,7 +7,7 @@ import static java.lang.Math.pow;
/*
* @Author: Wang Feng
* @Description:变长无符号整型,用于表示TLV中的T和L
* todo: 本类需要在VlInt经过彻底测试之后,再进行完善。或可直接继承vlint
* todo: 本类需要在VlInt经过彻底测试之后,再进行完善。或可直接继承vlint,或可内包装一个VlInt.
* @Version: 1.0.0
* @Date: 18:13 2021/3/1
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
@@ -17,13 +17,15 @@ public class SizeT {
private byte[] VlIntBytes;
// 变长整数表示的真实数值
private BigInteger VlIntValue;
// 变长整数的长度: 1 3 5 9 ...
private int size;
/**
* 测量一个变长整型实际用几个字节表示
* @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(Long.parseLong("241"))) < 0){
@@ -48,6 +50,12 @@ public class SizeT {
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);
@@ -63,6 +71,10 @@ public class SizeT {
return new SizeT(bigInteger);
}
public byte[] getVlIntBytes(){
return this.VlIntBytes;
}
// todo:传入一个int,将它解析成一个VlInt
public SizeT(int i){
this.VlIntValue=new BigInteger(String.valueOf(i));
@@ -78,7 +90,7 @@ public class SizeT {
// 将vlint转为一个int输出:需要判断size是不是小于4(1 or 3)
public int getVlIntValue2Int(){
if(this.SizeOfVarNumber()<4){
if(this.size()<4){
return VlIntValue.intValue();
}
return Integer.parseInt(null);
@@ -86,7 +98,7 @@ public class SizeT {
// 将vlint转为一个long输出:需要判断size是不是小于8(5)
public long getVlIntValue2Long(){
if(this.SizeOfVarNumber()<8){
if(this.size()<8){
return VlIntValue.longValue();
}
return Long.parseLong(null);
@@ -94,7 +106,7 @@ public class SizeT {
// 将vlint转为一个biginteger输出:需要判断size是不是等于9(9)
public BigInteger getVlIntValue(){
if(this.SizeOfVarNumber()>=9){
if(this.size()>=9){
return VlIntValue;
}
return null;
+31 -5
View File
@@ -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;