You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by no...@apache.org on 2010/12/25 13:44:52 UTC

svn commit: r1052775 - /james/server/trunk/core/src/main/java/org/apache/james/core/MimeMessageWrapper.java

Author: norman
Date: Sat Dec 25 12:44:52 2010
New Revision: 1052775

URL: http://svn.apache.org/viewvc?rev=1052775&view=rev
Log:
Only copy the whole message content if really needed. Optimization for JAMES-1154

Modified:
    james/server/trunk/core/src/main/java/org/apache/james/core/MimeMessageWrapper.java

Modified: james/server/trunk/core/src/main/java/org/apache/james/core/MimeMessageWrapper.java
URL: http://svn.apache.org/viewvc/james/server/trunk/core/src/main/java/org/apache/james/core/MimeMessageWrapper.java?rev=1052775&r1=1052774&r2=1052775&view=diff
==============================================================================
--- james/server/trunk/core/src/main/java/org/apache/james/core/MimeMessageWrapper.java (original)
+++ james/server/trunk/core/src/main/java/org/apache/james/core/MimeMessageWrapper.java Sat Dec 25 12:44:52 2010
@@ -41,6 +41,7 @@ import javax.mail.util.SharedByteArrayIn
 import org.apache.commons.io.IOUtils;
 import org.apache.james.lifecycle.api.Disposable;
 import org.apache.james.lifecycle.api.LifecycleUtil;
+import org.apache.james.util.stream.CombinedInputStream;
 
 /**
  * This object wraps a MimeMessage, only loading the underlying MimeMessage
@@ -570,6 +571,7 @@ public class MimeMessageWrapper
      * @throws MessagingException
      */
     
+    @SuppressWarnings("unchecked")
     public synchronized InputStream getMessageInputStream() throws MessagingException{
         if (!messageParsed && !isModified() && source != null) {
             try {
@@ -578,13 +580,22 @@ public class MimeMessageWrapper
                 throw new MessagingException("Unable to get inputstream", e);
             }
         } else {
-            
-            //TODO: Optimise me...
-            ByteArrayOutputStream out = new ByteArrayOutputStream();
             try {
-                writeTo(out);
-                return new ByteArrayInputStream(out.toByteArray());
 
+                if (!bodyModified && headersModified && source != null) {
+                    // ok only the headers were modified so we don't need to copy the whole message content into memory 
+                    InputStream in = source.getInputStream();
+                    // skip over headers from original stream we want to use the in memory ones
+                    new MailHeaders(in);
+                
+                    // now construct the new stream using the in memory headers and the body from the original source
+                    return new CombinedInputStream(new InputStream[]{new InternetHeadersInputStream(getAllHeaderLines()), in});
+                } else {
+                    // the body was changed so we have no other solution to copy it into memory first :(
+                    ByteArrayOutputStream out = new ByteArrayOutputStream();
+                    writeTo(out);
+                    return new ByteArrayInputStream(out.toByteArray());
+                }
             } catch (IOException e) {
                 throw new MessagingException("Unable to get inputstream", e);
             }



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