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/10/30 16:38:33 UTC

svn commit: r1029085 - in /james/server/trunk/core-library/src/main/java/org/apache/james/core: InternetHeadersInputStream.java MimeMessageWrapper.java

Author: norman
Date: Sat Oct 30 14:38:33 2010
New Revision: 1029085

URL: http://svn.apache.org/viewvc?rev=1029085&view=rev
Log:
Prevent copy of InternetHeader by just using a InputStream over them. (JAMES-1096)

Added:
    james/server/trunk/core-library/src/main/java/org/apache/james/core/InternetHeadersInputStream.java
Modified:
    james/server/trunk/core-library/src/main/java/org/apache/james/core/MimeMessageWrapper.java

Added: james/server/trunk/core-library/src/main/java/org/apache/james/core/InternetHeadersInputStream.java
URL: http://svn.apache.org/viewvc/james/server/trunk/core-library/src/main/java/org/apache/james/core/InternetHeadersInputStream.java?rev=1029085&view=auto
==============================================================================
--- james/server/trunk/core-library/src/main/java/org/apache/james/core/InternetHeadersInputStream.java (added)
+++ james/server/trunk/core-library/src/main/java/org/apache/james/core/InternetHeadersInputStream.java Sat Oct 30 14:38:33 2010
@@ -0,0 +1,91 @@
+/****************************************************************
+ * 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.core;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.util.Enumeration;
+
+import javax.mail.internet.InternetHeaders;
+
+/**
+ * Provide an {@link InputStream} over an {@link InternetHeaders} instance. When the end of {@link InternetHeaders} are reached a {@link #LINE_SEPERATOR} is append
+ * 
+ *
+ */
+public class InternetHeadersInputStream extends InputStream{
+
+    private final static String LINE_SEPERATOR = "\r\n";
+    
+    private Enumeration<String> headerLines;
+    private byte[] currLine;
+    private int pos = 0;
+    
+    @SuppressWarnings("unchecked")
+    public InternetHeadersInputStream(InternetHeaders headers) {
+        this.headerLines = headers.getAllHeaderLines();
+    }
+    @Override
+    public int read() throws IOException {
+        if (currLine == null || pos == currLine.length) {
+            if (readNextLine() == false) {
+                return -1;
+            }
+        }
+        return currLine[pos++];
+    }
+
+    /**
+     * Load the next header line if possible
+     * 
+     * @return true if there was an headerline which could be read
+     * 
+     * @throws IOException
+     */
+    private boolean readNextLine() throws IOException{
+        if (headerLines.hasMoreElements()) {
+            try {
+                pos = 0;
+                String line =  (headerLines.nextElement() + LINE_SEPERATOR);
+                // Add seperator to show that headers are complete
+                if (headerLines.hasMoreElements() == false) {
+                    line += LINE_SEPERATOR;
+                }
+                currLine = line.getBytes("US-ASCII");
+                return true;
+            } catch (UnsupportedEncodingException e) {
+                // should never happen
+                throw new IOException("Unable to decode header string", e);
+            }
+        } else {
+            return false;
+        }
+    }
+    
+    @Override
+    public void close() throws IOException {
+        currLine = null;
+    }
+    
+    
+}

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=1029085&r1=1029084&r2=1029085&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 Sat Oct 30 14:38:33 2010
@@ -34,7 +34,6 @@ import javax.mail.internet.MimeMessage;
 import javax.mail.util.SharedByteArrayInputStream;
 
 import java.io.BufferedWriter;
-import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -123,12 +122,9 @@ public class MimeMessageWrapper
             source = ((MimeMessageWrapper) original).source.share();
             // this probably speed up things
             if (((MimeMessageWrapper) original).headers != null) {
-                ByteArrayOutputStream temp = new ByteArrayOutputStream();
                 
-                //TODO: Remove the byte[] array usage
                 InternetHeaders ih = ((MimeMessageWrapper) original).headers;
-                MimeMessageUtil.writeHeadersTo(ih.getAllHeaderLines(),temp);
-                headers = createInternetHeaders(new ByteArrayInputStream(temp.toByteArray()));
+                headers = createInternetHeaders(new InternetHeadersInputStream(ih));
                 headersModified = ((MimeMessageWrapper) original).headersModified;
             }
         }



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