james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject svn commit: r385297 - in /james/server/trunk/src/java/org/apache/james/core: MailImpl.java MimeMessageCopyOnWriteProxy.java
Date Sun, 12 Mar 2006 15:33:20 GMT
Author: bago
Date: Sun Mar 12 07:33:19 2006
New Revision: 385297

URL: http://svn.apache.org/viewcvs?rev=385297&view=rev
Log:
Refactored getMessageSize() in MailImpl and MimeMessageCopyOnWriteProxy to remove duplicated
code and ensure the size is always returned.
Also fixed the calculated size (CRLF were not counted) and added a private outputstream to
count the size by writing the message.

Modified:
    james/server/trunk/src/java/org/apache/james/core/MailImpl.java
    james/server/trunk/src/java/org/apache/james/core/MimeMessageCopyOnWriteProxy.java

Modified: james/server/trunk/src/java/org/apache/james/core/MailImpl.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/java/org/apache/james/core/MailImpl.java?rev=385297&r1=385296&r2=385297&view=diff
==============================================================================
--- james/server/trunk/src/java/org/apache/james/core/MailImpl.java (original)
+++ james/server/trunk/src/java/org/apache/james/core/MailImpl.java Sun Mar 12 07:33:19 2006
@@ -58,6 +58,31 @@
  * @version CVS $Revision$ $Date$
  */
 public class MailImpl implements Disposable, Mail {
+    
+    /**
+     * Slow method to calculate the exact size of a message!
+     */
+    private final class SizeCalculatorOutputStream extends OutputStream {
+        long size = 0;
+
+        public void write(int arg0) throws IOException {
+            size++;
+        }
+
+        public long getSize() {
+            return size;
+        }
+
+        public void write(byte[] arg0, int arg1, int arg2) throws IOException {
+            size += arg2;
+        }
+
+        public void write(byte[] arg0) throws IOException {
+            size += arg0.length;
+        }
+    }
+
+
     /**
      * We hardcode the serialVersionUID so that from James 1.2 on,
      * MailImpl will be deserializable (so your mail doesn't get lost)
@@ -368,24 +393,44 @@
     public long getMessageSize() throws MessagingException {
         //If we have a MimeMessageWrapper, then we can ask it for just the
         //  message size and skip calculating it
+        long size = -1;
+        
         if (message instanceof MimeMessageWrapper) {
             MimeMessageWrapper wrapper = (MimeMessageWrapper) message;
-            return wrapper.getMessageSize();
-        }
-        if (message instanceof MimeMessageCopyOnWriteProxy) {
+            size = wrapper.getMessageSize();
+        } else if (message instanceof MimeMessageCopyOnWriteProxy) {
             MimeMessageCopyOnWriteProxy wrapper = (MimeMessageCopyOnWriteProxy) message;
-            return wrapper.getMessageSize();
+            size = wrapper.getMessageSize();
+        } else {
+            //SK: Should probably eventually store this as a locally
+            //  maintained value (so we don't have to load and reparse
+            //  messages each time).
+            size = message.getSize();
+            if (size != -1) {
+                Enumeration e = message.getAllHeaderLines();
+                if (e.hasMoreElements()) {
+                    size += 2;
+                }
+                while (e.hasMoreElements()) {
+                    // add 2 bytes for the CRLF
+                    size += ((String) e.nextElement()).length()+2;
+                }
+            }
         }
-        //SK: Should probably eventually store this as a locally
-        //  maintained value (so we don't have to load and reparse
-        //  messages each time).
-        long size = message.getSize();
-        Enumeration e = message.getAllHeaderLines();
-        while (e.hasMoreElements()) {
-            size += ((String) e.nextElement()).length();
+        
+        if (size == -1) {
+            SizeCalculatorOutputStream out = new SizeCalculatorOutputStream();
+            try {
+                message.writeTo(out);
+            } catch (IOException e) {
+                // should never happen as SizeCalculator does not actually throw IOExceptions.
+                throw new MessagingException("IOException wrapped by getMessageSize",e);
+            }
+            size = out.getSize();
         }
         return size;
     }
+    
     /**
      * Set the error message associated with this MailImpl.
      *

Modified: james/server/trunk/src/java/org/apache/james/core/MimeMessageCopyOnWriteProxy.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/java/org/apache/james/core/MimeMessageCopyOnWriteProxy.java?rev=385297&r1=385296&r2=385297&view=diff
==============================================================================
--- james/server/trunk/src/java/org/apache/james/core/MimeMessageCopyOnWriteProxy.java (original)
+++ james/server/trunk/src/java/org/apache/james/core/MimeMessageCopyOnWriteProxy.java Sun
Mar 12 07:33:19 2006
@@ -789,12 +789,8 @@
         if (wrapped instanceof MimeMessageWrapper) {
             return ((MimeMessageWrapper) wrapped).getMessageSize();
         } else {
-            long size = wrapped.getSize();
-            Enumeration e = wrapped.getAllHeaderLines();
-            while (e.hasMoreElements()) {
-                size += ((String) e.nextElement()).length();
-            }
-            return size;
+            // this is already handled by MailImpl.
+            return -1;
         }
     }
 



---------------------------------------------------------------------
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