error handle: EncoderException

This commit is contained in:
free will committed 2021-03-18 10:21:52 +08:00
1 parent d46b3c15cd
commit 5fc080ca6c
4 files changed
+41 -27

No files matched your search

+24 -24
View File
@@ -36,9 +36,9 @@ public class Encoder {
* @param reserveFromBack 默认情况下,左右指针都指向数组的最右侧,表示totalReserve指定的所有空间都是预留给 Prepend* 操作的
* reserveFromBack 可以指定需要预留多少空间给 Append*操作
*/
public boolean encoderReset(SizeT totalReserve,SizeT reserveFromBack){
public boolean encoderReset(SizeT totalReserve,SizeT reserveFromBack) throws EncoderException{
if(reserveFromBack.compareTo(totalReserve)>0){
return false;
throw new EncoderException("encoderReset: reserveFromBack > totalReserve");
}
this.isInitial=true;
this.isEstimator=false;
@@ -47,7 +47,7 @@ public class Encoder {
try {
len=totalReserve.getVlIntValue2Int();
} catch (VlIntException e) {
throw new Error("Encoder.encoderReset: "+e.getMessage());
throw new EncoderException("encoderReset: "+e.getMessage());
}
this.buffer=new byte[len]; //todo: 特殊情况下,可能越界。
this.left= (int) (totalReserve.subtract(reserveFromBack)-1);//todo: 特殊情况下,可能越界。
@@ -106,7 +106,7 @@ public class Encoder {
this.left--;
return 1;
}else{
return 0;
return -1;
}
}
@@ -124,7 +124,7 @@ public class Encoder {
this.right++;
return 1;
}else{
return 0;
return -1;
}
}
@@ -153,7 +153,7 @@ public class Encoder {
* @param uint64_value
* @return
*/
public int prependNonNegativeInteger(long uint64_value){
public int prependNonNegativeInteger(long uint64_value) throws EncoderException {
byte[] bytes=buildNonNegativeIntegerArr(uint64_value);
return this.prependByteArray(bytes,new SizeT(bytes.length));
}
@@ -164,7 +164,7 @@ public class Encoder {
* @param uint64_value
* @return
*/
public int appendNonNegativeInteger(long uint64_value){
public int appendNonNegativeInteger(long uint64_value) throws EncoderException {
byte[] bytes=buildNonNegativeIntegerArr(uint64_value);
return this.appendByteArray(bytes,new SizeT(bytes.length));
}
@@ -174,31 +174,31 @@ public class Encoder {
* @param size
* @return 返回插入成功的字节数
*/
public int prependByteArray(byte[] array,SizeT size){
public int prependByteArray(byte[] array,SizeT size) throws EncoderException {
if(check(size)&&(size.compareTo(array.length)<=0)){
if(this.isEstimator){
this.left-=array.length;
try {
return size.getVlIntValue2Int();
} catch (VlIntException e) {
throw new Error("Encoder.prependByteArray: "+e.getMessage());
throw new EncoderException("prependByteArray: "+e.getMessage());
}
}else{
try {
System.arraycopy(array,0,this.buffer,
this.left-size.getVlIntValue2Int()+1,size.getVlIntValue2Int());
} catch (VlIntException e) {
throw new Error("Encoder.prependByteArray: "+e.getMessage());
throw new EncoderException("prependByteArray: "+e.getMessage());
}
this.left-=array.length;
try {
return size.getVlIntValue2Int();
} catch (VlIntException e) {
throw new Error("Encoder.prependByteArray: "+e.getMessage());
throw new EncoderException("prependByteArray: "+e.getMessage());
}
}
}
return 0;
return -1;
}
/**
@@ -206,31 +206,31 @@ public class Encoder {
* @param size
* @return 返回插入成功的字节数
*/
public int appendByteArray(byte[] array,SizeT size){
public int appendByteArray(byte[] array,SizeT size) throws EncoderException {
if(check(size)&&(size.compareTo(array.length)<=0)){
if(this.isEstimator){
this.right+=array.length;
try {
return size.getVlIntValue2Int();
} catch (VlIntException e) {
throw new Error("Encoder.appendByteArray: "+e.getMessage());
throw new EncoderException("appendByteArray: "+e.getMessage());
}
}else{
try {
System.arraycopy(array,0,this.buffer,
this.right,size.getVlIntValue2Int());
} catch (VlIntException e) {
throw new Error("Encoder.appendByteArray: "+e.getMessage());
throw new EncoderException("appendByteArray: "+e.getMessage());
}
this.right+=array.length;
try {
return size.getVlIntValue2Int();
} catch (VlIntException e) {
throw new Error("Encoder.appendByteArray: "+e.getMessage());
throw new EncoderException("appendByteArray: "+e.getMessage());
}
}
}
return 0;
return -1;
}
/**
@@ -238,7 +238,7 @@ public class Encoder {
* @param varNumber
* @return
*/
public int prependVarNumber(VlInt varNumber){
public int prependVarNumber(VlInt varNumber) throws EncoderException {
return this.prependByteArray(varNumber.getVlIntBytes(),new SizeT(varNumber.size()));
}
@@ -247,7 +247,7 @@ public class Encoder {
* @param varNumber
* @return
*/
public int appendVarNumber(VlInt varNumber){
public int appendVarNumber(VlInt varNumber) throws EncoderException {
return this.appendByteArray(varNumber.getVlIntBytes(),new SizeT(varNumber.size()));
}
@@ -260,7 +260,7 @@ public class Encoder {
* @param size
* @return
*/
public int prependByteArrayBlock(VlInt tlvType,byte[] array,SizeT size){
public int prependByteArrayBlock(VlInt tlvType,byte[] array,SizeT size) throws EncoderException {
int totalLength=this.prependByteArray(array,size);
totalLength+=this.prependVarNumber(new VlInt(size));
totalLength+=this.prependVarNumber(tlvType);
@@ -274,12 +274,12 @@ public class Encoder {
* @param size
* @return
*/
public int appendByteArrayBlock(VlInt tlvType,byte[] array,SizeT size){
public int appendByteArrayBlock(VlInt tlvType,byte[] array,SizeT size) throws EncoderException {
int totalLength=this.appendByteArray(array,size);
try {
totalLength+=this.appendVarNumber(new VlInt(size.getVlIntValue2Long()));
} catch (VlIntException e) {
throw new Error("Encoder.appendByteArrayBlock: "+e.getMessage());
throw new EncoderException("appendByteArrayBlock: "+e.getMessage());
}
totalLength+=this.appendVarNumber(tlvType);
return totalLength;
@@ -290,7 +290,7 @@ public class Encoder {
* @param block
* @return
*/
public int prependBlock(Block block){
public int prependBlock(Block block) throws EncoderException {
return this.prependByteArrayBlock(block.getType(),block.getValue(),new SizeT(block.getLength()));
}
@@ -299,7 +299,7 @@ public class Encoder {
* @param block
* @return
*/
public int appendBlock(Block block){
public int appendBlock(Block block) throws EncoderException {
return this.appendByteArrayBlock(block.getType(),block.getValue(),new SizeT(block.getLength()));
}
@@ -0,0 +1,14 @@
package encoding;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 9:46 2021/3/18
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class EncoderException extends Exception{
public EncoderException(String msg){
super("Encoder."+msg);
}
}
+2 -2
View File
@@ -198,7 +198,7 @@ public class VlInt {
if(this.size()<4){
return VlIntValue.intValue();
}
throw new VlIntException("value大于int能表示的范围");
throw new VlIntException("getVlIntValue2Int: out of int bound.");
}
/**
@@ -210,7 +210,7 @@ public class VlInt {
if(this.size()<= 9){
return VlIntValue.longValue();
}
throw new VlIntException("value大于long能表示的范围");
throw new VlIntException("getVlIntValue2Long: out of long bound.");
}
/**
+1 -1
View File
@@ -9,6 +9,6 @@ package encoding;
*/
public class VlIntException extends Exception{
public VlIntException(String msg){
super(msg);
super("VlInt."+msg);
}
}