james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mwiederk...@apache.org
Subject svn commit: r733936 - in /james/mime4j/trunk/src: main/java/org/apache/james/mime4j/util/MimeUtil.java test/java/org/apache/james/mime4j/util/MimeUtilTest.java
Date Mon, 12 Jan 2009 22:19:17 GMT
Author: mwiederkehr
Date: Mon Jan 12 14:19:17 2009
New Revision: 733936

URL: http://svn.apache.org/viewvc?rev=733936&view=rev
Log:
Utility method for folding a long line into a multiple-line representation (needed for MIME4J-100).

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

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=733936&r1=733935&r2=733936&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 Mon Jan 12
14:19:17 2009
@@ -393,6 +393,60 @@
         return sb.toString();
     }
 
+    /**
+     * Splits the specified string into a multiple-line representation with
+     * lines no longer than 78 characters (see <a
+     * href='http://www.faqs.org/rfcs/rfc5322.html'>RFC 5322</a> section
+     * 2.2.3.). If the string contains non-whitespace sequences longer than 78
+     * characters a line break is inserted at the whitespace character following
+     * the sequence resulting in a line longer than 78 characters.
+     * 
+     * @param s
+     *            string to split.
+     * @param usedCharacters
+     *            number of characters already used up. Usually the number of
+     *            characters for header field name plus colon and one space.
+     * @return a multiple-line representation of the given string.
+     */
+    public static String fold(String s, int usedCharacters) {
+        final int maxCharacters = 78;
+
+        final int length = s.length();
+        if (usedCharacters + length <= maxCharacters)
+            return s;
+
+        StringBuilder sb = new StringBuilder();
+
+        int lastLineBreak = -usedCharacters;
+        int wspIdx = indexOfWsp(s, 0);
+        while (true) {
+            if (wspIdx == length) {
+                sb.append(s.substring(Math.max(0, lastLineBreak)));
+                return sb.toString();
+            }
+
+            int nextWspIdx = indexOfWsp(s, wspIdx + 1);
+
+            if (nextWspIdx - lastLineBreak > maxCharacters) {
+                sb.append(s.substring(Math.max(0, lastLineBreak), wspIdx));
+                sb.append("\r\n");
+                lastLineBreak = wspIdx;
+            }
+
+            wspIdx = nextWspIdx;
+        }
+    }
+
+    private static int indexOfWsp(String s, int fromIndex) {
+        final int len = s.length();
+        for (int index = fromIndex; index < len; index++) {
+            char c = s.charAt(index);
+            if (c == ' ' || c == '\t')
+                return index;
+        }
+        return len;
+    }
+
     private static synchronized int nextCounterValue() {
         return counter++;
     }

Added: 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=733936&view=auto
==============================================================================
--- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/util/MimeUtilTest.java (added)
+++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/util/MimeUtilTest.java Mon Jan
12 14:19:17 2009
@@ -0,0 +1,44 @@
+/*
+ * 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.mime4j.util;
+
+import junit.framework.TestCase;
+
+public class MimeUtilTest extends TestCase {
+
+    public void testFold() throws Exception {
+        assertEquals("this is\r\n a test", MimeUtil.fold("this is a test", 70));
+        assertEquals("this is\r\n a test", MimeUtil.fold("this is a test", 71));
+        assertEquals("this\r\n is a test", MimeUtil.fold("this is a test", 72));
+        assertEquals("this  \r\n   is a test", MimeUtil.fold(
+                "this     is a test", 72));
+    }
+
+    public void testFoldOverlyLongNonWhitespace() throws Exception {
+        String ninety = "1234567890123456789012345678901234567890"
+                + "12345678901234567890123456789012345678901234567890";
+        String input = String.format("testing 1 2 %s testing %s", ninety,
+                ninety);
+
+        String expected = String.format(
+                "testing 1 2\r\n %s\r\n testing\r\n %s", ninety, ninety);
+
+        assertEquals(expected, MimeUtil.fold(input, 0));
+    }
+
+}



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