Repository: james-mime4j Updated Branches: refs/heads/master d924dcb51 -> 03c1f6fcb
MIME4J-273 Update encodeB encoding bytes splitting point. EncoderUtil.encodeIfNecessary(<unicode string>, EncoderUtil.Usage.TEXT_TOKEN, <number less than 10>) is broken in some cases, such as : https://issues.apache.org/jira/projects/MIME4J/issues/MIME4J-250?filter=allopenissues The culprit is line String part1 = text.substring(0, text.length() / 2); Since the breaking point can be in between one unicode character. Once the string is breaking into two part, part1 will have a broken tail and part2 will have a broken head in this case. Update test for failing testEncodeEncodedWordSplitForUnicode. Changed parameter is to force word split. Project: http://git-wip-us.apache.org/repos/asf/james-mime4j/repo Commit: http://git-wip-us.apache.org/repos/asf/james-mime4j/commit/03c1f6fc Tree: http://git-wip-us.apache.org/repos/asf/james-mime4j/tree/03c1f6fc Diff: http://git-wip-us.apache.org/repos/asf/james-mime4j/diff/03c1f6fc Branch: refs/heads/master Commit: 03c1f6fcb5e1fc0d78371d03af5310a5c6cb1f98 Parents: d924dcb Author: Bowen <[email protected]> Authored: Fri Apr 13 22:15:31 2018 -0700 Committer: benwa <[email protected]> Committed: Fri Apr 27 15:02:03 2018 +0700 ---------------------------------------------------------------------- .../org/apache/james/mime4j/codec/EncoderUtil.java | 5 +++-- .../apache/james/mime4j/codec/EncoderUtilTest.java | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-mime4j/blob/03c1f6fc/core/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java b/core/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java index 18e81f1..c9b2753 100644 --- a/core/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java +++ b/core/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java @@ -539,12 +539,13 @@ public class EncoderUtil { if (totalLength <= ENCODED_WORD_MAX_LENGTH - usedCharacters) { return prefix + encodeB(bytes) + ENC_WORD_SUFFIX; } else { - String part1 = text.substring(0, text.length() / 2); + int splitOffset = text.offsetByCodePoints(text.length() / 2, -1); + String part1 = text.substring(0, splitOffset); byte[] bytes1 = encode(part1, charset); String word1 = encodeB(prefix, part1, usedCharacters, charset, bytes1); - String part2 = text.substring(text.length() / 2); + String part2 = text.substring(splitOffset); byte[] bytes2 = encode(part2, charset); String word2 = encodeB(prefix, part2, 0, charset, bytes2); http://git-wip-us.apache.org/repos/asf/james-mime4j/blob/03c1f6fc/core/src/test/java/org/apache/james/mime4j/codec/EncoderUtilTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/james/mime4j/codec/EncoderUtilTest.java b/core/src/test/java/org/apache/james/mime4j/codec/EncoderUtilTest.java index 93248fa..ca90a75 100644 --- a/core/src/test/java/org/apache/james/mime4j/codec/EncoderUtilTest.java +++ b/core/src/test/java/org/apache/james/mime4j/codec/EncoderUtilTest.java @@ -140,6 +140,20 @@ public class EncoderUtilTest { Usage.TEXT_TOKEN, 0, null, Encoding.Q); Assert.assertTrue(encodedSixtyOne.contains("?= =?US-ASCII?Q?")); } + + @Test + public void testEncodeEncodedWordSplitForUnicode() throws Exception { + StringBuilder sb = new StringBuilder("z"); + for (int i = 0; i < 10; i++) { + // Append unicode character ð« 10 times. + sb.append("\uD835\uDD6b"); + } + + String expected = "=?UTF-8?B?evCdlavwnZWr8J2Vq/Cdlas=?= " + + "=?UTF-8?B?8J2Vq/CdlavwnZWr8J2Vq/CdlavwnZWr?="; + Assert.assertEquals(expected, EncoderUtil.encodeEncodedWord(sb.toString(), + Usage.TEXT_TOKEN, 10, null, Encoding.B)); + } @Test public void testEncodeEncodedWord() throws Exception { --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
