@Test public void testDecodeEncodedWords() { String body, expect; MimeMessage message; body = "abc"; expect = "abc"; message = null; assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); body = "=?us-ascii?q?abc?="; expect = "abc"; message = null; assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); body = "=?"; expect = "=?"; message = null; assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); body = "=??"; expect = "=??"; message = null; assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); body = "=???"; expect = "=???"; message = null; assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); body = "=????"; expect = "=????"; message = null; assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); body = "=????="; expect = "=????="; message = null; assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); body = "=??q??="; expect = "=??q??="; ; message = null; assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); body = "=??q?a?="; expect = "a"; message = null; assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); body = "=??="; expect = "=??="; message = null; assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); body = "=?x?="; expect = "=?x?="; message = null; assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); body = "=?x??="; expect = "=?x??="; message = null; assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); body = "=?x?q?="; expect = "=?x?q?="; message = null; assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); body = "=?x?q??="; expect = "=?x?q??="; message = null; assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); body = "=?x?q?X?="; expect = "X"; message = null; assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); body = "=?us-ascii?b?abc?="; expect = ""; message = null; assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); body = "=?us-ascii?q?abc?= =?"; expect = "abc =?"; message = null; assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); body = "=?x?= =?"; expect = "=?x?= =?"; message = null; assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); body = "=?utf-8?B?5Liq5Lq66YKu566xOkJVRyAjMzAyNDY6OumCruS7tuato+aWh+mZhOS7tuWQ?=\n" + "=?utf-8?B?jeensOecgeeVpeaYvuekuuS8mOWMlg==?="; expect = "个人邮箱:BUG #30246::邮件正文附件��称省略显示优化"; message = null; assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); body = "=?gb2312?B?Obv9t9az6cnu29rHsLqju6rHyLPHSlfN8rrAvsa16qOsuPzT0DIwvNIzOTnU?= " + "=?gb2312?B?qr6r0aG439DHytTLr77Gteq1yMTjwLSjoaOoQUSjqQ?="; expect = "9积分抽深圳前海华侨城JW万豪酒店，更有20家399��精选高星试睡酒店等你来！（AD�"; message = null; assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); }
public static String decodeEncodedWords(String body, Message message) { if (!body.contains("=?")) { return body; } int previousEnd = 0; boolean previousWasEncoded = false; StringBuilder sb = new StringBuilder(); while (true) { int begin = body.indexOf("=?", previousEnd); if (begin == -1) { sb.append(body.substring(previousEnd)); return sb.toString(); } int qm1 = body.indexOf('?', begin + 2); if (qm1 == -1) { sb.append(body.substring(previousEnd)); return sb.toString(); } int qm2 = body.indexOf('?', qm1 + 1); if (qm2 == -1) { sb.append(body.substring(previousEnd)); return sb.toString(); } int end = body.indexOf("?=", qm2 + 1); if (end == -1) { sb.append(body.substring(previousEnd)); return sb.toString(); } end += 2; String sep = body.substring(previousEnd, begin); String decoded = decodeEncodedWord(body, begin, end, message); if (decoded == null) { sb.append(sep); sb.append(body.substring(begin, end)); } else { if (!previousWasEncoded || !CharsetUtil.isWhitespace(sep)) { sb.append(sep); } sb.append(decoded); } previousEnd = end; previousWasEncoded = decoded != null; } }
DecoderUtil { public static String decodeEncodedWords(String body, Message message) { if (!body.contains("=?")) { return body; } int previousEnd = 0; boolean previousWasEncoded = false; StringBuilder sb = new StringBuilder(); while (true) { int begin = body.indexOf("=?", previousEnd); if (begin == -1) { sb.append(body.substring(previousEnd)); return sb.toString(); } int qm1 = body.indexOf('?', begin + 2); if (qm1 == -1) { sb.append(body.substring(previousEnd)); return sb.toString(); } int qm2 = body.indexOf('?', qm1 + 1); if (qm2 == -1) { sb.append(body.substring(previousEnd)); return sb.toString(); } int end = body.indexOf("?=", qm2 + 1); if (end == -1) { sb.append(body.substring(previousEnd)); return sb.toString(); } end += 2; String sep = body.substring(previousEnd, begin); String decoded = decodeEncodedWord(body, begin, end, message); if (decoded == null) { sb.append(sep); sb.append(body.substring(begin, end)); } else { if (!previousWasEncoded || !CharsetUtil.isWhitespace(sep)) { sb.append(sep); } sb.append(decoded); } previousEnd = end; previousWasEncoded = decoded != null; } } }
DecoderUtil { public static String decodeEncodedWords(String body, Message message) { if (!body.contains("=?")) { return body; } int previousEnd = 0; boolean previousWasEncoded = false; StringBuilder sb = new StringBuilder(); while (true) { int begin = body.indexOf("=?", previousEnd); if (begin == -1) { sb.append(body.substring(previousEnd)); return sb.toString(); } int qm1 = body.indexOf('?', begin + 2); if (qm1 == -1) { sb.append(body.substring(previousEnd)); return sb.toString(); } int qm2 = body.indexOf('?', qm1 + 1); if (qm2 == -1) { sb.append(body.substring(previousEnd)); return sb.toString(); } int end = body.indexOf("?=", qm2 + 1); if (end == -1) { sb.append(body.substring(previousEnd)); return sb.toString(); } end += 2; String sep = body.substring(previousEnd, begin); String decoded = decodeEncodedWord(body, begin, end, message); if (decoded == null) { sb.append(sep); sb.append(body.substring(begin, end)); } else { if (!previousWasEncoded || !CharsetUtil.isWhitespace(sep)) { sb.append(sep); } sb.append(decoded); } previousEnd = end; previousWasEncoded = decoded != null; } }  }
DecoderUtil { public static String decodeEncodedWords(String body, Message message) { if (!body.contains("=?")) { return body; } int previousEnd = 0; boolean previousWasEncoded = false; StringBuilder sb = new StringBuilder(); while (true) { int begin = body.indexOf("=?", previousEnd); if (begin == -1) { sb.append(body.substring(previousEnd)); return sb.toString(); } int qm1 = body.indexOf('?', begin + 2); if (qm1 == -1) { sb.append(body.substring(previousEnd)); return sb.toString(); } int qm2 = body.indexOf('?', qm1 + 1); if (qm2 == -1) { sb.append(body.substring(previousEnd)); return sb.toString(); } int end = body.indexOf("?=", qm2 + 1); if (end == -1) { sb.append(body.substring(previousEnd)); return sb.toString(); } end += 2; String sep = body.substring(previousEnd, begin); String decoded = decodeEncodedWord(body, begin, end, message); if (decoded == null) { sb.append(sep); sb.append(body.substring(begin, end)); } else { if (!previousWasEncoded || !CharsetUtil.isWhitespace(sep)) { sb.append(sep); } sb.append(decoded); } previousEnd = end; previousWasEncoded = decoded != null; } }  static String decodeEncodedWords(String body, Message message); }
DecoderUtil { public static String decodeEncodedWords(String body, Message message) { if (!body.contains("=?")) { return body; } int previousEnd = 0; boolean previousWasEncoded = false; StringBuilder sb = new StringBuilder(); while (true) { int begin = body.indexOf("=?", previousEnd); if (begin == -1) { sb.append(body.substring(previousEnd)); return sb.toString(); } int qm1 = body.indexOf('?', begin + 2); if (qm1 == -1) { sb.append(body.substring(previousEnd)); return sb.toString(); } int qm2 = body.indexOf('?', qm1 + 1); if (qm2 == -1) { sb.append(body.substring(previousEnd)); return sb.toString(); } int end = body.indexOf("?=", qm2 + 1); if (end == -1) { sb.append(body.substring(previousEnd)); return sb.toString(); } end += 2; String sep = body.substring(previousEnd, begin); String decoded = decodeEncodedWord(body, begin, end, message); if (decoded == null) { sb.append(sep); sb.append(body.substring(begin, end)); } else { if (!previousWasEncoded || !CharsetUtil.isWhitespace(sep)) { sb.append(sep); } sb.append(decoded); } previousEnd = end; previousWasEncoded = decoded != null; } }  static String decodeEncodedWords(String body, Message message);  }