在序列化的过程中,我们将一个对象序列化为字符串的过程中会出现乱码的情形,这中情况,我们可以使用一种Base64编码的方式,将乱码的字符串转化为ascii编码的方式,那么这种编码是如何实现的呢?
我们采用的是利用将已有的编码进行长度扩充,Base64是将原来3个字节扩充为4个字节,我们将3个字节拆成4等份,每份都是6个bit,然后将这个bit值映射到一个数组,数组如下:
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
'P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d',
'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s',
't','u','v','w','x','y','z','0','1','2','3','4','5','6','7',
'8','9','+','/'
编码后的数组就是经过Base64编码的结果。
正如上面我们看到的,是对每3个字节进行编码,假如字符串的长度不是3的倍数,怎么办,没关系,我们根据字符串的长度和3的余数进行末尾填充'=’
,在编码的时候填充\x00
,解码的时候根据'='
号的数目来进行判断并解码,Base64编码的方式就是这样,下面是我针对Base64编码的一个实现,代码如下
public class Base64Realize {
//进行base64映射的字符数组
private final static char[] str = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'0','1','2','3','4','5','6','7','8','9','+','/'};
/**
* 获取解码器
* @return
*/
public static Base64Realize getDecoder(){
return new Base64Realize();
}
/**
* 解码
* @param code
* @return
*/
public String decode(String code){
//对字符串的长度进行计算
int length = code.length();
//判断长度的合法性
if(length == 0 || length % 4 != 0)
return null;
//获取字符串末尾的'='号数目
int endEqualNum = 0;
if(code.endsWith("=="))
endEqualNum = 2;
else if(code.endsWith("="))
endEqualNum = 1;
//对末尾的=号进行替换
code.replace('=','0');
StringBuilder sb = new StringBuilder(length);
//解码
int blockNum = length / 4;
String afterDecode = "";
for(int i = 0;i < blockNum;i++){
afterDecode = decodeDetail(code.substring(i * 4,i * 4 + 4));
sb.append(afterDecode);
}
//返回字符串
String result = sb.toString();
return result.substring(0,result.length() - endEqualNum);
}
/**
* 编码
* @param code
* @return
*/
public String encode(String code){
//初始化判断
if (code == null || code.equals(""))
return null;
//获取需编码字符串的长度
int length = code.length();
StringBuilder sb = new StringBuilder(length * 2);
//转化为char型数组
char[] code1 = code.toCharArray();
//获取长度对3的取余
int mod = length % 3;
//获取长度对3的倍数的
int div = length / 3;
//编码
for(int i = 0;i < div;i++){
int temp = i * 3;
sb.append(encodeDetail(code1[temp],code1[temp + 1],code1[temp + 2]));
}
//对超出的进行额外的编码
if (mod == 1) {
String str = encodeDetail(code1[length - 1], '\0', '\0');
sb.append(str.substring(0,str.length() - 2) + "==");
}
if(mod == 2) {
String str = encodeDetail(code1[length - 2], code1[length - 1], '\0');
sb.append(str.substring(0,str.length() - 1) + "=");
}
return sb.toString();
}
/**
* 编码的详细步骤
* @param a1
* @param a2
* @param a3
* @return
*/
private String encodeDetail(char a1,char a2,char a3){
char[] b = new char[4];
b[0] = str[((a1 & 0xFC) >> 2)];
b[1] = str[(a1 & 0x03) << 4 | (a2 & 0xF0) >> 4];
b[2] = str[(a2 & 0x0F) << 2 | (a3 & 0xC0) >> 6];
b[3] = str[(a3 & 0x3F)];
return String.copyValueOf(b);
}
/**
* 解码的详细步骤
* @param str
* @return
*/
private String decodeDetail(String str){
int len = str.length();
if(len != 4)
return null;
char[] b = new char[3];
int a1 = getIndex(str.charAt(0));
int a2 = getIndex(str.charAt(1));
int a3 = getIndex(str.charAt(2));
int a4 = getIndex(str.charAt(3));
b[0] = (char) (a1 << 2 | (a2 & 0x30) >> 4);
b[1] = (char) ((a2 & 0x0F) << 4 | (a3 & 0x3C) >> 2);
b[2] = (char) ((a3 & 0x03) << 6 | a4);
return String.copyValueOf(b);
}
/**
* 获取字节的映射位置
* @param c
* @return
*/
private int getIndex(char c){
for(int i = 0;i < str.length;i++){
if(str[i] == c)
return i;
}
return -1;
}
/**
* 获取编码器
* @return
*/
public static Base64Realize getEncoder(){
return new Base64Realize();
}
}