This is an automated email from the ASF dual-hosted git repository. chibenwa pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-mime4j.git
commit 082726140b4dc8f7f8bf036d58244e16495b8a9a Author: Benoit TELLIER <[email protected]> AuthorDate: Wed Nov 18 22:35:06 2026 +0100 [PERF] MimeUtil#fold: look for a fold at the end of a whitespace run once per run --- .../java/org/apache/james/mime4j/util/MimeUtil.java | 17 ++++++++++++----- .../java/org/apache/james/mime4j/util/MimeUtilTest.java | 7 +++++++ 2 files changed, 19 insertions(+), 5 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 ab60afe5..d6abbb57 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 @@ -259,6 +259,9 @@ public final class MimeUtil { int wspIdx = indexOfWsp(s, 0); // The used characters (usually the field name and its colon) are text of the first line boolean lineHasText = wspIdx > 0 || usedCharacters > 0; + // end of the whitespace run holding wspIdx, computed once per run + int runEnd = -1; + boolean runEndsWithFold = false; while (true) { if (wspIdx == length) { sb.append(s.substring(Math.max(0, lastLineBreak))); @@ -266,6 +269,10 @@ public final class MimeUtil { } int nextWspIdx = indexOfWsp(s, wspIdx + 1); + if (wspIdx >= runEnd) { + runEnd = endOfWspRun(s, wspIdx); + runEndsWithFold = runEnd < length && s.charAt(runEnd) == '\r'; + } if (wspIdx > 0 && s.charAt(wspIdx - 1) == '\n') { // the value is already folded here: a new line starts with this whitespace, @@ -273,7 +280,7 @@ public final class MimeUtil { lineStart = wspIdx; lineHasText = false; } else if (nextWspIdx - lineStart > maxCharacters && lineHasText && wspIdx < lastNonWspIdx - && !endsWithFold(s, wspIdx)) { + && !runEndsWithFold) { sb.append(s, Math.max(0, lastLineBreak), wspIdx); sb.append("\r\n"); lastLineBreak = wspIdx; @@ -289,15 +296,15 @@ public final class MimeUtil { } /** - * Whether the whitespace run starting at <code>index</code> is followed by a fold already in the value: - * breaking there would write a whitespace only line. + * Index of the first character after the whitespace run starting at <code>index</code>. + * When it is a fold already in the value, breaking in the run would write a whitespace only line. */ - private static boolean endsWithFold(String s, int index) { + private static int endOfWspRun(String s, int index) { int i = index; while (i < s.length() && isWsp(s.charAt(i))) { i++; } - return i < s.length() && s.charAt(i) == '\r'; + return i; } private static boolean holdsText(String s, int from, int to) { 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 fb3df848..6bfd5b58 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 @@ -206,4 +206,11 @@ public class MimeUtilTest { } Assert.assertEquals(value, MimeUtil.unfold(folded)); } + + @Test(timeout = 10000) + public void foldShouldBeLinearInALongWhitespaceRunEndingAtAFold() { + String value = "x" + repeat(" ", 1_000_000) + "\r\n y"; + String folded = MimeUtil.fold(value, 0); + Assert.assertEquals(value, folded); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
