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

+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;
}
}