This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-mime4j.git

commit ad155f9797038ba91a6fe79ae7cdc426899da6dc
Author: Benoit Tellier <btell...@linagora.com>
AuthorDate: Mon Jun 20 22:55:54 2022 +0700

    MIME4J-318 Write single body backed by ByteArrayOutputStream
    
    Reduce allocation rate by ~30% which allow to fasten Mime
    message parsing from 45 us to 39us, which represents a ~12%
    speedup.
---
 .../org/apache/james/mime4j/util/ContentUtil.java  |  9 +++
 .../james/mime4j/message/BasicBodyFactory.java     | 67 +++++++++++++++++++++-
 2 files changed, 74 insertions(+), 2 deletions(-)

diff --git a/core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java 
b/core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java
index a6c29696..ce4d4121 100644
--- a/core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java
+++ b/core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java
@@ -98,6 +98,15 @@ public class ContentUtil {
         return buf.toByteArray();
     }
 
+    public static ByteArrayOutputStream bufferEfficient(final InputStream in) 
throws IOException {
+        if (in == null) {
+            throw new IllegalArgumentException("Input stream may not be null");
+        }
+        ByteArrayOutputStream buf = new ByteArrayOutputStream();
+        copy(in, buf);
+        return buf;
+    }
+
     public static String buffer(final Reader in) throws IOException {
         if (in == null) {
             throw new IllegalArgumentException("Reader may not be null");
diff --git 
a/dom/src/main/java/org/apache/james/mime4j/message/BasicBodyFactory.java 
b/dom/src/main/java/org/apache/james/mime4j/message/BasicBodyFactory.java
index d4d0571a..9bb57ab6 100644
--- a/dom/src/main/java/org/apache/james/mime4j/message/BasicBodyFactory.java
+++ b/dom/src/main/java/org/apache/james/mime4j/message/BasicBodyFactory.java
@@ -29,6 +29,7 @@ import java.nio.charset.Charset;
 import java.nio.charset.IllegalCharsetNameException;
 import java.nio.charset.UnsupportedCharsetException;
 
+import org.apache.commons.io.output.ByteArrayOutputStream;
 import org.apache.james.mime4j.Charsets;
 import org.apache.james.mime4j.dom.BinaryBody;
 import org.apache.james.mime4j.dom.SingleBody;
@@ -116,7 +117,7 @@ public class BasicBodyFactory implements BodyFactory {
         if (content == null) {
             throw new IllegalArgumentException("Input stream may not be null");
         }
-        return new StringBody2(ContentUtil.buffer(content), 
resolveCharset(mimeCharset));
+        return new StringBody3(ContentUtil.bufferEfficient(content), 
resolveCharset(mimeCharset));
     }
 
     public TextBody textBody(final String text, final Charset charset) {
@@ -138,7 +139,7 @@ public class BasicBodyFactory implements BodyFactory {
     }
 
     public BinaryBody binaryBody(final InputStream is) throws IOException {
-        return new BinaryBody1(ContentUtil.buffer(is));
+        return new BinaryBody3(ContentUtil.bufferEfficient(is));
     }
 
     public BinaryBody binaryBody(final byte[] buf) {
@@ -220,6 +221,43 @@ public class BasicBodyFactory implements BodyFactory {
 
     }
 
+    static class StringBody3 extends TextBody {
+
+        private final ByteArrayOutputStream content;
+        private final Charset charset;
+
+        StringBody3(final ByteArrayOutputStream content, final Charset 
charset) {
+            super();
+            this.content = content;
+            this.charset = charset;
+        }
+
+        @Override
+        public String getMimeCharset() {
+            return this.charset != null ? this.charset.name() : null;
+        }
+
+        @Override
+        public Reader getReader() throws IOException {
+            return new InputStreamReader(this.content.toInputStream(), 
this.charset);
+        }
+
+        @Override
+        public InputStream getInputStream() throws IOException {
+            return this.content.toInputStream();
+        }
+
+        @Override
+        public void dispose() {
+        }
+
+        @Override
+        public SingleBody copy() {
+            return new StringBody3(this.content, this.charset);
+        }
+
+    }
+
     static class BinaryBody1 extends BinaryBody {
 
         private final byte[] content;
@@ -245,6 +283,31 @@ public class BasicBodyFactory implements BodyFactory {
 
     }
 
+    static class BinaryBody3 extends BinaryBody {
+
+        private final ByteArrayOutputStream content;
+
+        BinaryBody3(ByteArrayOutputStream content) {
+            super();
+            this.content = content;
+        }
+
+        @Override
+        public InputStream getInputStream() throws IOException {
+            return content.toInputStream();
+        }
+
+        @Override
+        public void dispose() {
+        }
+
+        @Override
+        public SingleBody copy() {
+            return new BinaryBody3(content);
+        }
+
+    }
+
     static class BinaryBody2 extends BinaryBody {
 
         private final String content;


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org
For additional commands, e-mail: server-dev-h...@james.apache.org

Reply via email to