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 d9feda8999be1343575cd5994067c7f2d0792f38
Author: Benoit TELLIER <[email protected]>
AuthorDate: Thu Nov 12 16:15:10 2026 +0100

    [PERF] BufferedLineReaderInputStream: release does not allocate a shift 
table
    
    Since 10ad0b96, release() replaced the shift table it hands back to the
    recycler with a new int[256], so that a released stream never writes into
    a table another stream now uses. That is 1 KB allocated for every stream
    released: 6 KB per parse of a multipart message, as the benchmarks showed.
    
    The table is now dropped, and indexOf returns -1 on a released stream,
    whose buffer is empty anyway, before the table is reached.
---
 .../james/mime4j/io/BufferedLineReaderInputStream.java      |  6 ++++--
 .../james/mime4j/io/BufferedLineReaderInputStreamTest.java  | 13 +++++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git 
a/core/src/main/java/org/apache/james/mime4j/io/BufferedLineReaderInputStream.java
 
b/core/src/main/java/org/apache/james/mime4j/io/BufferedLineReaderInputStream.java
index 96aaad8e..57c91288 100644
--- 
a/core/src/main/java/org/apache/james/mime4j/io/BufferedLineReaderInputStream.java
+++ 
b/core/src/main/java/org/apache/james/mime4j/io/BufferedLineReaderInputStream.java
@@ -149,7 +149,8 @@ public class BufferedLineReaderInputStream extends 
LineReaderInputStream {
         bufferRecycler.releaseIntBuffer(shiftTable);
         // The arrays may now be handed to another stream: never read nor 
write them again
         this.buffer = EMPTY_BYTES;
-        this.shiftTable = new int[256];
+        // No new table: a released stream searches nothing (see indexOf)
+        this.shiftTable = null;
         this.cachedShiftPattern = null;
         this.bufpos = 0;
         this.buflen = 0;
@@ -275,7 +276,8 @@ public class BufferedLineReaderInputStream extends 
LineReaderInputStream {
         if (off < this.bufpos || len < 0 || off + len > this.buflen) {
             throw new IndexOutOfBoundsException("looking for 
"+off+"("+len+")"+" in "+bufpos+"/"+buflen);
         }
-        if (len < pattern.length) {
+        if (len < pattern.length || released) {
+            // Released: the shift table went back to the recycler, and the 
buffer is empty
             return -1;
         }
 
diff --git 
a/core/src/test/java/org/apache/james/mime4j/io/BufferedLineReaderInputStreamTest.java
 
b/core/src/test/java/org/apache/james/mime4j/io/BufferedLineReaderInputStreamTest.java
index d9d792e3..2ce3242e 100644
--- 
a/core/src/test/java/org/apache/james/mime4j/io/BufferedLineReaderInputStreamTest.java
+++ 
b/core/src/test/java/org/apache/james/mime4j/io/BufferedLineReaderInputStreamTest.java
@@ -198,4 +198,17 @@ public class BufferedLineReaderInputStreamTest {
             }
         }
     }
+
+    @Test
+    public void aReleasedStreamShouldReadAndFindNothing() throws Exception {
+        BufferedLineReaderInputStream in = new BufferedLineReaderInputStream(
+                new 
java.io.ByteArrayInputStream(ContentUtil.toAsciiByteArray("abc\r\n--b\r\n")), 
4096);
+        Assert.assertTrue(in.fillBuffer() > 0);
+        in.release();
+
+        Assert.assertEquals(-1, in.indexOf(new byte[0], 0, 0));
+        Assert.assertEquals(-1, 
in.indexOf(ContentUtil.toAsciiByteArray("--b"), 0, 0));
+        Assert.assertEquals(-1, in.read());
+        Assert.assertEquals(-1, in.readLine(new ByteArrayBuffer(16)));
+    }
 }


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

Reply via email to