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 c16049e0b879429a82b3c55481dcad8e19400eab Author: Benoit TELLIER <[email protected]> AuthorDate: Fri Nov 13 14:02:27 2026 +0100 [FIX] QuotedPrintableInputStream: decoded content is served from an offset, not shifted on every read Blanks are held back so that trailing blanks before a line break can be dropped. When a run of them ended with a visible byte, what did not fit in the caller's buffer went to decodedBuf, and each later read served at most len bytes from its front and then shifted all the rest with remove(0, chunk). Draining N bytes with reads of L bytes moved about N^2 / 2L bytes: with the 4 KB reads of ContentUtil#copy, some 12 GB of memmove for a part of 10 MB of spaces followed by one letter, on each decode of the message. decodedBuf is now served from a read offset, cleared once drained, so the drain is linear. It is only appended to once the caller's buffer is full, when it has already been drained: the decoded output does not change. --- .../mime4j/codec/QuotedPrintableInputStream.java | 30 ++++++++++-- .../codec/QuotedPrintableInputStreamTest.java | 57 ++++++++++++++++++++++ 2 files changed, 83 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/apache/james/mime4j/codec/QuotedPrintableInputStream.java b/core/src/main/java/org/apache/james/mime4j/codec/QuotedPrintableInputStream.java index f90e0af7..11979c39 100644 --- a/core/src/main/java/org/apache/james/mime4j/codec/QuotedPrintableInputStream.java +++ b/core/src/main/java/org/apache/james/mime4j/codec/QuotedPrintableInputStream.java @@ -55,6 +55,8 @@ public class QuotedPrintableInputStream extends InputStream { private final InputStream in; private final RecycledByteArrayBuffer decodedBuf; + // next byte of decodedBuf to serve: draining from the front does not shift the buffer + private int decodedPos = 0; private final RecycledByteArrayBuffer blanks; private final byte[] encoded; @@ -158,6 +160,7 @@ public class QuotedPrintableInputStream extends InputStream { index += chunk; int remaining = blanks.length() - chunk; if (remaining > 0) { + compactDecoded(); decodedBuf.append(blanks.buffer(), chunk, remaining); } blanks.clear(); @@ -171,23 +174,42 @@ public class QuotedPrintableInputStream extends InputStream { if (index < to) { buffer[index++] = (byte) b; } else { + compactDecoded(); decodedBuf.append(b); } } return index; } + /** + * Drops the bytes of decodedBuf already served. decodedBuf is only appended to once the + * caller's buffer is full, after read0 drained it, so this moves nothing in practice. + */ + private void compactDecoded() { + if (decodedPos > 0) { + decodedBuf.remove(0, decodedPos); + decodedPos = 0; + } + } + private int read0(final byte[] buffer, final int off, final int len) throws IOException { boolean eof = false; int to = off + len; int index = off; // check if a previous invocation left decoded content - if (decodedBuf.length() > 0) { - int chunk = Math.min(decodedBuf.length(), to - index); - System.arraycopy(decodedBuf.buffer(), 0, buffer, index, chunk); - decodedBuf.remove(0, chunk); + // Served from decodedPos: removing the served bytes from the front on every read moved + // what is left each time, quadratic in a long run of blanks + int available = decodedBuf.length() - decodedPos; + if (available > 0) { + int chunk = Math.min(available, to - index); + System.arraycopy(decodedBuf.buffer(), decodedPos, buffer, index, chunk); + decodedPos += chunk; index += chunk; + if (decodedPos == decodedBuf.length()) { + decodedBuf.clear(); + decodedPos = 0; + } } while (index < to) { diff --git a/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableInputStreamTest.java b/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableInputStreamTest.java index 0539396d..14cd37d4 100644 --- a/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableInputStreamTest.java +++ b/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableInputStreamTest.java @@ -19,6 +19,8 @@ package org.apache.james.mime4j.codec; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; @@ -26,6 +28,8 @@ import java.io.InputStreamReader; import java.io.Reader; import java.io.UnsupportedEncodingException; +import java.util.Arrays; + import org.apache.commons.io.IOUtils; import org.apache.james.mime4j.Charsets; import org.apache.james.mime4j.io.InputStreams; @@ -284,4 +288,57 @@ public class QuotedPrintableInputStreamTest { // "=2" is left as is (MIME4J-160), the CRLF stays one line break Assert.assertEquals("a=2\r\nb\r\n", readText(in)); } + + private static byte[] blanksThenX(int blanks) { + byte[] encoded = new byte[blanks + 1]; + Arrays.fill(encoded, 0, blanks, (byte) ' '); + encoded[blanks] = 'x'; + return encoded; + } + + private static byte[] decodeReadingBy(byte[] encoded, int readSize) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(encoded.length); + try (QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(new ByteArrayInputStream(encoded), false)) { + byte[] buffer = new byte[readSize]; + for (int n = decoder.read(buffer); n != -1; n = decoder.read(buffer)) { + out.write(buffer, 0, n); + } + } + return out.toByteArray(); + } + + @Test(timeout = 5000) + public void longRunOfBlanksShouldDecodeInLinearTime() throws Exception { + // Draining the blanks by removing the served bytes from the front of the buffer moved + // about N^2 / 2L bytes: some 2 TB here, about a minute + byte[] encoded = blanksThenX(16 * 1024 * 1024); + Assert.assertArrayEquals(encoded, decodeReadingBy(encoded, 64)); + } + + @Test + public void longRunOfBlanksShouldDecodeUnchangedWhateverTheReadSize() throws Exception { + byte[] encoded = blanksThenX(100000); + Assert.assertArrayEquals(encoded, decodeReadingBy(encoded, 4096)); + Assert.assertArrayEquals(encoded, decodeReadingBy(encoded, 1)); + try (QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(new ByteArrayInputStream(encoded), false)) { + Assert.assertArrayEquals(encoded, IOUtils.toByteArray(decoder)); + } + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + try (QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(new ByteArrayInputStream(encoded), false)) { + for (int b = decoder.read(); b != -1; b = decoder.read()) { + bytes.write(b); + } + } + Assert.assertArrayEquals(encoded, bytes.toByteArray()); + } + + @Test + public void blanksSpillingOverSeveralReadsShouldKeepTheTextAfterThem() throws Exception { + String encoded = "a" + new String(blanksThenX(10000), "US-ASCII") + "=3D \r\n" + " b \r\nc"; + String expected = "a" + new String(blanksThenX(10000), "US-ASCII") + "=\r\n b\r\nc"; + for (int readSize : new int[] {1, 3, 100, 4096, 20000}) { + Assert.assertEquals("read by " + readSize, expected, + new String(decodeReadingBy(encoded.getBytes("US-ASCII"), readSize), "US-ASCII")); + } + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
