james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mwiederk...@apache.org
Subject svn commit: r740408 - in /james/mime4j/trunk/src: main/java/org/apache/james/mime4j/field/Field.java main/java/org/apache/james/mime4j/util/MimeUtil.java test/java/org/apache/james/mime4j/util/MimeUtilTest.java
Date Tue, 03 Feb 2009 20:15:10 GMT
Author: mwiederkehr
Date: Tue Feb  3 20:15:10 2009
New Revision: 740408

URL: http://svn.apache.org/viewvc?rev=740408&view=rev
Log:
added MimeUtil.unfold(String)

Modified:
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Field.java
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/util/MimeUtil.java
    james/mime4j/trunk/src/test/java/org/apache/james/mime4j/util/MimeUtilTest.java

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Field.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Field.java?rev=740408&r1=740407&r2=740408&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Field.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Field.java Tue Feb  3 20:15:10
2009
@@ -23,6 +23,7 @@
 import java.util.regex.Pattern;
 
 import org.apache.james.mime4j.MimeException;
+import org.apache.james.mime4j.util.MimeUtil;
 
 /**
  * The base class of all field classes.
@@ -98,7 +99,7 @@
         /*
          * Unfold the field.
          */
-        final String unfolded = raw.replaceAll("\r|\n", "");
+        final String unfolded = MimeUtil.unfold(raw);
         
         /*
          * Split into name and value.
@@ -144,7 +145,7 @@
         String raw = name + ": " + body;
 
         // Unfold body
-        body = body.replaceAll("\r|\n", "");
+        body = MimeUtil.unfold(body);
 
         return parser.parse(name, body, raw);
     }

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/util/MimeUtil.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/util/MimeUtil.java?rev=740408&r1=740407&r2=740408&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/util/MimeUtil.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/util/MimeUtil.java Tue Feb  3
20:15:10 2009
@@ -175,19 +175,8 @@
     @SuppressWarnings("fallthrough")
     public static Map<String, String> getHeaderParams(String pValue) {
         pValue = pValue.trim();
-        
-        /*
-         * Unfold Content-Type value
-         */
-        StringBuilder sb = new StringBuilder(128);
-        for (int i = 0; i < pValue.length(); i++) {
-            char c = pValue.charAt(i);
-            if (c == '\r' || c == '\n') {
-                continue;
-            }
-            sb.append(c);
-        }
-        pValue = sb.toString();
+
+        pValue = unfold(pValue);
         
         Map<String, String> result = new HashMap<String, String>();
 
@@ -467,6 +456,43 @@
         }
     }
 
+    /**
+     * Unfold a multiple-line representation into a single line.
+     * 
+     * @param s
+     *            string to unfold.
+     * @return unfolded string.
+     */
+    public static String unfold(String s) {
+        final int length = s.length();
+        for (int idx = 0; idx < length; idx++) {
+            char c = s.charAt(idx);
+            if (c == '\r' || c == '\n') {
+                return unfold0(s, idx);
+            }
+        }
+
+        return s;
+    }
+
+    private static String unfold0(String s, int crlfIdx) {
+        final int length = s.length();
+        StringBuilder sb = new StringBuilder(length);
+
+        if (crlfIdx > 0) {
+            sb.append(s.substring(0, crlfIdx));
+        }
+
+        for (int idx = crlfIdx + 1; idx < length; idx++) {
+            char c = s.charAt(idx);
+            if (c != '\r' && c != '\n') {
+                sb.append(c);
+            }
+        }
+
+        return sb.toString();
+    }
+
     private static int indexOfWsp(String s, int fromIndex) {
         final int len = s.length();
         for (int index = fromIndex; index < len; index++) {

Modified: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/util/MimeUtilTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/util/MimeUtilTest.java?rev=740408&r1=740407&r2=740408&view=diff
==============================================================================
--- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/util/MimeUtilTest.java (original)
+++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/util/MimeUtilTest.java Tue Feb
 3 20:15:10 2009
@@ -17,7 +17,6 @@
  * under the License.                                           *
  ****************************************************************/
 
-
 package org.apache.james.mime4j.util;
 
 import junit.framework.TestCase;
@@ -44,4 +43,28 @@
         assertEquals(expected, MimeUtil.fold(input, 0));
     }
 
+    public void testUnfold() throws Exception {
+        assertEquals("", MimeUtil.unfold(""));
+        assertEquals("x", MimeUtil.unfold("x"));
+        assertEquals(" x ", MimeUtil.unfold(" x "));
+
+        assertEquals("", MimeUtil.unfold("\r"));
+        assertEquals("", MimeUtil.unfold("\n"));
+        assertEquals("", MimeUtil.unfold("\r\n"));
+
+        assertEquals(" ", MimeUtil.unfold(" \n"));
+        assertEquals(" ", MimeUtil.unfold("\n "));
+        assertEquals(" ", MimeUtil.unfold(" \r\n"));
+        assertEquals(" ", MimeUtil.unfold("\r\n "));
+
+        assertEquals("this is a test", MimeUtil.unfold("this is\r\n a test"));
+        assertEquals("this is a test", MimeUtil.unfold("this is\r\n a test"));
+        assertEquals("this is a test", MimeUtil.unfold("this\r\n is a test"));
+        assertEquals("this     is a test", MimeUtil
+                .unfold("this  \r\n   is a test"));
+
+        assertEquals("this is a test", MimeUtil
+                .unfold("this\r\n is\r\n a\r\n test"));
+    }
+
 }



Mime
View raw message