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; } /** * 在前向插入一个可变长度的数字 * @param varNumber * @return */ public int prependVarNumber(VlInt varNumber){ return this.prependByteArray(varNumber.getVlIntBytes(),new SizeT(varNumber.size())); } /** * 在后向插入一个可变长度的数字 * @param varNumber * @return */ public int appendVarNumber(VlInt varNumber){ return this.appendByteArray(varNumber.getVlIntBytes(),new SizeT(varNumber.size())); } //todo: 在前向&后向插入一个非负整数 /** * 根据TLV-TYPE和一个字节数组,在前向插入一个 TLV Block * @param tlvType * @param array * @param size * @return */ public int prependByteArrayBlock(VlInt tlvType,byte[] array,SizeT size){ int totalLength=this.prependByteArray(array,size); totalLength+=this.prependVarNumber(new VlInt(size)); totalLength+=this.prependVarNumber(tlvType); return totalLength; } /** * 根据TLV-TYPE和一个字节数组,在后向插入一个 TLV Block * @param tlvType * @param array * @param size * @return */ public int appendByteArrayBlock(VlInt tlvType,byte[] array,SizeT size){ int totalLength=this.appendByteArray(array,size); totalLength+=this.appendVarNumber(new VlInt(size.getVlIntValue2Long())); totalLength+=this.appendVarNumber(tlvType); return totalLength; } /** * 在前向插入一个 TLV Block * @param block * @return */ public int prependBlock(Block block){ return this.prependByteArrayBlock(block.getTlvType(),block.getValue(),new SizeT(block.getLength())); } /** * 在后向插入一个 TLV Block * @param block * @return */ public int appendBlock(Block block){ return this.appendByteArrayBlock(block.getTlvType(),block.getValue(),new SizeT(block.getLength())); } /** * 获取编码后的字节数组 * @return */ public byte[] getBuffer() { if((!this.isInitial)||this.isEstimator){ return null; } int len=this.right-this.left-1; byte[] res=new byte[len]; System.arraycopy(this.buffer,this.left+1,res,0,len); return res; } }