Skip to content
Permalink
Browse files
MIME4J-273 Fix encoded word splitting point for EncodeQ method as well.
  • Loading branch information
bwndai authored and chibenwa committed Jul 10, 2019
1 parent fefae5a commit 6ec1135e813fc845875cfae3c1055ad534a418b9
Showing 3 changed files with 20 additions and 5 deletions.
@@ -566,12 +566,13 @@ private static String encodeQ(String prefix, String text, Usage usage,
if (totalLength <= ENCODED_WORD_MAX_LENGTH - usedCharacters) {
return prefix + encodeQ(bytes, usage) + 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 = encodeQ(prefix, part1, usage, usedCharacters,
charset, bytes1);

String part2 = text.substring(text.length() / 2);
String part2 = text.substring(splitOffset);
byte[] bytes2 = encode(part2, charset);
String word2 = encodeQ(prefix, part2, usage, 0, charset, bytes2);

@@ -142,7 +142,7 @@ public void testEncodeEncodedWordSplit() throws Exception {
}

@Test
public void testEncodeEncodedWordSplitForUnicode() throws Exception {
public void testEncodeBEncodedWordSplitForUnicode() throws Exception {
StringBuilder sb = new StringBuilder("z");
for (int i = 0; i < 10; i++) {
// Append unicode character 𝕫 10 times.
@@ -155,6 +155,20 @@ public void testEncodeEncodedWordSplitForUnicode() throws Exception {
Usage.TEXT_TOKEN, 10, null, Encoding.B));
}

@Test
public void testEncodeQEncodedWordSplitForUnicode() throws Exception {
StringBuilder sb = new StringBuilder("z");
for (int i = 0; i < 6; i++) {
// Append unicode character 𝕫 6 times.
sb.append("\uD835\uDD6b");
}

String expected = "=?UTF-8?Q?z=F0=9D=95=AB=F0=9D=95=AB?= " +
"=?UTF-8?Q?=F0=9D=95=AB=F0=9D=95=AB=F0=9D=95=AB=F0=9D=95=AB?=";
Assert.assertEquals(expected, EncoderUtil.encodeEncodedWord(sb.toString(),
Usage.TEXT_TOKEN, 10, null, Encoding.Q));
}

@Test
public void testEncodeEncodedWord() throws Exception {
Assert.assertEquals("=?US-ASCII?Q??=", EncoderUtil.encodeEncodedWord("",
@@ -251,8 +251,8 @@ public void testSubject() throws Exception {
seventyEight)));

String seventyNine = seventyEight + "9";
String expected = "Subject: =?US-ASCII?Q?1234567890123456789012345678901234?="
+ "\r\n =?US-ASCII?Q?56789012345678901234567890123456789?=";
String expected = "Subject: =?US-ASCII?Q?123456789012345678901234567890123?="
+ "\r\n =?US-ASCII?Q?456789012345678901234567890123456789?=";
Assert.assertEquals(expected, decode(Fields.subject(seventyNine)));
}

0 comments on commit 6ec1135

Please sign in to comment.