james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nor...@apache.org
Subject svn commit: r1029458 - in /james/server/trunk: core-library/src/main/java/org/apache/james/core/ util/src/main/java/org/apache/james/util/stream/
Date Sun, 31 Oct 2010 19:23:18 GMT
Author: norman
Date: Sun Oct 31 19:23:17 2010
New Revision: 1029458

URL: http://svn.apache.org/viewvc?rev=1029458&view=rev
Log:
Optimize handling of MimeMessageWrapper and MimeMessageCopyOnWriteProxy when using MimeMessageInputStream
(JAMES-1097)

Added:
    james/server/trunk/util/src/main/java/org/apache/james/util/stream/CombinedInputStream.java
Modified:
    james/server/trunk/core-library/src/main/java/org/apache/james/core/MimeMessageInputStream.java
    james/server/trunk/core-library/src/main/java/org/apache/james/core/MimeMessageWrapper.java

Modified: james/server/trunk/core-library/src/main/java/org/apache/james/core/MimeMessageInputStream.java
URL: http://svn.apache.org/viewvc/james/server/trunk/core-library/src/main/java/org/apache/james/core/MimeMessageInputStream.java?rev=1029458&r1=1029457&r2=1029458&view=diff
==============================================================================
--- james/server/trunk/core-library/src/main/java/org/apache/james/core/MimeMessageInputStream.java
(original)
+++ james/server/trunk/core-library/src/main/java/org/apache/james/core/MimeMessageInputStream.java
Sun Oct 31 19:23:17 2010
@@ -25,58 +25,79 @@ import java.io.InputStream;
 import javax.mail.MessagingException;
 import javax.mail.internet.MimeMessage;
 
+import org.apache.james.util.stream.CombinedInputStream;
+
 /**
- * {@link InputStream} which contains the headers and the Body of the
- * wrapped {@link MimeMessage}
+ * Provide an {@link InputStream} over an {@link MimeMessage}
  * 
  */
 public class MimeMessageInputStream extends InputStream {
-    private final InputStream headersInputStream;
-    private final InputStream bodyInputStream;
-    private int cStream = 0;
+    private InputStream in;
 
     @SuppressWarnings("unchecked")
     public MimeMessageInputStream(MimeMessage message) throws IOException {
+        MimeMessage m = message;
+        if (m instanceof MimeMessageCopyOnWriteProxy) {
+            m = ((MimeMessageCopyOnWriteProxy) message).getWrappedMessage();
+        }
         try {
-            headersInputStream = new InternetHeadersInputStream(message.getAllHeaderLines());
 
-            // use the raw InputStream because we want to have no conversion here and just
obtain the original message body
-            this.bodyInputStream = message.getRawInputStream();
+            if (m instanceof MimeMessageWrapper) {
+                in = ((MimeMessageWrapper) m).getMessageInputStream();
+            } else {
+                in = new CombinedInputStream(new InputStream[] { new InternetHeadersInputStream(message.getAllHeaderLines()),
message.getRawInputStream() });
+
+            }
         } catch (MessagingException e) {
             throw new IOException("Unable to read MimeMessage: " + e.getMessage());
         }
+
     }
 
     @Override
     public int read() throws IOException {
-        int i = -1;
-        if (cStream == 0) {
-            i = headersInputStream.read();
-        } else {
-            i = bodyInputStream.read();
-        }
-
-        if (i == -1 && cStream == 0) {
-            cStream++;
-            return read();
-        }
-        return i;
+        return in.read();
 
     }
 
-    /** Closes all streams */
+    @Override
     public void close() throws IOException {
-        headersInputStream.close();
-        bodyInputStream.close();
+       in.close();
     }
 
-    /** Is there more data to read */
+    @Override
     public int available() throws IOException {
-        if (cStream == 0) {
-            return headersInputStream.available() + bodyInputStream.available() + 2;
-        } else {
-            return bodyInputStream.available();
-        }
+        return in.available();
+    }
+
+    @Override
+    public void mark(int readlimit) {
+        in.mark(readlimit);
+    }
+
+    @Override
+    public boolean markSupported() {
+        return in.markSupported();
+    }
+
+    @Override
+    public int read(byte[] b, int off, int len) throws IOException {
+        return in.read(b, off, len);
+    }
+
+    @Override
+    public int read(byte[] b) throws IOException {
+        return in.read(b);
+    }
+
+    @Override
+    public void reset() throws IOException {
+        in.reset();
+    }
+
+    @Override
+    public long skip(long n) throws IOException {
+        return in.skip(n);
     }
 
 }

Modified: james/server/trunk/core-library/src/main/java/org/apache/james/core/MimeMessageWrapper.java
URL: http://svn.apache.org/viewvc/james/server/trunk/core-library/src/main/java/org/apache/james/core/MimeMessageWrapper.java?rev=1029458&r1=1029457&r2=1029458&view=diff
==============================================================================
--- james/server/trunk/core-library/src/main/java/org/apache/james/core/MimeMessageWrapper.java
(original)
+++ james/server/trunk/core-library/src/main/java/org/apache/james/core/MimeMessageWrapper.java
Sun Oct 31 19:23:17 2010
@@ -25,6 +25,7 @@ import org.apache.commons.io.IOUtils;
 import org.apache.james.lifecycle.Disposable;
 import org.apache.james.lifecycle.LifecycleUtil;
 import org.apache.james.util.InternetPrintWriter;
+import org.apache.james.util.stream.CombinedInputStream;
 
 import javax.activation.DataHandler;
 import javax.mail.MessagingException;
@@ -559,6 +560,24 @@ public class MimeMessageWrapper
         } else return super.getRawInputStream();
     }
 
+    /**
+     * Return an {@link InputStream} which holds the full content of the message. This method
+     * tries to optimize this call as far as possible
+     * 
+     * @return messageInputStream
+     * @throws MessagingException
+     */
+    public InputStream getMessageInputStream() throws MessagingException{
+        if (!messageParsed && !isModified() && source != null) {
+            try {
+                return source.getInputStream();
+            } catch (IOException e) {
+                throw new MessagingException("Unable to get inputstream", e);
+            }
+        } else {
+            return new CombinedInputStream(new InputStream[] { new InternetHeadersInputStream(getAllHeaderLines()),
getRawInputStream()});
+        }
+    }
     
     
 }

Added: james/server/trunk/util/src/main/java/org/apache/james/util/stream/CombinedInputStream.java
URL: http://svn.apache.org/viewvc/james/server/trunk/util/src/main/java/org/apache/james/util/stream/CombinedInputStream.java?rev=1029458&view=auto
==============================================================================
--- james/server/trunk/util/src/main/java/org/apache/james/util/stream/CombinedInputStream.java
(added)
+++ james/server/trunk/util/src/main/java/org/apache/james/util/stream/CombinedInputStream.java
Sun Oct 31 19:23:17 2010
@@ -0,0 +1,63 @@
+/****************************************************************
+ * 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.util.stream;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Combine an array of {@link InputStream} and read from them
+ * 
+ *
+ */
+public class CombinedInputStream extends InputStream{
+
+    private InputStream[] streams;
+    private int current = 0;
+    public CombinedInputStream(InputStream[] streams) {
+        this.streams = streams;
+    }
+    
+    
+    @Override
+    public int read() throws IOException {
+        int i = streams[current].read();
+        if (i == -1 && current < streams.length -1) {
+            i = streams[++current].read();
+        }
+        return i;
+    }
+
+
+    @Override
+    public void close() throws IOException {
+        for (int i = 0 ; i < streams.length; i++) {
+            streams[i].close();
+        }
+    }
+
+
+    @Override
+    public boolean markSupported() {
+        return false;
+    }
+
+}



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


Mime
View raw message