normalcoding.java

来自「JAVA邮件系统」· Java 代码 · 共 95 行

JAVA
95
字号

public class NormalCoding implements Encodeing 
{
	private boolean CheckCodeBeforeDotOrLineEnd(byte[] sSrc,int index){
		for(int i=index;i>=0;i--){
			if( sSrc[i]==(byte)'.' )
				continue;
			if( sSrc[i]==(byte)0xa )
				return true;
			return false;
		}
		return true;
	}
	private boolean CheckDotCodeLineEnd(byte[] sSrc,int i ){
		if( i > sSrc.length - 3 )
			return false;
		if( sSrc[i]==(byte)'.'
			&&sSrc[i+1]==(byte)0xd
		    &&sSrc[i+2]==(byte)0xa
		  )
			return true;
		return false;
	}
	
	//sometime , before encode , we need calc the number of 
	//hight ascii code in buffer;
	public  int  CalcEncodeLengthForCharBuffer( byte[] sSrc ){
		if( sSrc==null  ){
			return 0;
		}
		int nDes=0;
		for(int i=0;i<sSrc.length ;i++ ){
			if( CheckDotCodeLineEnd(sSrc,i)
				&&CheckCodeBeforeDotOrLineEnd(sSrc,i) ){
				nDes++;
			}
			nDes++;
		}
		return nDes;
	};
	public  int  CalcEncodeLength( byte[] sSrc , int nLength ){
		if( nLength == 0 || sSrc==null  ){
			return 0;
		}
		int nDes=0;
		for(int i=0;i<nLength;i++ ){
			if( CheckDotCodeLineEnd(sSrc,i)
				&&CheckCodeBeforeDotOrLineEnd(sSrc,i) ){
				nDes++;
			}
			nDes++;
		}
		return nDes;
	};
	
	//the buffer should be alloc and free by external module;
	//Now ,it always return true;
	public  int Encode( byte[] sSrc,byte[] sDes,int nSrcLength, 
						int nOffsetSrc ,int nOffsetDes){
		if( nSrcLength == 0 || sSrc==null || sDes==null ){
			return 0;
		}
		int nDes=0;
		for(int i=0;i<nSrcLength;i++ ){
			sDes[nOffsetDes+nDes]=sSrc[nOffsetSrc+i];
			if( CheckDotCodeLineEnd(sSrc,nOffsetSrc+i)
				&&CheckCodeBeforeDotOrLineEnd(sSrc,nOffsetSrc+i) ){
				sDes[nOffsetDes+nDes]=(byte)'.';
				nDes++;
			}
			nDes++;
		}
		return nDes;
	};

	//the buffer should be alloc and free by external module;
	//Now ,it always return true;
	public  int Decode( byte[] sSrc,byte[] sDes,int nSrcLength 
						,int nOffsetSrc ,int nOffsetDes ){
		if( nSrcLength == 0 || sSrc==null || sDes==null ){
			return 0;
		}
		int nDes=0;
		for(int i=0;i<nSrcLength;i++ ){
			if( CheckDotCodeLineEnd(sSrc,nOffsetSrc+i)
				&&CheckCodeBeforeDotOrLineEnd(sSrc,nOffsetSrc+i) ){
				continue;
			}
			sDes[nOffsetDes+nDes]=sSrc[nOffsetSrc+i];
			nDes++;
		}
		return nDes;
	};
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?