diff --git a/src/main/java/component/Identifier.java b/src/main/java/component/Identifier.java index 6ec51b2..689d4e1 100644 --- a/src/main/java/component/Identifier.java +++ b/src/main/java/component/Identifier.java @@ -114,6 +114,7 @@ public class Identifier implements TlvComponentBase, IEncodingAble { * @param block */ public Identifier(Block block) throws ComponentException { + components = new IdentifierComponentContainer(); this.buildIdentifierByBlock(block); } @@ -122,8 +123,12 @@ public class Identifier implements TlvComponentBase, IEncodingAble { * * @param identifierString */ - public Identifier(String identifierString) { - this.buildIdentifierByString(identifierString); + public Identifier(String identifierString) throws ComponentException { + components = new IdentifierComponentContainer(); + if(!this.buildIdentifierByString(identifierString)){ + throw new ComponentException + ("Identifier(String): identifierString does not starts with \"/\". "); + } } /** @@ -132,6 +137,7 @@ public class Identifier implements TlvComponentBase, IEncodingAble { * @param container */ public Identifier(IdentifierComponentContainer container) throws ComponentException { + components = new IdentifierComponentContainer(); this.buildIdentifierByComponents(container); } @@ -327,13 +333,13 @@ public class Identifier implements TlvComponentBase, IEncodingAble { } /** - * 获取前 num 个名称组件构成的标识 + * 获取前 num 个名称组件构成的新标识 * * @param num * @return */ - public IdentifierComponentContainer getPrefix(int num) throws ComponentException { - return this.components.getPrefix(num); + public Identifier getPrefix(int num) throws ComponentException { + return new Identifier(this.components.getPrefix(num)); } /** @@ -353,4 +359,12 @@ public class Identifier implements TlvComponentBase, IEncodingAble { public boolean isValid() { return this.components.length() > 0; } + + /** + * 标识后面新增一个名称组件 + * @param component + */ + public void append(IdentifierComponent component){ + this.components.addElement(component); + } } diff --git a/src/main/java/component/IdentifierComponent.java b/src/main/java/component/IdentifierComponent.java index c8e4dab..a289391 100644 --- a/src/main/java/component/IdentifierComponent.java +++ b/src/main/java/component/IdentifierComponent.java @@ -14,6 +14,7 @@ import util.ByteHelper; import util.ByteHelperException; import util.StringHelper; +import java.nio.charset.StandardCharsets; import java.util.Arrays; // @Description: @@ -69,15 +70,11 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble { public IdentifierComponent(String nameComponent) { this.identifierBlock = new Block(); this.identifierBlock.setType(new VlInt(TLV.TlvIdentifierComponent)); - this.identifierBlock.setLength(new VlInt(nameComponent.length() + 1)); - byte[] value = new byte[0]; - try { - value = new byte[this.identifierBlock.getLength().getVlIntValue2Int()]; - } catch (VlIntException e) { - e.printStackTrace(); - } + int len=nameComponent.getBytes(StandardCharsets.UTF_8).length; + this.identifierBlock.setLength(new VlInt(len + 1)); + byte[] value = new byte[len+1]; value[0] = MarkerString; - System.arraycopy(nameComponent.getBytes(), 0, value, 1, nameComponent.getBytes().length); + System.arraycopy(nameComponent.getBytes(StandardCharsets.UTF_8), 0, value, 1, nameComponent.getBytes().length); this.identifierBlock.setValue(value); this.marker = MarkerString; this.stringValue = nameComponent; @@ -93,12 +90,7 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble { this.identifierBlock.setType(new VlInt(TLV.TlvIdentifierComponent)); byte[] uint64Array=Encoder.buildNonNegativeIntegerArr(intValue); this.identifierBlock.setLength(new VlInt( uint64Array.length+ 1)); - byte[] value = new byte[0]; - try { - value = new byte[this.identifierBlock.getLength().getVlIntValue2Int()]; - } catch (VlIntException e) { - e.printStackTrace(); - } + byte[] value = new byte[uint64Array.length+1]; value[0] = MarkerNonNegativeInteger; System.arraycopy(uint64Array, 0, value, 1, uint64Array.length); this.identifierBlock.setValue(value); @@ -115,12 +107,7 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble { this.identifierBlock = new Block(); this.identifierBlock.setType(new VlInt(TLV.TlvIdentifierComponent)); this.identifierBlock.setLength(new VlInt(arr.length + 1)); - byte[] value = new byte[0]; - try { - value = new byte[this.identifierBlock.getLength().getVlIntValue2Int()]; - } catch (VlIntException e) { - e.printStackTrace(); - } + byte[] value = new byte[arr.length+1]; value[0] = MarkerByteArray; System.arraycopy(arr, 0, value, 1, arr.length); this.identifierBlock.setValue(value); @@ -309,6 +296,11 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble { @Override public boolean wireDecode(Block block) throws ComponentException { try { + // 检查 Type 是否正确 + if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvIdentifierComponent))) { + return false; + } + this.identifierBlock = new Block(block); //todo:越界问题 @@ -357,12 +349,12 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble { public String toUri() { if (isString()) { return StringHelper.escape(this.stringValue); - } else if (isNonNegativeInteger()) { + } else if(isByteArray()){ + return StringHelper.escape(new String(this.byteArrayValue)); + }else{ // return StringHelper.FormatUint(this.intValue,10); // todo: 需要对long类型按照指定编码规则编码成字符串 return ""; - } else { - return StringHelper.escape(new String(this.byteArrayValue)); } } } diff --git a/src/test/java/component/IdentifierTest.java b/src/test/java/component/IdentifierTest.java new file mode 100644 index 0000000..f55af71 --- /dev/null +++ b/src/test/java/component/IdentifierTest.java @@ -0,0 +1,62 @@ +package component; + +import org.junit.Test; +import util.ByteHelper; +import util.StringHelper; + +/* + * @Author: Wang Feng + * @Description: + * @Version: 1.0.0 + * @Date: 15:27 2021/3/29 + * @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室 + */ +public class IdentifierTest { + @Test + public void TestCreateIdentifierByString() throws ComponentException { + String str="/min/pkusz/王锋"; + Identifier got=new Identifier(str); + System.out.println(got.size()); + String encode=got.toUri(); + System.out.println(encode); + System.out.println(StringHelper.unescape(encode)); + } + + @Test + public void TestIdentifier_Get() throws ComponentException { + String str="/min/王锋"; + Identifier got=new Identifier(str); + for (int i = 0; i < got.size(); i++) { + System.out.println(got.get(i).toUri()); + } + } + + @Test + public void TestIdentifier_GetPrefix() throws ComponentException { + String str="/min/pku/王锋"; + Identifier got=new Identifier(str); + for (int i = 0; i < got.size(); i++) { + System.out.println("prefix: "+got.getPrefix(i+1).toUri()); + } + } + + @Test + public void TestIdentifier_GetSubIdentifier() throws ComponentException { + String str="/min/pku/王锋"; + Identifier got=new Identifier(str); + for (int i = 0; i < got.size(); i++) { + System.out.println("prefix: "+got.getSubIdentifier(0,i+1).toUri()); + } + System.out.println("prefix: "+got.getSubIdentifier(1,2).toUri()); + } + + @Test + public void TestIdentifier_Append() throws ComponentException { + String str="/min/pku/王锋"; + Identifier got=new Identifier(str); + IdentifierComponent addcomponent=new IdentifierComponent("1"); + got.append(addcomponent); + System.out.println(got.toUri()); + System.out.println(got.getPrefix(4).toUri()); + } +}