mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
add test: KeyLocatorTest
This commit is contained in:
1 parent
025d2f1c62
commit
c8f0e35f1b
4 files changed
+136
-42
No files matched your search
@@ -353,9 +353,8 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
|
||||
} else if(isByteArray()){
|
||||
return StringHelper.escape(new String(this.byteArrayValue));
|
||||
}else{
|
||||
// return StringHelper.FormatUint(this.intValue,10);
|
||||
// todo: 需要对long类型按照指定编码规则编码成字符串
|
||||
return "";
|
||||
return Long.valueOf(this.intValue).toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,10 +13,9 @@ import encoding.*;
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class KeyLocator implements TlvComponentBase, IEncodingAble {
|
||||
private Identifier identifier;
|
||||
private Identifier identifier=new Identifier();
|
||||
|
||||
public KeyLocator(){
|
||||
identifier=new Identifier();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -24,15 +23,19 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble {
|
||||
* @param block
|
||||
*/
|
||||
public KeyLocator(Block block) throws ComponentException {
|
||||
this.buildKeyLocatorByBlock(block);
|
||||
if(!this.buildKeyLocatorByBlock(block)){
|
||||
throw new ComponentException("KeyLocator(Block): block invalid.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据标识字符串 创建一个 KeyLocator
|
||||
* @param identifierString
|
||||
*/
|
||||
public KeyLocator(String identifierString){
|
||||
this.buildKeyLocatorByString(identifierString);
|
||||
public KeyLocator(String identifierString) throws ComponentException {
|
||||
if(!this.buildKeyLocatorByString(identifierString)){
|
||||
throw new ComponentException("KeyLocator(String): string invalid.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,42 +113,33 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble {
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) throws ComponentException {
|
||||
int totalLength=0;
|
||||
// 编码 TLV-VALUE
|
||||
int tmplen= 0;
|
||||
try {
|
||||
tmplen = this.identifier.wireEncode(encoder);
|
||||
} catch (ComponentException e) {
|
||||
int totalLength = 0;
|
||||
// 编码 TLV-VALUE
|
||||
int tmplen = this.identifier.wireEncode(encoder);
|
||||
if (tmplen <= 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmplen;
|
||||
|
||||
// 编码 TLV-LENGTH
|
||||
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
|
||||
if (tmplen <= 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmplen;
|
||||
|
||||
// 编码 TLV-TYPE
|
||||
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
|
||||
if (tmplen <= 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmplen;
|
||||
|
||||
return totalLength;
|
||||
} catch (ComponentException | EncoderException e) {
|
||||
throw new ComponentException("KeyLocator.wireEncode: "+e.getMessage());
|
||||
}
|
||||
if(tmplen<=0){
|
||||
return -1;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
// 编码 TLV-LENGTH
|
||||
try {
|
||||
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
|
||||
} catch (EncoderException e) {
|
||||
throw new ComponentException("KeyLocator.wireEncode: "+e.getMessage());
|
||||
}
|
||||
if(tmplen<=0){
|
||||
return -1;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
// 编码 TLV-TYPE
|
||||
try {
|
||||
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
|
||||
} catch (EncoderException e) {
|
||||
throw new ComponentException("KeyLocator.wireEncode: "+e.getMessage());
|
||||
}
|
||||
if(tmplen<=0){
|
||||
return -1;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,17 +151,19 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble {
|
||||
public boolean wireDecode(Block block) throws ComponentException {
|
||||
try {
|
||||
// 检查Type是否正确
|
||||
if(!TLV.isValidIdentifierType(block.getType())){
|
||||
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvKeyLocator))){
|
||||
return false;
|
||||
}
|
||||
// 解析子 TLV
|
||||
if(!block.parseSubElements()){
|
||||
return false;
|
||||
}
|
||||
|
||||
// 判断是否有且仅有一个子TLV
|
||||
if(block.getSubElements().length()!=1){
|
||||
return false;
|
||||
}
|
||||
|
||||
// 解析子TLV为一个Identifier
|
||||
return this.identifier.wireDecode(block.getSubElements().getBlock(0));
|
||||
} catch (BlockException e) {
|
||||
|
||||
@@ -247,7 +247,6 @@ public class Block {
|
||||
if ((this.value.length == 0) || this.elements.length() != 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 循环解析所有的一级子TLV
|
||||
for (int start = 0; start < this.value.length; ) {
|
||||
// 读取 TLV-TYPE
|
||||
|
||||
Reference in new issue
Block a user