IMAP文件夹编解码实现

参考链接: http://hi.baidu.com/hswt/blog/item/e577b0fb4c45ed334f4aea95.html

据原作者讲,是从Perl算法转成的C#算法,我现在没有办法,只能使用Java, 所以又转成Java的实现了,都比较类似,但是由于本人对C#的不熟悉,还是费一些时间在网上找关于C#的正则表达式对象的使用方法.

现在把真正的Java版本放出来, 如果对概念有什么不理解的请直接参考原文章

public class ImapFolderEncoder {
public static String encode(String folder) {
String rtn = "", base64;
int index = 0;
Pattern regAsis = Pattern.compile("\\G(?:[\\x20-\\x25\\x27-\\x7e])+");
Pattern reg26 = Pattern.compile("\\G&");
Pattern regEncode = Pattern.compile("\\G(?:[^\\x20-\\x7e])+");
Pattern regEq = Pattern.compile("=+$");
Pattern regSlash = Pattern.compile("\\/");
while (index < folder.length()) {
Matcher m;
m = regAsis.matcher(folder);
if (m.find(index)) {
index = index + (m.end() - m.start());
rtn = rtn + m.group();
continue;
}
m = reg26.matcher(folder);
if (m.find(index)) {
index = index + (m.end() - m.start());
rtn = rtn + "&-";
continue;
}
m = regEncode.matcher(folder);
if (m.find(index)) {
index = index + (m.end() - m.start());
base64 = SimpleUtil.encodeBase64Content(m.group(), "UTF-16BE");
base64 = base64.replaceAll(regEq.pattern(), "");
base64 = base64.replaceAll(regSlash.pattern(), ",");
rtn = rtn + "&" + base64 + "-";
continue;
}
}
return rtn;
}

}




public class ImapFolderDecoder {
public static String decode(String folder) {
String rtn = "", base64;
int index = 0;
Pattern regAsis = Pattern.compile("\\G([^&]+)");
Pattern reg26 = Pattern.compile("\\G\\&-");
Pattern regDecode = Pattern.compile("\\G\\&([A-Za-z0-9+,]+)-?");
Pattern regComma = Pattern.compile(",");
while (index < folder.length()) {
Matcher m;
m = regAsis.matcher(folder);
if (m.find(index)) {
index = index + (m.end() - m.start());
rtn = rtn + m.group();
continue;
}
m = reg26.matcher(folder);
if (m.find(index)) {
index = index + (m.end() - m.start());
rtn = rtn + "&";
continue;
}
m = regDecode.matcher(folder);
if (m.find(index)) {
index = index + (m.end() - m.start());
base64 = m.group().substring(1, m.group().length() - 1);
base64 = base64.replaceAll(regComma.pattern(), "/");
int mod = base64.length() % 4;
int count = 4 - mod;
while (count > 0) {
base64 += "=";
count--;
}
base64 = SimpleUtil.base64Decode(base64, "UTF-16BE");
rtn = rtn + base64;
continue;
}
}
return rtn;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值