mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
solve meaven error & writing encoder ...
This commit is contained in:
1 parent
4877609b75
commit
faf17d314c
7 files changed
+205
-14
No files matched your search
@@ -0,0 +1,167 @@
|
||||
package encoding;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description: 一个工具类,用来便捷的实现TLV编码
|
||||
* @Version: 1.0.0
|
||||
* @Date: 20:21 2021/3/5
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class Encoder {
|
||||
private byte[] buffer;
|
||||
private int left; // 左指针 => 指向 Prepend* 操作下一个可以插入的位置
|
||||
private int right; // 右指针 => 指向 Append* 操作下一个可以插入的位置
|
||||
private boolean isInitial; // 是否初始化
|
||||
private boolean isEstimator;// 是否仅测量编码后的长度,而不实际进行编码
|
||||
private SizeT totalReserve; // 编码缓存数组的大小
|
||||
|
||||
/**
|
||||
* 编码测量初始化
|
||||
* @Description:
|
||||
* 1. 一旦调用这个函数进行初始化,则调用所有的编码函数只会测量编码后的长度,不会执行实际的编码
|
||||
*/
|
||||
public void estimatorReset(){
|
||||
this.left=0;
|
||||
this.right=1;
|
||||
this.isInitial=true;
|
||||
this.isEstimator=true;
|
||||
this.totalReserve=new SizeT(Long.MAX_VALUE); //todo: 这里可能越界。
|
||||
}
|
||||
|
||||
/**
|
||||
* 编码初始化
|
||||
* @Description:
|
||||
* @param totalReserve 总的存储编码结果的数组长度
|
||||
* @param reserveFromBack 默认情况下,左右指针都指向数组的最右侧,表示totalReserve指定的所有空间都是预留给 Prepend* 操作的
|
||||
* reserveFromBack 可以指定需要预留多少空间给 Append*操作
|
||||
*/
|
||||
public boolean estimatorReset(SizeT totalReserve,SizeT reserveFromBack){
|
||||
if(reserveFromBack.compareTo(totalReserve)>0){
|
||||
return false;
|
||||
}
|
||||
this.isInitial=true;
|
||||
this.isEstimator=false;
|
||||
this.totalReserve=totalReserve;
|
||||
this.buffer=new byte[totalReserve.getVlIntValue2Int()]; //todo: 特殊情况下,可能越界。
|
||||
this.left=totalReserve.subtract(reserveFromBack)-1;//todo: 特殊情况下,可能越界。
|
||||
this.right=totalReserve.subtract(reserveFromBack);//todo: 特殊情况下,可能越界。
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为空
|
||||
* @return
|
||||
*/
|
||||
public boolean empty(){
|
||||
return (this.left+1)>=this.right;
|
||||
}
|
||||
|
||||
/**
|
||||
* 编码后的大小
|
||||
* @return
|
||||
*/
|
||||
public SizeT size(){
|
||||
return new SizeT(this.right-this.left-1);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 在每个操作之前进行有效性检查
|
||||
* @Description:
|
||||
* 1. 首先检查 Encoder 实例有没有初始化,没有初始化则返回错误;
|
||||
* 2. 然后检查预计新增 size 个字节,会不会超出最大包限制,超出则返回错误;
|
||||
* 3. 否则返回 nil
|
||||
* @param size
|
||||
* @return
|
||||
*/
|
||||
public boolean check(SizeT size){
|
||||
if(!this.isInitial){
|
||||
return false;
|
||||
}
|
||||
if(this.totalReserve.compareTo(this.size().add(size))<0){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepend a byte
|
||||
* @param value
|
||||
* @return 返回成功插入的字节数
|
||||
*/
|
||||
public int prependByte(byte value){
|
||||
if(this.isEstimator){
|
||||
this.left--;
|
||||
return 1;
|
||||
}
|
||||
if(check(new SizeT(1))){
|
||||
this.buffer[this.left]=value;
|
||||
this.left--;
|
||||
return 1;
|
||||
}else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param value
|
||||
* @return 返回插入成功的字节数
|
||||
*/
|
||||
public int appendByte(byte value){
|
||||
if(this.isEstimator){
|
||||
this.right++;
|
||||
return 1;
|
||||
}
|
||||
if(check(new SizeT(1))){
|
||||
this.buffer[this.right]=value;
|
||||
this.right++;
|
||||
return 1;
|
||||
}else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array
|
||||
* @param size
|
||||
* @return 返回插入成功的字节数
|
||||
*/
|
||||
public int prependByteArray(byte[] array,SizeT size){
|
||||
if(check(size)&&(size.compareTo(array.length)<=0)){
|
||||
if(this.isEstimator){
|
||||
this.left-=array.length;
|
||||
return size.getVlIntValue2Int();
|
||||
}else{
|
||||
System.arraycopy(array,0,this.buffer,
|
||||
this.left-size.getVlIntValue2Int()+1,size.getVlIntValue2Int());
|
||||
this.left-=array.length;
|
||||
return size.getVlIntValue2Int();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array
|
||||
* @param size
|
||||
* @return 返回插入成功的字节数
|
||||
*/
|
||||
public int appendByteArray(byte[] array,SizeT size){
|
||||
if(check(size)&&(size.compareTo(array.length)<=0)){
|
||||
if(this.isEstimator){
|
||||
this.right+=array.length;
|
||||
return size.getVlIntValue2Int();
|
||||
}else{
|
||||
System.arraycopy(array,0,this.buffer,
|
||||
this.right,size.getVlIntValue2Int());
|
||||
this.right+=array.length;
|
||||
return size.getVlIntValue2Int();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user