add error handle: ComponentException & PacketException

This commit is contained in:
free will committed 2021-03-24 11:14:54 +08:00
1 parent d7f063cbb9
commit 7bf0906291
35 files changed
+955 -362

No files matched your search

+39 -13
View File
@@ -38,7 +38,7 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
* 根据 TLV Block 创建一个 SignatureInfo
* @param block
*/
public SignatureInfo(Block block) throws VlIntException {
public SignatureInfo(Block block) throws ComponentException {
this.buildSignatureInfoByBlock(block);
}
@@ -74,7 +74,7 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
* @param block
* @return
*/
public boolean buildSignatureInfoByBlock(Block block) throws VlIntException {
public boolean buildSignatureInfoByBlock(Block block) throws ComponentException {
return this.wireDecode(block);
}
@@ -100,32 +100,50 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
if(!this.keyLocator.isValid()){
// KeyLocator 是可选的,如果当前 SignatureInfo 对象有一个有效的 KeyLocator,则对其进行编码
int tmplen=this.keyLocator.wireEncode(encoder);
int tmplen= 0;
try {
tmplen = this.keyLocator.wireEncode(encoder);
} catch (ComponentException e) {
throw new ComponentException("SignatureInfo.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
}
int tmplen=this.signatureType.wireEncode(encoder);
int tmplen= 0;
try {
tmplen = this.signatureType.wireEncode(encoder);
} catch (EncoderException e) {
throw new ComponentException("SignatureInfo.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
try {
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("SignatureInfo.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvSignatureInfo));
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvSignatureInfo));
} catch (EncoderException e) {
throw new ComponentException("SignatureInfo.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -140,15 +158,19 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) throws VlIntException {
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvSignatureInfo))){
return false;
}
// 解析子 TLV
if(!block.parseSubElements()){
return false;
try {
if(!block.parseSubElements()){
return false;
}
} catch (BlockException e) {
throw new ComponentException("SignatureInfo.wireDecode: "+e.getMessage());
}
boolean findSignatureType=false;
@@ -163,8 +185,12 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
}
switch (type){
case TLV.TlvSignatureType:
if(!this.signatureType.wireDecode(cacheBlock)){
return false;
try {
if(!this.signatureType.wireDecode(cacheBlock)){
return false;
}
} catch (VlIntException e) {
throw new ComponentException("SignatureInfo.wireDecode: "+e.getMessage());
}
findSignatureType=true;
case TLV.TlvKeyLocator:
@@ -172,7 +198,7 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
return false;
}
default:
throw new Error("SignatureInfo.wireDecode: SignatureInfo sub element type error");
throw new ComponentException("SignatureInfo.wireDecode: SignatureInfo sub element type error");
// return false;
}