error handle to all: EncoderException

This commit is contained in:
free will committed 2021-03-18 11:22:49 +08:00
1 parent 5fc080ca6c
commit 8cf6ed24c9
34 files changed
+700 -233

No files matched your search

+6 -6
View File
@@ -43,25 +43,25 @@ public class CanBePrefix implements TlvComponentBase,InitialAble, IEncodingAble
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
// 这是一个存在性组件,所以如果值为false,则无需编码
if(!this.canBePrefix){
return 0;
return -1;
}
int totalLength=0;
// 编码 TLV-LENGTH
int tmplen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvCanBePrefix));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
+9 -13
View File
@@ -43,27 +43,27 @@ public class CongestionMark implements TlvComponentBase,InitialAble, IEncodingAb
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=encoder.prependNonNegativeInteger(this.congestionMark);
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvCongestionMark));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
@@ -76,7 +76,7 @@ public class CongestionMark implements TlvComponentBase,InitialAble, IEncodingAb
* @return
*/
@Override
public boolean wireDecode(Block block) {
public boolean wireDecode(Block block) throws VlIntException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvCongestionMark))){
return false;
@@ -84,11 +84,7 @@ public class CongestionMark implements TlvComponentBase,InitialAble, IEncodingAb
// 读取 CongestionMark
long value= 0;
try {
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
} catch (VlIntException e) {
e.printStackTrace();
}
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
if(value<0){
return false;
}
+9 -13
View File
@@ -43,27 +43,27 @@ public class FreshnessPeriod implements TlvComponentBase,InitialAble, IEncodingA
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=encoder.prependNonNegativeInteger(this.freshnessPeriod);
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvFreshnessPeriod));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
@@ -76,7 +76,7 @@ public class FreshnessPeriod implements TlvComponentBase,InitialAble, IEncodingA
* @return
*/
@Override
public boolean wireDecode(Block block) {
public boolean wireDecode(Block block) throws VlIntException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvFreshnessPeriod))){
return false;
@@ -84,11 +84,7 @@ public class FreshnessPeriod implements TlvComponentBase,InitialAble, IEncodingA
// 读取 value
long value= 0;
try {
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
} catch (VlIntException e) {
e.printStackTrace();
}
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
if(value<0){
return false;
}
+9 -13
View File
@@ -43,27 +43,27 @@ public class HopLimit implements TlvComponentBase,InitialAble, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=encoder.prependNonNegativeInteger(this.hopLimit);
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvHopLimit));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
@@ -71,7 +71,7 @@ public class HopLimit implements TlvComponentBase,InitialAble, IEncodingAble {
}
@Override
public boolean wireDecode(Block block) {
public boolean wireDecode(Block block) throws VlIntException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvHopLimit))){
return false;
@@ -79,11 +79,7 @@ public class HopLimit implements TlvComponentBase,InitialAble, IEncodingAble {
// 读取 HopLimit
long value= 0;
try {
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
} catch (VlIntException e) {
e.printStackTrace();
}
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
if(value<0){
return false;
}
+4 -4
View File
@@ -184,13 +184,13 @@ public class Identifier implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
int totalLength=0;
// 编码 TLV-VALUE
for (int i = 0; i < components.length(); i++) {
int tmpLen=components.getElement(i).wireEncode(encoder);
if(tmpLen<=0){
return 0;
return -1;
}
totalLength+=tmpLen;
}
@@ -198,14 +198,14 @@ public class Identifier implements TlvComponentBase, IEncodingAble {
// 编码 TLV-LENGTH
int tmpLen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmpLen<=0){
return 0;
return -1;
}
totalLength+=tmpLen;
// 编码 TLV-TYPE
tmpLen=encoder.prependVarNumber(new VlInt(TLV.TlvIdentifier));
if(tmpLen<=0){
return 0;
return -1;
}
totalLength+=tmpLen;
@@ -120,7 +120,11 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
* @return
*/
public IdentifierComponent(Block block){
this.wireDecode(block);
try {
this.wireDecode(block);
} catch (VlIntException e) {
throw new Error("IdentifierComponent: wireDecode error. "+e.getMessage());
}
}
/**
@@ -177,7 +181,7 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
return this.identifierBlock.encode(encoder);
}
@@ -187,16 +191,12 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) {
public boolean wireDecode(Block block) throws VlIntException {
this.identifierBlock=new Block(block);
//todo:越界问题
int length= 0;
try {
length = this.identifierBlock.getLength().getVlIntValue2Int();
} catch (VlIntException e) {
e.printStackTrace();
}
length = this.identifierBlock.getLength().getVlIntValue2Int();
byte[] value=this.identifierBlock.getValue();
// 要求TLV-LENGTH至少为1,因为 IdentifierComponent 使用 TLV-VALUE 中的第一个字节作为Marker
+4 -4
View File
@@ -139,7 +139,7 @@ public class IdentifierField implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
// 如果标识区不存在标识,则是一个无效的MIN包,直接抛出错误
if(this.identifiers.length()==0){
return 0;
@@ -150,7 +150,7 @@ public class IdentifierField implements TlvComponentBase, IEncodingAble {
for (int i = this.identifiers.length()-1; i >= 0; i--) {
int tmplen=this.identifiers.getElement(i).wireEncode(encoder);
if(tmplen<=0){
return 0;
return -1;
}
totalLength+=tmplen;
}
@@ -158,14 +158,14 @@ public class IdentifierField implements TlvComponentBase, IEncodingAble {
// 编码 TLV-LENGTH
int tmplen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen<=0){
return 0;
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvIdentifierField));
if(tmplen<=0){
return 0;
return -1;
}
totalLength+=tmplen;
@@ -264,27 +264,27 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=this.identifier.wireEncode(encoder);
if(tmplen<=0){
return 0;
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen<=0){
return 0;
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(this.tlvType);
if(tmplen<=0){
return 0;
return -1;
}
totalLength+=tmplen;
+9 -13
View File
@@ -43,27 +43,27 @@ public class InterestLifeTime implements TlvComponentBase,InitialAble, IEncoding
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=encoder.prependNonNegativeInteger(this.interestLifeTime);
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvInterestLifeTime));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
@@ -76,7 +76,7 @@ public class InterestLifeTime implements TlvComponentBase,InitialAble, IEncoding
* @return
*/
@Override
public boolean wireDecode(Block block) {
public boolean wireDecode(Block block) throws VlIntException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvInterestLifeTime))){
return false;
@@ -84,11 +84,7 @@ public class InterestLifeTime implements TlvComponentBase,InitialAble, IEncoding
// 读取 value
long value= 0;
try {
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
} catch (VlIntException e) {
e.printStackTrace();
}
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
if(value<0){
return false;
}
+4 -4
View File
@@ -105,26 +105,26 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=this.identifier.wireEncode(encoder);
if(tmplen<=0){
return 0;
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen<=0){
return 0;
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen<=0){
return 0;
return -1;
}
totalLength+=tmplen;
+6 -6
View File
@@ -43,25 +43,25 @@ public class MustBeRefresh implements TlvComponentBase,InitialAble, IEncodingAbl
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
// 这是一个存在性组件,所以如果值为false,则无需编码
if(!this.mustBeRefresh){
return 0;
return -1;
}
int totalLength=0;
// 编码 TLV-LENGTH
int tmplen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvMustBeRefresh));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
@@ -57,10 +57,10 @@ public class MutableDangerousField implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
// 如果可变非受保护区没有任何子 TLV,则可以去掉可变非受保护区 TLV
if(this.blocks.length()==0){
return 0;
return -1;
}
int totalLength=0;
@@ -68,23 +68,23 @@ public class MutableDangerousField implements TlvComponentBase, IEncodingAble {
// 反向遍历,可以保证解码的时候正向输出
for(int i=this.blocks.length()-1;i>=0;i++){
int tmplen=encoder.prependBlock(this.blocks.getBlock(i));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
}
// 编码 TLV-LENGTH
int tmplen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvReadOnlyField));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
+11 -11
View File
@@ -24,37 +24,37 @@ public class MutableField implements TlvComponentBase,IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
int totalLength=0;
// 编码 TLV—VALUE
int tmplen=this.mutableDangerousField.wireEncode(encoder);
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
tmplen=this.mutableProtectField.wireEncode(encoder);
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 如果可变保护区和可变非受保护区均为空,则可以去掉可变区
if(totalLength==0){
return 0;
if(totalLength<=0){
return -1;
}
// 编码 TLV-LENGTH
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvMutableField));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
@@ -57,10 +57,10 @@ public class MutableProtectField implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
// 如果没有任何子 TLV,则可以去掉它
if(this.blocks.length()==0){
return 0;
return -1;
}
int totalLength=0;
@@ -68,23 +68,23 @@ public class MutableProtectField implements TlvComponentBase, IEncodingAble {
// 反向遍历,可以保证解码的时候正向输出
for(int i=this.blocks.length()-1;i>=0;i++){
int tmplen=encoder.prependBlock(this.blocks.getBlock(i));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
}
// 编码 TLV-LENGTH
int tmplen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvReadOnlyField));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
+9 -13
View File
@@ -43,27 +43,27 @@ public class NackHeader implements TlvComponentBase,InitialAble, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=encoder.prependNonNegativeInteger(this.nackReason);
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvNackHeader));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
@@ -76,7 +76,7 @@ public class NackHeader implements TlvComponentBase,InitialAble, IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) {
public boolean wireDecode(Block block) throws VlIntException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvNackHeader))){
return false;
@@ -84,11 +84,7 @@ public class NackHeader implements TlvComponentBase,InitialAble, IEncodingAble {
// 读取 value
long value= 0;
try {
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
} catch (VlIntException e) {
e.printStackTrace();
}
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
if(value<0){
return false;
}
+9 -13
View File
@@ -53,27 +53,27 @@ public class Nonce implements TlvComponentBase,InitialAble, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=encoder.prependNonNegativeInteger(this.nonce);
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvNonce));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
@@ -86,7 +86,7 @@ public class Nonce implements TlvComponentBase,InitialAble, IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) {
public boolean wireDecode(Block block) throws VlIntException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvNonce))){
return false;
@@ -94,11 +94,7 @@ public class Nonce implements TlvComponentBase,InitialAble, IEncodingAble {
// 读取 value
long value= 0;
try {
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
} catch (VlIntException e) {
e.printStackTrace();
}
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
if(value<0){
return false;
}
+7 -7
View File
@@ -63,27 +63,27 @@ public class Payload implements TlvComponentBase,InitialAble, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=encoder.prependByteArray(this.value,new SizeT(this.value.length));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvPayload));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
+7 -7
View File
@@ -57,7 +57,7 @@ public class ReadOnlyField implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
// 如果只读区没有任何子 TLV,则可以去掉只读区 TLV
if(this.blocks.length()==0){
return 0;
@@ -68,23 +68,23 @@ public class ReadOnlyField implements TlvComponentBase, IEncodingAble {
// 反向遍历,可以保证解码的时候正向输出
for(int i=this.blocks.length()-1;i>=0;i++){
int tmplen=encoder.prependBlock(this.blocks.getBlock(i));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
}
// 编码 TLV-LENGTH
int tmplen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvReadOnlyField));
if(tmplen==0){
return 0;
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
+8 -7
View File
@@ -102,10 +102,10 @@ public class SignatureField implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
// 如果签名区没有签名,则可以直接去掉签名区 TLV,因为它不是必须的
if(signatures.length()==0){
return 0;
return -1;
}
int totalLength=0;
// 编码 TLV-VALUE
@@ -114,12 +114,12 @@ public class SignatureField implements TlvComponentBase, IEncodingAble {
Signature newSignature=signatures.getElement(i);
int tmplen=newSignature.getSigValue().wireEncode(encoder);
if(tmplen<=0){
return 0;
return -1;
}
totalLength+=tmplen;
tmplen=newSignature.getSigInfo().wireEncode(encoder);
if(tmplen<=0){
return 0;
return -1;
}
totalLength+=tmplen;
}
@@ -127,14 +127,14 @@ public class SignatureField implements TlvComponentBase, IEncodingAble {
// 编码签名区 TLV-LENGTH
int tmplen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen<=0){
return 0;
return -1;
}
totalLength+=tmplen;
// 编码签名区 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvSignatureField));
if(tmplen<=0){
return 0;
return -1;
}
totalLength+=tmplen;
@@ -160,7 +160,8 @@ public class SignatureField implements TlvComponentBase, IEncodingAble {
ElementContainer subElements=block.getSubElements();
int subElementNum=subElements.length();
if(subElementNum%2!=0){
throw new Error("SignatureField.wireDecode: Invalid SignatureField TLV, require even num sub element");
// throw new Error("SignatureField.wireDecode: Invalid SignatureField TLV, require even num sub element");
return false;
}
// 存储所有解析到的签名
+4 -4
View File
@@ -100,27 +100,27 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
int totalLength=0;
// 编码 TLV-VALUE
if(!this.keyLocator.isValid()){
// KeyLocator 是可选的,如果当前 SignatureInfo 对象有一个有效的 KeyLocator,则对其进行编码
int tmplen=this.keyLocator.wireEncode(encoder);
if(tmplen<=0){
return 0;
return -1;
}
totalLength+=tmplen;
}
int tmplen=this.signatureType.wireEncode(encoder);
if(tmplen<=0){
return 0;
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen<=0){
return 0;
return -1;
}
totalLength+=tmplen;
+11 -11
View File
@@ -57,7 +57,7 @@ public class SignatureType implements TlvComponentBase, IEncodingAble {
* 根据 TLV Block 创建一个 SignatureType
* @param block
*/
public SignatureType(Block block){
public SignatureType(Block block) throws VlIntException {
this.buildSignatureTypeByBlock(block);
}
@@ -83,7 +83,7 @@ public class SignatureType implements TlvComponentBase, IEncodingAble {
* @param block
* @return
*/
public boolean buildSignatureTypeByBlock(Block block){
public boolean buildSignatureTypeByBlock(Block block) throws VlIntException {
return this.wireDecode(block);
}
@@ -101,27 +101,27 @@ public class SignatureType implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=encoder.prependNonNegativeInteger(this.value.getSignatureAlgorithm());
if(tmplen<=0){
return 0;
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen<=0){
return 0;
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvSignatureType));
if(tmplen<=0){
return 0;
return -1;
}
totalLength+=tmplen;
@@ -134,7 +134,7 @@ public class SignatureType implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) {
public boolean wireDecode(Block block) throws VlIntException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvSignatureType))){
return false;
@@ -142,11 +142,11 @@ public class SignatureType implements TlvComponentBase, IEncodingAble {
// 尝试从 TLE-VALUE 中读取出一个非负整数
long result= 0;
try {
// try {
result = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
} catch (VlIntException e) {
throw new Error("SignatureType.wireDecode: Invalid SignatureType TLV");
}
// } catch (VlIntException e) {
// throw new Error("SignatureType.wireDecode: Invalid SignatureType TLV");
// }
if(result<0){
return false;
}
+11 -11
View File
@@ -27,7 +27,7 @@ public class SignatureValue implements TlvComponentBase, IEncodingAble {
* 根据 TLV Block 创建一个 SignatureValue
* @param block
*/
public SignatureValue(Block block){
public SignatureValue(Block block) throws VlIntException {
this.buildSignatureValueByBlock(block);
}
@@ -47,7 +47,7 @@ public class SignatureValue implements TlvComponentBase, IEncodingAble {
* @param block
* @return
*/
public boolean buildSignatureValueByBlock(Block block){
public boolean buildSignatureValueByBlock(Block block) throws VlIntException {
return this.wireDecode(block);
}
@@ -65,26 +65,26 @@ public class SignatureValue implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=encoder.prependByteArray(this.value,new SizeT(this.value.length));
if(tmplen<=0){
return 0;
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen<=0){
return 0;
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvSignatureValue));
if(tmplen<=0){
return 0;
return -1;
}
totalLength+=tmplen;
@@ -97,18 +97,18 @@ public class SignatureValue implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) {
public boolean wireDecode(Block block) throws VlIntException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvSignatureValue))){
return false;
}
int len= 0;
try {
// try {
len = block.getLength().getVlIntValue2Int();
} catch (VlIntException e) {
e.printStackTrace();
}
// } catch (VlIntException e) {
// e.printStackTrace();
// }
this.value=new byte[len];
System.arraycopy(block.getValue(),0,this.value,0,len);
return true;
+14 -14
View File
@@ -22,7 +22,7 @@ public class TTL implements IEncodingAble {
public TTL(){
}
public TTL(Block block){
public TTL(Block block) throws VlIntException {
buildTTLByBlock(block);
}
@@ -31,7 +31,7 @@ public class TTL implements IEncodingAble {
* @param block
* @return
*/
public boolean buildTTLByBlock(Block block){
public boolean buildTTLByBlock(Block block) throws VlIntException {
return this.wireDecode(block);
}
@@ -57,26 +57,26 @@ public class TTL implements IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
int totalLength=0;
// 编码 TLV-VALUE
int tmpLen=encoder.prependNonNegativeInteger(this.ttl);
if(tmpLen==0){
return 0;
if(tmpLen<=0){
return -1;
}
totalLength+=tmpLen;
// 编码 TLV-LENGTH
tmpLen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmpLen==0){
return 0;
if(tmpLen<=0){
return -1;
}
totalLength+=tmpLen;
// 编码 TLV-TYPE
tmpLen=encoder.prependVarNumber(new VlInt(TLV.TlvTTL));
if(tmpLen==0){
return 0;
if(tmpLen<=0){
return -1;
}
totalLength+=tmpLen;
@@ -89,17 +89,17 @@ public class TTL implements IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) {
public boolean wireDecode(Block block) throws VlIntException {
// 检查Type是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvTTL))){
return false;
}
long value= 0;
try {
// try {
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
} catch (VlIntException e) {
e.printStackTrace();
}
// } catch (VlIntException e) {
// e.printStackTrace();
// }
if(value<0){
return false;
}
+12 -12
View File
@@ -32,7 +32,7 @@ public class Block {
* 则会对比解析到的 TLV-LENGTH 与 TLV-VALUE的实际长度是否匹配
* @return
*/
public static Block createBlockByBuffer(byte[] buffer,boolean verifyLength){
public static Block createBlockByBuffer(byte[] buffer,boolean verifyLength) throws EncoderException {
if(buffer.length==0){
return null;
}
@@ -67,7 +67,7 @@ public class Block {
* @param verifyLength
* @return
*/
public static Block createBlockByTypeLengthBuffer(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength){
public static Block createBlockByTypeLengthBuffer(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength) throws EncoderException {
return new Block(tlvType,tlvLength,buffer,verifyLength);
}
@@ -78,33 +78,33 @@ public class Block {
* @param buffer
* @param verifyLength
*/
public Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength){
public Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength) throws EncoderException {
this.tlvType=tlvType;
this.length=tlvLength;
// 检查长度是否正确
if(verifyLength && (!this.length.isEqual(buffer.length))){
//todo: 抛出异常
throw new Error("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): invalid length.");
}
// 设置 Raw
Encoder encoder=new Encoder();
boolean resetBool=encoder.encoderReset(new SizeT(tlvType.size()+tlvLength.size()+buffer.length),new SizeT(0));
if(!resetBool){
//todo: 抛出异常
throw new Error("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): encoderReset error");
}
if(encoder.prependByteArray(buffer,new SizeT(buffer.length))==0){
//todo: 抛出异常
throw new Error("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): prepenndByteArray buffer error");
}
if(encoder.prependVarNumber(tlvLength)==0){
//todo: 抛出异常
throw new Error("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): prependVarNumber tlvLength error");
}
if(encoder.prependVarNumber(tlvType)==0){
//todo: 抛出异常
throw new Error("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): prependVarNumber tlvType error");
}
byte[] encodedBuffer=encoder.getBuffer();
if(encodedBuffer==null){
//todo: 抛出异常
throw new Error("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): encoder.getBuffer error");
}
this.raw=encodedBuffer;
@@ -248,8 +248,8 @@ public class Block {
}
this.addElement(block);
start+=tlvLength.getVlIntValue2Int();
} catch (VlIntException e) {
e.printStackTrace();
} catch (VlIntException | EncoderException e) {
return false;
}
}
return true;
@@ -302,7 +302,7 @@ public class Block {
* @param encoder
* @return
*/
public int encode(Encoder encoder){
public int encode(Encoder encoder) throws EncoderException {
// 如果已经有raw,则说明编码过,或者是解码而来的,不用在此Encode
if(this.hasRaw()){
return encoder.prependByteArray(this.raw,new SizeT(this.raw.length));
+2 -2
View File
@@ -15,13 +15,13 @@ public interface IEncodingAble {
* @param encoder
* @return
*/
public int wireEncode(Encoder encoder);
public int wireEncode(Encoder encoder) throws EncoderException;
/**
* 线速解码,将一个TLV解码成一个网络包
* @param block
* @return
*/
public boolean wireDecode(Block block);
public boolean wireDecode(Block block) throws VlIntException;
}
+2 -2
View File
@@ -44,7 +44,7 @@ public class SelfEncodingBase {
* @param iEncodingAble
* @return
*/
public Block selfWireEncode(IEncodingAble iEncodingAble){
public Block selfWireEncode(IEncodingAble iEncodingAble) throws EncoderException {
// this.setiEncodingAble(iEncodingAble);
if((this.rawBlock!=null)&&this.rawBlock.isValid()){
return this.rawBlock;
@@ -64,7 +64,7 @@ public class SelfEncodingBase {
* @param iEncodingAble
* @return
*/
private Block easyEncoder(IEncodingAble iEncodingAble){
private Block easyEncoder(IEncodingAble iEncodingAble) throws EncoderException {
Encoder encoder=new Encoder();
// 首先测量目标 TLV 的大小
+4 -4
View File
@@ -51,7 +51,7 @@ public class CPacket implements InteractWithField, IEncodingAble {
* @return
*/
@Override
public boolean fillDataToFields() {
public boolean fillDataToFields() throws EncoderException{
// 可变区 => 受保护区(无)
// 填充可变区 => 非受保护区
@@ -90,7 +90,7 @@ public class CPacket implements InteractWithField, IEncodingAble {
* @return
*/
@Override
public boolean extraDataFromFields() {
public boolean extraDataFromFields() throws VlIntException {
// 提取 Payload
Block payloadBlock=this.minPacket.readOnlyField.getBlockByType(new VlInt(TLV.TlvPayload));
if(payloadBlock==null){
@@ -128,7 +128,7 @@ public class CPacket implements InteractWithField, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
// 首先将属性填充到分区当中
if(!this.fillDataToFields()){
return 0;
@@ -146,7 +146,7 @@ public class CPacket implements InteractWithField, IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) {
public boolean wireDecode(Block block) throws VlIntException {
if(!this.minPacket.wireDecode(block)){
return false;
}
+4 -4
View File
@@ -68,7 +68,7 @@ public class Data implements IEncodingAble,InteractWithField {
* @return
*/
@Override
public boolean fillDataToFields() {
public boolean fillDataToFields() throws EncoderException {
/////////////////////////////////////////////////////////////
//// 填充可变区
// { => 可变区
@@ -146,7 +146,7 @@ public class Data implements IEncodingAble,InteractWithField {
* @return
*/
@Override
public boolean extraDataFromFields() {
public boolean extraDataFromFields() throws VlIntException {
/////////////////////////////////////////////////////////////
//// 解析可变区
// { => 可变区
@@ -216,7 +216,7 @@ public class Data implements IEncodingAble,InteractWithField {
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
// 首先将各项属性填充到分区当中
if(this.fillDataToFields()){
return 0;
@@ -234,7 +234,7 @@ public class Data implements IEncodingAble,InteractWithField {
* @return
*/
@Override
public boolean wireDecode(Block block) {
public boolean wireDecode(Block block) throws VlIntException {
if(!this.minPacket.wireDecode(block)){
return false;
}
+5 -2
View File
@@ -1,5 +1,8 @@
package packet;
import encoding.EncoderException;
import encoding.VlIntException;
/*
* @Author: Wang Feng
* @Description: 定义了一个网络包应该实现哪些和分区交互的方法
@@ -12,11 +15,11 @@ public interface InteractWithField {
* 将包的属性填充到网络包的四个分区当中
* @return
*/
boolean fillDataToFields();
boolean fillDataToFields() throws EncoderException;
/**
* 从网络包的四个分区当中提取出特定网络包的相关属性
* @return
*/
boolean extraDataFromFields();
boolean extraDataFromFields() throws VlIntException;
}
+4 -4
View File
@@ -104,7 +104,7 @@ public class Interest implements InteractWithField,IEncodingAble{
* @return
*/
@Override
public boolean fillDataToFields() {
public boolean fillDataToFields() throws EncoderException {
/////////////////////////////////////////////////////////////
//// 填充可变区
/////////////////////////////////////////////////////////////
@@ -201,7 +201,7 @@ public class Interest implements InteractWithField,IEncodingAble{
* @return
*/
@Override
public boolean extraDataFromFields() {
public boolean extraDataFromFields() throws VlIntException {
/////////////////////////////////////////////////////////////
//// 解析可变区
// { => 可变区
@@ -292,7 +292,7 @@ public class Interest implements InteractWithField,IEncodingAble{
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
// 首先将各项属性填充到分区当中
if(!this.fillDataToFields()){
return 0;
@@ -310,7 +310,7 @@ public class Interest implements InteractWithField,IEncodingAble{
* @return
*/
@Override
public boolean wireDecode(Block block) {
public boolean wireDecode(Block block) throws VlIntException {
if(!this.minPacket.wireDecode(block)){
return false;
}
+1 -1
View File
@@ -35,7 +35,7 @@ public class MINPacket implements IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
public int wireEncode(Encoder encoder) throws EncoderException {
if(!this.isPacketValid()){
return 0;
}