mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-06-18 04:50:25 +08:00
finished component: MutableProtectedField
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
package component;
|
||||
|
||||
import encoding.*;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description: 表示一个MIN包格式中的可变保护区
|
||||
* @Version: 1.0.0
|
||||
* @Date: 20:45 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class MutableProtectField implements TlvComponentBase, IEncodingAble {
|
||||
private ElementContainer blocks;
|
||||
|
||||
/**
|
||||
* 添加一个Block
|
||||
* @param block
|
||||
*/
|
||||
public void addBlock(Block block){
|
||||
blocks.addElement(block);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定位置的 Block
|
||||
* @return
|
||||
*/
|
||||
public Block getBlock(int index){
|
||||
return this.blocks.getBlock(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空所有的子 Block
|
||||
*/
|
||||
public void clearBlocks(){
|
||||
this.blocks.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有的 Block
|
||||
* @return
|
||||
*/
|
||||
public ElementContainer getBlocks(){
|
||||
return this.blocks;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取第一个指定TLV-TYPE的子 TLV Block
|
||||
* @return
|
||||
*/
|
||||
public Block getBlockByType(VlInt tlvType){
|
||||
return this.blocks.getElement(tlvType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 线速编码为一个 TLV
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) {
|
||||
// 如果没有任何子 TLV,则可以去掉它
|
||||
if(this.blocks.length()==0){
|
||||
return 0;
|
||||
}
|
||||
|
||||
int totalLength=0;
|
||||
// 编码TLV-VALUE
|
||||
// 反向遍历,可以保证解码的时候正向输出
|
||||
for(int i=this.blocks.length()-1;i>=0;i++){
|
||||
int tmplen=encoder.prependBlock(this.blocks.getBlock(i));
|
||||
if(tmplen==0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
}
|
||||
|
||||
// 编码 TLV-LENGTH
|
||||
int tmplen=encoder.prependVarNumber(new VlInt(totalLength));
|
||||
if(tmplen==0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
// 编码 TLV-TYPE
|
||||
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvReadOnlyField));
|
||||
if(tmplen==0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 TLV Block 中解码出一个 ReadOnlyField
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvReadOnlyField))){
|
||||
return false;
|
||||
}
|
||||
|
||||
// 解析子 TLV
|
||||
if(!block.parseSubElements()){
|
||||
return false;
|
||||
}
|
||||
|
||||
this.blocks=block.getSubElements();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user