This is an automated email from the ASF dual-hosted git repository. rcordier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-mime4j.git
The following commit(s) were added to refs/heads/master by this push: new 66a09219 Update fold method in MimeUtil to allow inputing max characters 66a09219 is described below commit 66a09219457854c7a26e5b7c0e4c9dd59b4b0c32 Author: hung phan <hp...@linagora.com> AuthorDate: Tue Nov 5 15:07:25 2024 +0700 Update fold method in MimeUtil to allow inputing max characters --- .../org/apache/james/mime4j/util/MimeUtil.java | 38 ++++++++++++++++++---- .../org/apache/james/mime4j/util/MimeUtilTest.java | 23 ++++++++----- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/org/apache/james/mime4j/util/MimeUtil.java b/core/src/main/java/org/apache/james/mime4j/util/MimeUtil.java index 46e71e4d..9c4655fe 100644 --- a/core/src/main/java/org/apache/james/mime4j/util/MimeUtil.java +++ b/core/src/main/java/org/apache/james/mime4j/util/MimeUtil.java @@ -73,6 +73,10 @@ public final class MimeUtil { * The <code>7bit</code> encoding. */ public static final String ENC_7BIT = "7bit"; + /** + * The default maximum number of characters. + */ + public static final int DEFAULT_MAX_CHARACTERS = 76; // used to create unique ids private static final Random random = new Random(); @@ -203,10 +207,10 @@ public final class MimeUtil { /** * Splits the specified string into a multiple-line representation with - * lines no longer than 76 characters (because the line might contain + * lines no longer than the maximum number of characters (because the line might contain * encoded words; see <a href='http://www.faqs.org/rfcs/rfc2047.html'>RFC * 2047</a> section 2). If the string contains non-whitespace sequences - * longer than 76 characters a line break is inserted at the whitespace + * longer than the maximum number of characters a line break is inserted at the whitespace * character following the sequence resulting in a line longer than 76 * characters. * @@ -215,11 +219,11 @@ public final class MimeUtil { * @param usedCharacters * number of characters already used up. Usually the number of * characters for header field name plus colon and one space. + * @param maxCharacters + * maximum number of characters * @return a multiple-line representation of the given string. */ - public static String fold(String s, int usedCharacters) { - final int maxCharacters = 76; - + public static String fold(String s, int usedCharacters, int maxCharacters) { final int length = s.length(); if (usedCharacters + length <= maxCharacters) return s; @@ -246,6 +250,26 @@ public final class MimeUtil { } } + /** + * Splits the specified string into a multiple-line representation with + * lines no longer than 76 characters (because the line might contain + * encoded words; see <a href='http://www.faqs.org/rfcs/rfc2047.html'>RFC + * 2047</a> section 2). If the string contains non-whitespace sequences + * longer than 76 characters a line break is inserted at the whitespace + * character following the sequence resulting in a line longer than 76 + * characters. + * + * @param s + * string to split. + * @param usedCharacters + * number of characters already used up. Usually the number of + * characters for header field name plus colon and one space. + * @return a multiple-line representation of the given string. + */ + public static String fold(String s, int usedCharacters) { + return fold(s, usedCharacters, DEFAULT_MAX_CHARACTERS); + } + /** * Unfold a multiple-line representation into a single line. * @@ -266,7 +290,7 @@ public final class MimeUtil { } /** - Unfold and decode header value + Unfold and decode header value */ public static String unscrambleHeaderValue(String headerValue) { return DecoderUtil.decodeEncodedWords( @@ -364,4 +388,4 @@ public final class MimeUtil { result.put(7L, "Sun"); return result; } -} +} \ No newline at end of file diff --git a/core/src/test/java/org/apache/james/mime4j/util/MimeUtilTest.java b/core/src/test/java/org/apache/james/mime4j/util/MimeUtilTest.java index e24ae809..de69e745 100644 --- a/core/src/test/java/org/apache/james/mime4j/util/MimeUtilTest.java +++ b/core/src/test/java/org/apache/james/mime4j/util/MimeUtilTest.java @@ -25,23 +25,30 @@ import org.junit.Test; public class MimeUtilTest { @Test - public void testFold() throws Exception { + public void testFold() { + Assert.assertEquals("this\r\n is\r\n a\r\n test", MimeUtil.fold("this is a test", 0, 4)); + Assert.assertEquals("this\r\n is a\r\n test", MimeUtil.fold("this is a test", 0, 5)); + Assert.assertEquals("this\r\n is\r\n a\r\n test", MimeUtil.fold("this is a test", 1, 4)); + } + + @Test + public void testFoldWithDefaultMaxCharacters() { Assert.assertEquals("this is\r\n a test", MimeUtil.fold("this is a test", 68)); Assert.assertEquals("this is\r\n a test", MimeUtil.fold("this is a test", 69)); Assert.assertEquals("this\r\n is a test", MimeUtil.fold("this is a test", 70)); Assert.assertEquals("this \r\n is a test", MimeUtil.fold( - "this is a test", 70)); + "this is a test", 70)); } @Test public void testFoldOverlyLongNonWhitespace() throws Exception { String ninety = "1234567890123456789012345678901234567890" - + "12345678901234567890123456789012345678901234567890"; + + "12345678901234567890123456789012345678901234567890"; String input = String.format("testing 1 2 %s testing %s", ninety, - ninety); + ninety); String expected = String.format( - "testing 1 2\r\n %s\r\n testing\r\n %s", ninety, ninety); + "testing 1 2\r\n %s\r\n testing\r\n %s", ninety, ninety); Assert.assertEquals(expected, MimeUtil.fold(input, 0)); } @@ -65,10 +72,10 @@ public class MimeUtilTest { Assert.assertEquals("this is a test", MimeUtil.unfold("this is\r\n a test")); Assert.assertEquals("this is a test", MimeUtil.unfold("this\r\n is a test")); Assert.assertEquals("this is a test", MimeUtil - .unfold("this \r\n is a test")); + .unfold("this \r\n is a test")); Assert.assertEquals("this is a test", MimeUtil - .unfold("this\r\n is\r\n a\r\n test")); + .unfold("this\r\n is\r\n a\r\n test")); } -} +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org