IMAP文件夹编解码兑现
IMAP文件夹编解码实现
参考链接: http://hi.baidu.com/hswt/blog/item/e577b0fb4c45ed334f4aea95.html
据原作者讲,是从Perl算法转成的C#算法,我现在没有办法,只能使用Java, 所以又转成Java的实现了,都比较类似,但是由于本人对C#的不熟悉,还是费一些时间在网上找关于C#的正则表达式对象的使用方法.
现在把真正的Java版本放出来, 如果对概念有什么不理解的请直接参考原文章
参考链接: 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; } }