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-jdkim.git

commit d9b9320d7993cf2be29449620a09333f22bbb576
Author: Benoit TELLIER <[email protected]>
AuthorDate: Thu Nov 19 06:03:39 2026 +0100

    [PERF] Message: convert line ends and capture the header a block at a time
    
    mime4j's EOLConvertingInputStream only implements read(): each 4 KB
    block mime4j asked for went through InputStream#read(byte[], int, int)
    and four nested read() calls per octet, body included. The header
    capture then called the synchronized ByteArrayOutputStream#write(int)
    once per header octet.
    
    CrlfConvertingInputStream does the same conversion (a lone CR or a
    lone LF becomes CRLF) into the caller's array, keeping the CR state
    and a pending LF across reads. HeaderCapturingInputStream looks for
    the end of the header in the block and appends it with one write.
    
    A test compares both against the former streams on random CR/LF mixes
    and block boundaries.
---
 .../jdkim/impl/CrlfConvertingInputStream.java      | 118 ++++++++++++++++++++
 .../jdkim/impl/HeaderCapturingInputStream.java     |  25 ++++-
 .../java/org/apache/james/jdkim/impl/Message.java  |   3 +-
 .../jdkim/impl/CrlfConvertingInputStreamTest.java  | 123 +++++++++++++++++++++
 4 files changed, 264 insertions(+), 5 deletions(-)

diff --git 
a/main/src/main/java/org/apache/james/jdkim/impl/CrlfConvertingInputStream.java 
b/main/src/main/java/org/apache/james/jdkim/impl/CrlfConvertingInputStream.java
new file mode 100644
index 0000000..a020fc5
--- /dev/null
+++ 
b/main/src/main/java/org/apache/james/jdkim/impl/CrlfConvertingInputStream.java
@@ -0,0 +1,118 @@
+/******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one                 *
+ * or more contributor license agreements.  See the NOTICE file               *
+ * distributed with this work for additional information                      *
+ * regarding copyright ownership.  The ASF licenses this file                 *
+ * to you under the Apache License, Version 2.0 (the                          *
+ * "License"); you may not use this file except in compliance                 *
+ * with the License.  You may obtain a copy of the License at                 *
+ *                                                                            *
+ *   http://www.apache.org/licenses/LICENSE-2.0                               *
+ *                                                                            *
+ * Unless required by applicable law or agreed to in writing,                 *
+ * software distributed under the License is distributed on an                *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY                     *
+ * KIND, either express or implied.  See the License for the                  *
+ * specific language governing permissions and limitations                    *
+ * under the License.                                                         *
+ 
******************************************************************************/
+
+package org.apache.james.jdkim.impl;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Converts a lone CR and a lone LF to CRLF, as mime4j's 
EOLConvertingInputStream with CONVERT_BOTH does, but
+ * a block at a time: that stream only implements read(), so every bulk read 
went through one virtual call
+ * chain per octet.
+ */
+class CrlfConvertingInputStream extends InputStream {
+
+    private final InputStream in;
+    private final byte[] buffer = new byte[8192];
+    private int position;
+    private int limit;
+    // the previous octet of the source was a CR, already written as CRLF: an 
LF right after it is dropped
+    private boolean afterCR;
+    // the LF of a CRLF that did not fit in the caller's array
+    private boolean pendingLF;
+
+    CrlfConvertingInputStream(InputStream in) {
+        this.in = in;
+    }
+
+    @Override
+    public int read() throws IOException {
+        byte[] one = new byte[1];
+        int read = read(one, 0, 1);
+        return read == -1 ? -1 : one[0] & 0xFF;
+    }
+
+    @Override
+    public int read(byte[] b, int off, int len) throws IOException {
+        if (off < 0 || len < 0 || len > b.length - off) {
+            throw new IndexOutOfBoundsException();
+        }
+        if (len == 0) {
+            return 0;
+        }
+        int o = off;
+        int end = off + len;
+        if (pendingLF) {
+            pendingLF = false;
+            b[o++] = '\n';
+        }
+        while (o < end) {
+            if (position == limit) {
+                // read the source again only if nothing is returned yet, so 
that a read never blocks needlessly
+                if (o > off || !fill()) {
+                    break;
+                }
+            }
+            byte c = buffer[position++];
+            if (c == '\r') {
+                afterCR = true;
+                b[o++] = '\r';
+                if (o < end) {
+                    b[o++] = '\n';
+                } else {
+                    pendingLF = true;
+                }
+            } else if (c == '\n') {
+                if (afterCR) {
+                    afterCR = false;
+                    continue;
+                }
+                b[o++] = '\r';
+                if (o < end) {
+                    b[o++] = '\n';
+                } else {
+                    pendingLF = true;
+                }
+            } else {
+                afterCR = false;
+                b[o++] = c;
+            }
+        }
+        return o == off ? -1 : o - off;
+    }
+
+    private boolean fill() throws IOException {
+        int read;
+        do {
+            read = in.read(buffer, 0, buffer.length);
+        } while (read == 0);
+        if (read == -1) {
+            return false;
+        }
+        position = 0;
+        limit = read;
+        return true;
+    }
+
+    @Override
+    public void close() throws IOException {
+        in.close();
+    }
+}
diff --git 
a/main/src/main/java/org/apache/james/jdkim/impl/HeaderCapturingInputStream.java
 
b/main/src/main/java/org/apache/james/jdkim/impl/HeaderCapturingInputStream.java
index efe61ea..aa002a8 100644
--- 
a/main/src/main/java/org/apache/james/jdkim/impl/HeaderCapturingInputStream.java
+++ 
b/main/src/main/java/org/apache/james/jdkim/impl/HeaderCapturingInputStream.java
@@ -33,6 +33,8 @@ import java.io.InputStream;
  */
 class HeaderCapturingInputStream extends FilterInputStream {
 
+    private static final byte[] TERMINATOR = {'\r', '\n', '\r', '\n'};
+
     private final ByteArrayOutputStream header = new ByteArrayOutputStream();
     // number of bytes of the "\r\n\r\n" terminator seen so far
     private int matched = 2;
@@ -54,8 +56,8 @@ class HeaderCapturingInputStream extends FilterInputStream {
     @Override
     public int read(byte[] b, int off, int len) throws IOException {
         int read = super.read(b, off, len);
-        for (int i = 0; i < read && !complete; i++) {
-            capture(b[off + i]);
+        if (read > 0 && !complete) {
+            capture(b, off, read);
         }
         return read;
     }
@@ -78,8 +80,25 @@ class HeaderCapturingInputStream extends FilterInputStream {
             return;
         }
         header.write(b);
+        match(b);
+    }
+
+    /**
+     * Captures b[off, off + len) up to the end of the header block, with a 
single write.
+     */
+    private void capture(byte[] b, int off, int len) {
+        int end = off + len;
+        int i = off;
+        while (i < end && !complete) {
+            match(b[i]);
+            i++;
+        }
+        header.write(b, off, i - off);
+    }
+
+    private void match(byte b) {
         // the header block starts right after a (virtual) CRLF
-        if (b == "\r\n\r\n".charAt(matched)) {
+        if (b == TERMINATOR[matched]) {
             matched++;
         } else {
             matched = b == '\r' ? 1 : 0;
diff --git a/main/src/main/java/org/apache/james/jdkim/impl/Message.java 
b/main/src/main/java/org/apache/james/jdkim/impl/Message.java
index 4f7b335..ec72711 100644
--- a/main/src/main/java/org/apache/james/jdkim/impl/Message.java
+++ b/main/src/main/java/org/apache/james/jdkim/impl/Message.java
@@ -25,7 +25,6 @@ import org.apache.james.mime4j.MimeIOException;
 import org.apache.james.mime4j.dom.MessageBuilder;
 import org.apache.james.mime4j.dom.MessageServiceFactory;
 import org.apache.james.mime4j.dom.SingleBody;
-import org.apache.james.mime4j.io.EOLConvertingInputStream;
 import org.apache.james.mime4j.stream.MimeConfig;
 
 import java.io.IOException;
@@ -58,7 +57,7 @@ public class Message implements Headers {
 
         // mime4j drops the fields whose name it cannot parse and normalizes 
the
         // others: the fields are taken from the raw header bytes instead.
-        HeaderCapturingInputStream header = new HeaderCapturingInputStream(new 
EOLConvertingInputStream(is));
+        HeaderCapturingInputStream header = new HeaderCapturingInputStream(new 
CrlfConvertingInputStream(is));
         try {
             this.message = mb.parseMessage(header);
         } catch (MimeIOException e) {
diff --git 
a/main/src/test/java/org/apache/james/jdkim/impl/CrlfConvertingInputStreamTest.java
 
b/main/src/test/java/org/apache/james/jdkim/impl/CrlfConvertingInputStreamTest.java
new file mode 100644
index 0000000..46dd1e0
--- /dev/null
+++ 
b/main/src/test/java/org/apache/james/jdkim/impl/CrlfConvertingInputStreamTest.java
@@ -0,0 +1,123 @@
+/******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one                 *
+ * or more contributor license agreements.  See the NOTICE file               *
+ * distributed with this work for additional information                      *
+ * regarding copyright ownership.  The ASF licenses this file                 *
+ * to you under the Apache License, Version 2.0 (the                          *
+ * "License"); you may not use this file except in compliance                 *
+ * with the License.  You may obtain a copy of the License at                 *
+ *                                                                            *
+ *   http://www.apache.org/licenses/LICENSE-2.0                               *
+ *                                                                            *
+ * Unless required by applicable law or agreed to in writing,                 *
+ * software distributed under the License is distributed on an                *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY                     *
+ * KIND, either express or implied.  See the License for the                  *
+ * specific language governing permissions and limitations                    *
+ * under the License.                                                         *
+ 
******************************************************************************/
+
+package org.apache.james.jdkim.impl;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Arrays;
+import java.util.Random;
+
+import org.apache.james.mime4j.io.EOLConvertingInputStream;
+import org.junit.Test;
+
+public class CrlfConvertingInputStreamTest {
+
+    private static final byte[] ALPHABET = {'\r', '\n', 'a', ' ', (byte) 0xff};
+
+    /**
+     * A source that returns at most a random number of octets per read, to 
move the block boundaries.
+     */
+    private static InputStream chopped(byte[] data, Random random) {
+        return new ByteArrayInputStream(data) {
+            @Override
+            public synchronized int read(byte[] b, int off, int len) {
+                return super.read(b, off, Math.min(len, 1 + 
random.nextInt(5)));
+            }
+        };
+    }
+
+    private static byte[] readAll(InputStream in, Random random) throws 
IOException {
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        byte[] block = new byte[16];
+        while (true) {
+            int read;
+            if (random.nextInt(4) == 0) {
+                int b = in.read();
+                if (b == -1) {
+                    break;
+                }
+                out.write(b);
+                continue;
+            }
+            read = in.read(block, 0, 1 + random.nextInt(block.length));
+            if (read == -1) {
+                break;
+            }
+            out.write(block, 0, read);
+        }
+        return out.toByteArray();
+    }
+
+    private static byte[] mime4j(byte[] data) throws IOException {
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        InputStream in = new EOLConvertingInputStream(new 
ByteArrayInputStream(data));
+        int b;
+        while ((b = in.read()) != -1) {
+            out.write(b);
+        }
+        return out.toByteArray();
+    }
+
+    @Test
+    public void shouldConvertLikeEOLConvertingInputStream() throws IOException 
{
+        Random random = new Random(5322);
+        for (int run = 0; run < 50000; run++) {
+            byte[] data = new byte[random.nextInt(40)];
+            for (int i = 0; i < data.length; i++) {
+                data[i] = ALPHABET[random.nextInt(ALPHABET.length)];
+            }
+            byte[] actual = readAll(new 
CrlfConvertingInputStream(chopped(data, random)), random);
+            assertThat(actual).as("input %s", 
Arrays.toString(data)).isEqualTo(mime4j(data));
+        }
+    }
+
+    @Test
+    public void shouldCaptureTheHeaderWhateverTheBlocks() throws IOException {
+        Random random = new Random(6376);
+        for (int run = 0; run < 50000; run++) {
+            byte[] data = new byte[random.nextInt(40)];
+            for (int i = 0; i < data.length; i++) {
+                data[i] = ALPHABET[random.nextInt(3)];
+            }
+            byte[] crlf = mime4j(data);
+            HeaderCapturingInputStream header = new 
HeaderCapturingInputStream(chopped(crlf, random));
+            assertThat(readAll(header, random)).isEqualTo(crlf);
+            assertThat(header.getHeader()).as("input %s", 
Arrays.toString(crlf)).isEqualTo(headerBlock(crlf));
+        }
+    }
+
+    // up to the first empty line, which may be the first line
+    private static byte[] headerBlock(byte[] crlf) {
+        byte[] prefixed = new byte[crlf.length + 2];
+        prefixed[0] = '\r';
+        prefixed[1] = '\n';
+        System.arraycopy(crlf, 0, prefixed, 2, crlf.length);
+        for (int i = 0; i + 4 <= prefixed.length; i++) {
+            if (prefixed[i] == '\r' && prefixed[i + 1] == '\n' && prefixed[i + 
2] == '\r' && prefixed[i + 3] == '\n') {
+                return Arrays.copyOf(crlf, i + 2);
+            }
+        }
+        return crlf;
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to