add test: ReadOnlyFieldTest

This commit is contained in:
free will committed 2021-03-25 17:41:13 +08:00
1 parent 0a7ccc95a3
commit 8a2da10583
19 files changed
+1251 -966

No files matched your search

+70 -58
View File
@@ -12,121 +12,133 @@ import encoding.*;
public class ReadOnlyField implements TlvComponentBase, IEncodingAble {
private ElementContainer blocks;
public ReadOnlyField() {
blocks = new ElementContainer();
}
/**
* 往只读区添加一个Block
*
* @param block
*/
public void addBlock(Block block){
public void addBlock(Block block) {
blocks.addElement(block);
}
/**
* 获取只读区指定位置的 Block
*
* @return
*/
public Block getBlock(int index){
return this.blocks.getBlock(index);
public Block getBlock(int index) throws ComponentException {
try {
return this.blocks.getBlock(index);
} catch (BlockException e) {
throw new ComponentException("ReadOnlyField.getBlock: " + e.getMessage());
}
}
/**
* 清空只读区中,所有的子 Block
*/
public void clearBlocks(){
public void clearBlocks() {
this.blocks.clear();
}
/**
* 获取只读区所有的 Block
*
* @return
*/
public ElementContainer getBlocks(){
public ElementContainer getBlocks() {
return this.blocks;
}
/**
* 获取只读区中,第一个指定TLV-TYPE的子 TLV Block
*
* @return
*/
public Block getBlockByType(VlInt tlvType){
return this.blocks.getElement(tlvType);
public Block getBlockByType(VlInt tlvType) throws ComponentException {
try {
return this.blocks.getElement(tlvType);
} catch (BlockException e) {
throw new ComponentException("ReadOnlyField.getBlockType: " + e.getMessage());
}
}
/**
* 将只读区线速编码为一个 TLV
*
* @param encoder
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
// 如果只读区没有任何子 TLV,则可以去掉只读区 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= 0;
try {
tmplen = encoder.prependBlock(this.blocks.getBlock(i));
} catch (EncoderException e) {
throw new ComponentException("ReadOnlyField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
try {
// 如果只读区没有任何子 TLV,则可以去掉只读区 TLV
if (this.blocks.length() == 0) {
return -1;
}
totalLength+=tmplen;
}
// 编码 TLV-LENGTH
int tmplen= 0;
try {
int totalLength = 0;
// 编码TLV-VALUE
// 反向遍历,可以保证解码的时候正向输出
for (int i = this.blocks.length() - 1; i >= 0; i--) {
int tmplen = 0;
tmplen = encoder.prependBlock(this.blocks.getBlock(i));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
}
// 编码 TLV-LENGTH
int tmplen = 0;
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("ReadOnlyField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvReadOnlyField));
} catch (EncoderException e) {
throw new ComponentException("ReadOnlyField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvReadOnlyField));
return totalLength;
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
return totalLength;
} catch (EncoderException | BlockException e) {
throw new ComponentException("ReadOnlyField.wireEncode: " + e.getMessage());
}
}
/**
* 从 TLV Block 中解码出一个 ReadOnlyField
*
* @param block
* @return
*/
@Override
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvReadOnlyField))){
return false;
}
// 解析子 TLV
try {
if(!block.parseSubElements()){
// 检查 Type 是否正确
if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvReadOnlyField))) {
return false;
}
} catch (BlockException e) {
throw new ComponentException("ReadOnlyField.wireDecode: "+e.getMessage());
}
this.blocks=block.getSubElements();
return true;
// 解析子 TLV
if (!block.parseSubElements()) {
return false;
}
this.blocks = block.getSubElements();
return true;
} catch (BlockException e) {
throw new ComponentException("ReadOnlyField.wireDecode: " + e.getMessage());
}
}
}