📄 base_64coding.java
字号:
import java.lang.Integer;
import java.lang.Math;
public class Base_64Coding implements Encodeing
{
private static String BaseCode =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static byte nEncodeTemp , cTempHigh ,cTempLow;
static byte[] DecodeTable;
//initial.
public Base_64Coding(){
DecodeTable = new byte[256];
for(int i=0;i<256;i++ )
DecodeTable[i]=-2;
//make decode table;
for( int i=0;i<64;i++ )
{
DecodeTable[ BaseCode.charAt (i) ] = (byte)i;
DecodeTable[ BaseCode.charAt (i) | 0x80 ] = (byte)i;
}
DecodeTable[ '=' ] = -1;
DecodeTable[ '=' | 0x80 ] = -1;
}
//sometime , before encode , we need calc the number of
//hight ascii code in buffer;
public int CalcEncodeLengthForCharBuffer( byte[] sSrc ){
if( sSrc == null )
return 1;
int nLength = sSrc.length;
return CalcEncodeLength( sSrc , nLength );
};
public int CalcEncodeLength( byte[] sSrc , int nLength ){
return ( nLength/3 + 1 )*4 + 1 + ( nLength/lineCode + 1 )*2 ;
};
//the buffer should be alloc and free by external module;
//Now ,it always return true;
private int EncodeOneline( byte[] sSrc,byte[] sDes,int nSrcLength
,int nOffsetSrc ,int nOffsetDes ){
if( nSrcLength == 0 || sSrc==null || sDes==null ){
return 0;
}
int nDes = 0;
char tempc1,tempc2,tempc3;
// 3 byte => 4 byte ;
for( int i=0 ; i < nSrcLength ; i = i+3 ){
//first code;
tempc1 = (char)( ( (char)sSrc[nOffsetSrc+i] ) & 0x00ff );
sDes[nOffsetDes+nDes] = (byte)(BaseCode.charAt ( Math.abs(tempc1)>>2 ) );
//second code;
nEncodeTemp = (byte)(tempc1<<4);
cTempHigh= (byte)(nEncodeTemp&0x30);
if( ( i+1 >= nSrcLength ) ){
sDes[nOffsetDes+nDes+1] = (byte)(BaseCode.charAt ( Math.abs( cTempHigh) ) );
sDes[nOffsetDes+nDes+2] = (byte)'=';
sDes[nOffsetDes+nDes+3] = (byte)'=';
return nDes+4;
}
else{
tempc2 = (char)( ( (char)sSrc[nOffsetSrc+i+1] ) & 0x00ff );
cTempLow = (byte)(tempc2>>4);
sDes[nOffsetDes+nDes+1] = (byte)( BaseCode.charAt ( Math.abs( (byte)cTempHigh | cTempLow ) ));
}
//third code;
nEncodeTemp = (byte)(tempc2 <<2);
cTempHigh= (byte)(nEncodeTemp&0x3c);
if( ( i+2 >= nSrcLength ) ){
sDes[nOffsetDes+nDes+2] = (byte)(BaseCode.charAt ( Math.abs(cTempHigh) ));
sDes[nOffsetDes+nDes+3] = (byte)('=');
sDes[nOffsetDes+nDes+4] = 0;
return nDes+4;
}
else{
tempc3 = (char)( ( (char)sSrc[nOffsetSrc+i+2] ) & 0x00ff );
cTempLow = (byte)(tempc3>>6);
sDes[nOffsetDes+nDes+2] = (byte)(BaseCode.charAt ( Math.abs( (byte)cTempHigh | cTempLow ) ) );
}
//fourth code;
sDes[nOffsetDes+nDes+3] = (byte)(BaseCode.charAt ( Math.abs( tempc3&0x3f ) ) );
nDes +=4;
}
return nDes;
};
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;
int nLine =nSrcLength/Encodeing.lineCode;
int nTail = nSrcLength%Encodeing.lineCode;
if( nTail!=0 )
nLine++;
int nSrcNow = 0;
for( int i=0;i<nLine;i++ ){
if( i == nLine -1 )
nDes += EncodeOneline ( sSrc,sDes,
nTail,nSrcNow ,nDes);
else
nDes += EncodeOneline ( sSrc,sDes,
Encodeing.lineCode
,nSrcNow ,nDes );
sDes[nDes] = 0xd;
sDes[nDes+1] = 0xa;
nDes += 2;
nSrcNow += Encodeing.lineCode;
}
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;
}
//convert the decode src code;
for( int i=0;i<nSrcLength;i++ ){
sSrc[nOffsetSrc+i] = DecodeTable[ sSrc[nOffsetSrc+i] & 0x7F ];
}
// 4 byte => 3 byte ;
int nDes=0;
byte cSrc0,cSrc1,cSrc2,cSrc3;
for( int i=0 ; i < nSrcLength ; ){
//first get the code ;
while( sSrc[nOffsetSrc+i] <= -1 ){
if( i >= nSrcLength-1 ){
return nDes;
}
i++;
}
if( i >= nSrcLength ){
return nDes;
}
cSrc0 = sSrc[nOffsetSrc+i];
i++;
if( i>= nSrcLength || sSrc[nOffsetSrc+i] <= -1 ){
cSrc1 = -1;
}
else{
cSrc1 = sSrc[nOffsetSrc+i];
i++;
}
if( i>= nSrcLength || sSrc[nOffsetSrc+i] <= -1 ){
cSrc2 = -1;
}
else{
cSrc2 = sSrc[nOffsetSrc+i];
i++;
}
if( i>= nSrcLength || sSrc[nOffsetSrc+i] <= -1 ){
cSrc3 = -1;
}
else{
cSrc3 = sSrc[nOffsetSrc+i];
i++;
}
//first code;
cTempHigh = (byte)(cSrc0<<2);
if( cSrc1 < 0 ){
sDes[nOffsetDes+nDes] = cTempHigh;
nDes++;
continue;
}
else{
cTempLow = (byte)(cSrc1>>4);
sDes[nOffsetDes+nDes] = (byte)(cTempHigh|cTempLow);
nDes++;
}
//second code;
cTempHigh = (byte)(cSrc1<<4);
if( cSrc2 < 0 ){
sDes[nOffsetDes+nDes] = cTempHigh ;
nDes++;
continue;
}
else{
cTempLow = (byte)(cSrc2>>2);
sDes[nOffsetDes+nDes] = (byte)(cTempHigh | cTempLow);
nDes++;
}
//third code;
cTempHigh= (byte)(cSrc2<<6);
if( cSrc3 < 0 ){
continue;
}
else{
sDes[nOffsetDes+nDes] = (byte)(cTempHigh | cSrc3);
nDes++;
}
}
return nDes;
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -