From 28f963c28a9755c6ceed285fceebe1d2dd4c1d58 Mon Sep 17 00:00:00 2001 From: free will <2647778488@qq.com> Date: Thu, 25 Mar 2021 20:04:37 +0800 Subject: [PATCH] add error handle: ElementContainer --- src/main/java/component/Identifier.java | 20 +++++++++---------- src/main/java/component/IdentifierField.java | 13 ++++++------ .../java/component/IdentifierWrapper.java | 8 ++++---- src/main/java/component/KeyLocator.java | 8 ++++---- .../java/component/MutableDangerousField.java | 2 +- .../java/component/MutableProtectField.java | 2 +- src/main/java/component/ReadOnlyField.java | 2 +- src/main/java/component/Signature.java | 2 +- src/main/java/component/SignatureField.java | 6 +++--- src/main/java/component/SignatureInfo.java | 4 ++-- src/main/java/encoding/Block.java | 6 +----- src/main/java/encoding/ElementContainer.java | 6 +++--- src/main/java/packet/Data.java | 2 +- src/main/java/packet/Interest.java | 2 +- 14 files changed, 40 insertions(+), 43 deletions(-) diff --git a/src/main/java/component/Identifier.java b/src/main/java/component/Identifier.java index 5e0b5e0..6ec51b2 100644 --- a/src/main/java/component/Identifier.java +++ b/src/main/java/component/Identifier.java @@ -64,9 +64,9 @@ class IdentifierComponentContainer { * @param index * @return */ - public IdentifierComponent getElement(int index) { + public IdentifierComponent getElement(int index) throws ComponentException { if (index >= this.length()) { - return null; + throw new ComponentException("IdentifierComponents.getElement: index is out of bound."); } return this.identifierComponents.get(index); } @@ -77,7 +77,7 @@ class IdentifierComponentContainer { * @param num * @return */ - public IdentifierComponentContainer getPrefix(int num) { + public IdentifierComponentContainer getPrefix(int num) throws ComponentException { if (num > this.length()) { return null; } @@ -88,7 +88,7 @@ class IdentifierComponentContainer { return identifierComponentContainer; } - public IdentifierComponentContainer getSubIdentifierContainer(int startIndex, int num) { + public IdentifierComponentContainer getSubIdentifierContainer(int startIndex, int num) throws ComponentException { if ((startIndex + num) > this.length()) { return null; } @@ -131,7 +131,7 @@ public class Identifier implements TlvComponentBase, IEncodingAble { * * @param container */ - public Identifier(IdentifierComponentContainer container) { + public Identifier(IdentifierComponentContainer container) throws ComponentException { this.buildIdentifierByComponents(container); } @@ -181,7 +181,7 @@ public class Identifier implements TlvComponentBase, IEncodingAble { * @param container * @return */ - public boolean buildIdentifierByComponents(IdentifierComponentContainer container) { + public boolean buildIdentifierByComponents(IdentifierComponentContainer container) throws ComponentException { if (container.length() <= 0) { return false; } @@ -281,7 +281,7 @@ public class Identifier implements TlvComponentBase, IEncodingAble { * * @return */ - public String toUri() { + public String toUri() throws ComponentException { String res = ""; for (int i = 0; i < components.length(); i++) { res += ("/" + components.getElement(i).toUri()); @@ -313,7 +313,7 @@ public class Identifier implements TlvComponentBase, IEncodingAble { * @param index * @return */ - public IdentifierComponent get(int index) { + public IdentifierComponent get(int index) throws ComponentException { return this.components.getElement(index); } @@ -332,7 +332,7 @@ public class Identifier implements TlvComponentBase, IEncodingAble { * @param num * @return */ - public IdentifierComponentContainer getPrefix(int num) { + public IdentifierComponentContainer getPrefix(int num) throws ComponentException { return this.components.getPrefix(num); } @@ -341,7 +341,7 @@ public class Identifier implements TlvComponentBase, IEncodingAble { * * @return */ - public Identifier getSubIdentifier(int startIndex, int num) { + public Identifier getSubIdentifier(int startIndex, int num) throws ComponentException { return new Identifier(this.components.getSubIdentifierContainer(startIndex, num)); } diff --git a/src/main/java/component/IdentifierField.java b/src/main/java/component/IdentifierField.java index 247d8f7..c90df74 100644 --- a/src/main/java/component/IdentifierField.java +++ b/src/main/java/component/IdentifierField.java @@ -2,6 +2,7 @@ package component; import encoding.*; +import java.awt.*; import java.util.LinkedList; import java.util.List; @@ -50,9 +51,9 @@ class IdentifierContainer { * @param index * @return */ - public IdentifierWrapper getElement(int index) { + public IdentifierWrapper getElement(int index) throws ComponentException { if (index >= this.length()) { - return null; + throw new ComponentException("IdentifierWrappers.getElement: index is out of bound."); } return this.identifierWrappers.get(index); } @@ -63,13 +64,13 @@ class IdentifierContainer { * @param vlInt * @return */ - public IdentifierWrapper getElementByType(VlInt vlInt) { + public IdentifierWrapper getElementByType(VlInt vlInt) throws ComponentException { for (int i = 0; i < identifierWrappers.size(); i++) { if (identifierWrappers.get(i).getTlvType().isEqual(vlInt)) { return identifierWrappers.get(i); } } - return null; + throw new ComponentException("IdentifierWrappers.getElementByType: no found the tlvType"); } public void setElement(int index, IdentifierWrapper wrapper) { @@ -102,7 +103,7 @@ public class IdentifierField implements TlvComponentBase, IEncodingAble { * @param tlvType * @return */ - public IdentifierWrapper getIdentifierByType(VlInt tlvType) { + public IdentifierWrapper getIdentifierByType(VlInt tlvType) throws ComponentException { return this.identifiers.getElementByType(tlvType); } @@ -112,7 +113,7 @@ public class IdentifierField implements TlvComponentBase, IEncodingAble { * @param index * @return */ - public IdentifierWrapper getIdentifier(int index) { + public IdentifierWrapper getIdentifier(int index) throws ComponentException { return this.identifiers.getElement(index); } diff --git a/src/main/java/component/IdentifierWrapper.java b/src/main/java/component/IdentifierWrapper.java index 2953ef7..1dc2320 100644 --- a/src/main/java/component/IdentifierWrapper.java +++ b/src/main/java/component/IdentifierWrapper.java @@ -122,7 +122,7 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble { * * @param container */ - public IdentifierWrapper createCommonIdentifierByComponents(IdentifierComponentContainer container) { + public IdentifierWrapper createCommonIdentifierByComponents(IdentifierComponentContainer container) throws ComponentException { IdentifierWrapper identifierWrapper = new IdentifierWrapper(); identifierWrapper.tlvType = new VlInt(TLV.TlvIdentifierCommon); identifierWrapper.identifier.buildIdentifierByComponents(container); @@ -176,7 +176,7 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble { * @param container * @return */ - public IdentifierWrapper createContentInterestIdentifierByComponents(IdentifierComponentContainer container) { + public IdentifierWrapper createContentInterestIdentifierByComponents(IdentifierComponentContainer container) throws ComponentException { IdentifierWrapper identifierWrapper = new IdentifierWrapper(); identifierWrapper.tlvType = new VlInt(TLV.TlvIdentifierContentInterest); identifierWrapper.identifier.buildIdentifierByComponents(container); @@ -230,7 +230,7 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble { * @param container * @return */ - public IdentifierWrapper createContentDataIdentifierByComponents(IdentifierComponentContainer container) { + public IdentifierWrapper createContentDataIdentifierByComponents(IdentifierComponentContainer container) throws ComponentException { IdentifierWrapper identifierWrapper = new IdentifierWrapper(); identifierWrapper.tlvType = new VlInt(TLV.TlvIdentifierContentData); identifierWrapper.identifier.buildIdentifierByComponents(container); @@ -362,7 +362,7 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble { * * @return */ - public String toUri() { + public String toUri() throws ComponentException { return this.identifier.toUri(); } } diff --git a/src/main/java/component/KeyLocator.java b/src/main/java/component/KeyLocator.java index a11e0e0..2365b27 100644 --- a/src/main/java/component/KeyLocator.java +++ b/src/main/java/component/KeyLocator.java @@ -39,7 +39,7 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble { * 根据 components 创建一个 KeyLocator * @param container */ - public KeyLocator(IdentifierComponentContainer container){ + public KeyLocator(IdentifierComponentContainer container) throws ComponentException { this.buildKeyLocatorByComponents(container); } @@ -47,7 +47,7 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble { * 根据 Identifier 创建一个 KeyLocator * @param identifier */ - public KeyLocator(Identifier identifier){ + public KeyLocator(Identifier identifier) throws ComponentException { this.buildKeyLocatorByIdentifier(identifier); } @@ -74,7 +74,7 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble { * @param container * @return */ - public boolean buildKeyLocatorByComponents(IdentifierComponentContainer container){ + public boolean buildKeyLocatorByComponents(IdentifierComponentContainer container) throws ComponentException { return this.identifier.buildIdentifierByComponents(container); } @@ -83,7 +83,7 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble { * @param identifier * @return */ - public boolean buildKeyLocatorByIdentifier(Identifier identifier){ + public boolean buildKeyLocatorByIdentifier(Identifier identifier) throws ComponentException { return this.identifier.buildIdentifierByComponents(identifier.getComponents()); } diff --git a/src/main/java/component/MutableDangerousField.java b/src/main/java/component/MutableDangerousField.java index 39de92b..1927f44 100644 --- a/src/main/java/component/MutableDangerousField.java +++ b/src/main/java/component/MutableDangerousField.java @@ -57,7 +57,7 @@ public class MutableDangerousField implements TlvComponentBase, IEncodingAble { */ public Block getBlockByType(VlInt tlvType) throws ComponentException { try { - return this.blocks.getElement(tlvType); + return this.blocks.getBlockByType(tlvType); } catch (BlockException e) { throw new ComponentException("MutableDangerousField.getBlockByType: "+e.getMessage()); } diff --git a/src/main/java/component/MutableProtectField.java b/src/main/java/component/MutableProtectField.java index 9d59ffb..4b73637 100644 --- a/src/main/java/component/MutableProtectField.java +++ b/src/main/java/component/MutableProtectField.java @@ -57,7 +57,7 @@ public class MutableProtectField implements TlvComponentBase, IEncodingAble { */ public Block getBlockByType(VlInt tlvType) throws ComponentException { try { - return this.blocks.getElement(tlvType); + return this.blocks.getBlockByType(tlvType); } catch (BlockException e) { throw new ComponentException("MutableProtectField.getBlockByType: "+e.getMessage()); } diff --git a/src/main/java/component/ReadOnlyField.java b/src/main/java/component/ReadOnlyField.java index 7f0eed3..0d1d94d 100644 --- a/src/main/java/component/ReadOnlyField.java +++ b/src/main/java/component/ReadOnlyField.java @@ -61,7 +61,7 @@ public class ReadOnlyField implements TlvComponentBase, IEncodingAble { */ public Block getBlockByType(VlInt tlvType) throws ComponentException { try { - return this.blocks.getElement(tlvType); + return this.blocks.getBlockByType(tlvType); } catch (BlockException e) { throw new ComponentException("ReadOnlyField.getBlockType: " + e.getMessage()); } diff --git a/src/main/java/component/Signature.java b/src/main/java/component/Signature.java index af34680..c81173c 100644 --- a/src/main/java/component/Signature.java +++ b/src/main/java/component/Signature.java @@ -34,7 +34,7 @@ public class Signature { * @param identity * @param value */ - public Signature(Identity identity,byte[] value){ + public Signature(Identity identity,byte[] value) throws ComponentException { // 往包中添加签名 KeyLocator keyLocator=new KeyLocator(identity.getName()); this.sigInfo=new SignatureInfo( diff --git a/src/main/java/component/SignatureField.java b/src/main/java/component/SignatureField.java index 29dcfe7..87ee0fd 100644 --- a/src/main/java/component/SignatureField.java +++ b/src/main/java/component/SignatureField.java @@ -50,9 +50,9 @@ class SignatureContainer { * @param index * @return */ - public Signature getElement(int index) { + public Signature getElement(int index) throws ComponentException { if (index >= length()) { - return null; + throw new ComponentException("Signatures.getElement: index is out of bound."); } return signatures.get(index); } @@ -99,7 +99,7 @@ public class SignatureField implements TlvComponentBase, IEncodingAble { * @param index * @return */ - public Signature getSignature(int index) { + public Signature getSignature(int index) throws ComponentException { return signatures.getElement(index); } diff --git a/src/main/java/component/SignatureInfo.java b/src/main/java/component/SignatureInfo.java index 5406d0b..d4386d7 100644 --- a/src/main/java/component/SignatureInfo.java +++ b/src/main/java/component/SignatureInfo.java @@ -37,7 +37,7 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble { * @param value * @param locator */ - public SignatureInfo(SignatureAlgorithm value,KeyLocator locator){ + public SignatureInfo(SignatureAlgorithm value,KeyLocator locator) throws ComponentException { this.buildSignatureInfoBySignatureTypeValueAndKeyLocator(value,locator); } @@ -64,7 +64,7 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble { * @param locator * @return */ - public boolean buildSignatureInfoBySignatureTypeValueAndKeyLocator(SignatureAlgorithm value,KeyLocator locator){ + public boolean buildSignatureInfoBySignatureTypeValueAndKeyLocator(SignatureAlgorithm value,KeyLocator locator) throws ComponentException { if(!this.signatureType.buildSignatureTypeByValue(value)){ return false; } diff --git a/src/main/java/encoding/Block.java b/src/main/java/encoding/Block.java index 4a6a17b..3f631be 100644 --- a/src/main/java/encoding/Block.java +++ b/src/main/java/encoding/Block.java @@ -2,10 +2,6 @@ package encoding; import component.ComponentException; import util.ByteHelper; -import util.ByteHelperException; - -import java.util.LinkedList; -import java.util.List; /* * @Author: Wang Feng @@ -302,7 +298,7 @@ public class Block { * @return */ public Block getElement(VlInt tlvType) throws BlockException { - return this.elements.getElement(tlvType); + return this.elements.getBlockByType(tlvType); } /** diff --git a/src/main/java/encoding/ElementContainer.java b/src/main/java/encoding/ElementContainer.java index 168eff0..63a29df 100644 --- a/src/main/java/encoding/ElementContainer.java +++ b/src/main/java/encoding/ElementContainer.java @@ -69,18 +69,18 @@ public class ElementContainer{ * @param tlvType * @return */ - public Block getElement(VlInt tlvType) throws BlockException { + public Block getBlockByType(VlInt tlvType) throws BlockException { for(Block block: this.elementContainer){ if(block.getType().isEqual(tlvType)){ return block; } } - throw new BlockException("ReadOnlyField.getBlock: no found the tlvType"); + throw new BlockException("Blocks.getBlockByType: no found the tlvType"); } public Block getBlock(int index) throws BlockException { if(index>=this.length()){ - throw new BlockException("ReadOnlyField.getBlock: index is out of bound."); + throw new BlockException("Blocks.getBlock: index is out of bound."); } return this.elementContainer.get(index); diff --git a/src/main/java/packet/Data.java b/src/main/java/packet/Data.java index 9e13cb5..8918e81 100644 --- a/src/main/java/packet/Data.java +++ b/src/main/java/packet/Data.java @@ -45,7 +45,7 @@ public class Data implements IEncodingAble, InteractWithField { * * @return */ - public String toUri() { + public String toUri() throws ComponentException { if (this.name == null) { return ""; } diff --git a/src/main/java/packet/Interest.java b/src/main/java/packet/Interest.java index 38f8e7e..7d20de9 100644 --- a/src/main/java/packet/Interest.java +++ b/src/main/java/packet/Interest.java @@ -101,7 +101,7 @@ public class Interest implements InteractWithField, IEncodingAble { * * @return */ - public String toUri() { + public String toUri() throws ComponentException { return this.name.toUri(); }