james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1087265 - in /james/mime4j/trunk/dom/src: main/java/org/apache/james/mime4j/dom/field/ main/java/org/apache/james/mime4j/field/ main/java/org/apache/james/mime4j/message/ test/java/org/apache/james/mime4j/field/
Date Thu, 31 Mar 2011 12:02:16 GMT
Author: olegk
Date: Thu Mar 31 12:02:16 2011
New Revision: 1087265

URL: http://svn.apache.org/viewvc?rev=1087265&view=rev
Log:
Moved field formatting logic from Field interface to MimeWriter

Added:
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/field/FieldRawData.java
  (with props)
Modified:
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/field/Field.java
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/AbstractField.java
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MimeWriter.java
    james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/FieldsTest.java

Modified: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/field/Field.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/field/Field.java?rev=1087265&r1=1087264&r2=1087265&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/field/Field.java (original)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/field/Field.java Thu
Mar 31 12:02:16 2011
@@ -19,9 +19,6 @@
 
 package org.apache.james.mime4j.dom.field;
 
-import java.io.IOException;
-import java.io.OutputStream;
-
 /**
  * Abstract MIME field.
  */
@@ -41,11 +38,4 @@ public interface Field {
      */
     String getBody();
 
-    /**
-     * Writes the original raw field bytes to an output stream.
-     * The output is folded, the last CRLF is not included.
-     * @throws IOException
-     */
-    void writeTo(OutputStream out) throws IOException;
-    
 }

Added: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/field/FieldRawData.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/field/FieldRawData.java?rev=1087265&view=auto
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/field/FieldRawData.java
(added)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/field/FieldRawData.java
Thu Mar 31 12:02:16 2011
@@ -0,0 +1,35 @@
+/****************************************************************
+ * 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.dom.field;
+
+import org.apache.james.mime4j.util.ByteSequence;
+
+/**
+ * Abstract MIME field.
+ */
+public interface FieldRawData extends Field {
+    
+    /**
+     * Gets original (raw) representation of the field, if available, 
+     * <code>null</code> otherwise.
+     */
+    ByteSequence getRaw();
+
+}

Propchange: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/field/FieldRawData.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/field/FieldRawData.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/field/FieldRawData.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/AbstractField.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/AbstractField.java?rev=1087265&r1=1087264&r2=1087265&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/AbstractField.java
(original)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/AbstractField.java
Thu Mar 31 12:02:16 2011
@@ -19,10 +19,8 @@
 
 package org.apache.james.mime4j.field;
 
-import java.io.IOException;
-import java.io.OutputStream;
-
 import org.apache.james.mime4j.codec.DecodeMonitor;
+import org.apache.james.mime4j.dom.field.FieldRawData;
 import org.apache.james.mime4j.dom.field.ParseException;
 import org.apache.james.mime4j.dom.field.ParsedField;
 import org.apache.james.mime4j.util.ByteSequence;
@@ -30,7 +28,7 @@ import org.apache.james.mime4j.util.Byte
 /**
  * The base class of all field classes.
  */
-public abstract class AbstractField implements ParsedField {
+public abstract class AbstractField implements ParsedField, FieldRawData {
 
     private final String name;
     private final String body;
@@ -59,13 +57,6 @@ public abstract class AbstractField impl
     }
     
     /**
-     * @see org.apache.james.mime4j.dom.field.Field#writeTo(java.io.OutputStream)
-     */
-    public void writeTo(OutputStream out) throws IOException {
-        out.write(raw.toByteArray());
-    }
-    
-    /**
      * Gets the unfolded, unparsed and possibly encoded (see RFC 2047) field 
      * body string.
      * 
@@ -76,6 +67,14 @@ public abstract class AbstractField impl
     }
 
     /**
+     * Gets original (raw) representation of the field, if available, 
+     * <code>null</code> otherwise.
+     */
+    public ByteSequence getRaw() {
+        return raw;
+    }
+
+    /**
      * @see ParsedField#isValidField() 
      */
     public boolean isValidField() {

Modified: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MimeWriter.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MimeWriter.java?rev=1087265&r1=1087264&r2=1087265&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MimeWriter.java (original)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MimeWriter.java Thu
Mar 31 12:02:16 2011
@@ -33,6 +33,7 @@ import org.apache.james.mime4j.dom.Singl
 import org.apache.james.mime4j.dom.field.ContentTypeField;
 import org.apache.james.mime4j.dom.field.Field;
 import org.apache.james.mime4j.dom.field.FieldName;
+import org.apache.james.mime4j.dom.field.FieldRawData;
 import org.apache.james.mime4j.util.ByteArrayBuffer;
 import org.apache.james.mime4j.util.ByteSequence;
 import org.apache.james.mime4j.util.ContentUtil;
@@ -195,7 +196,21 @@ public class MimeWriter {
      */
     public void writeHeader(Header header, OutputStream out) throws IOException {
         for (Field field : header) {
-            field.writeTo(out);
+            ByteSequence raw = null;
+            if (field instanceof FieldRawData) {
+                raw = ((FieldRawData) field).getRaw();
+            }
+            if (raw == null) {
+                StringBuilder buf = new StringBuilder();
+                buf.append(field.getName());
+                buf.append(": ");
+                String body = field.getBody();
+                if (body != null) {
+                    buf.append(body);
+                }
+                raw = ContentUtil.encode(MimeUtil.fold(buf.toString(), 0));
+            }
+            writeBytes(raw, out);
             out.write(CRLF);
         }
 

Modified: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/FieldsTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/FieldsTest.java?rev=1087265&r1=1087264&r2=1087265&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/FieldsTest.java (original)
+++ james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/FieldsTest.java Thu
Mar 31 12:02:16 2011
@@ -19,7 +19,6 @@
 
 package org.apache.james.mime4j.field;
 
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.Date;
@@ -37,11 +36,12 @@ import org.apache.james.mime4j.dom.field
 import org.apache.james.mime4j.dom.field.ContentTypeField;
 import org.apache.james.mime4j.dom.field.DateTimeField;
 import org.apache.james.mime4j.dom.field.Field;
+import org.apache.james.mime4j.dom.field.FieldRawData;
 import org.apache.james.mime4j.dom.field.MailboxField;
 import org.apache.james.mime4j.dom.field.MailboxListField;
 import org.apache.james.mime4j.field.Fields;
 import org.apache.james.mime4j.field.address.AddressBuilder;
-import org.apache.james.mime4j.util.ByteArrayBuffer;
+import org.apache.james.mime4j.util.ByteSequence;
 import org.apache.james.mime4j.util.ContentUtil;
 import org.apache.james.mime4j.util.MimeUtil;
 
@@ -384,15 +384,25 @@ public class FieldsTest extends TestCase
         }
     }
 
-    public static String decode(Field f) {
-        ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        try {
-            f.writeTo(bos);
-        } catch (IOException e) {
-            throw new RuntimeException("bytearrayoutputstream doens't throw this exception");
+    public static String decode(Field f) throws IOException {
+        String s = null;
+        if (f instanceof FieldRawData) {
+            ByteSequence raw = ((FieldRawData) f).getRaw();
+            if (raw != null) {
+                s = ContentUtil.decode(raw);
+            }
+        }
+        if (s == null) {
+            StringBuilder buf = new StringBuilder();
+            buf.append(f.getName());
+            buf.append(": ");
+            String body = f.getBody();
+            if (body != null) {
+                buf.append(body);
+            }
+            s = MimeUtil.fold(buf.toString(), 0);
         }
-        ByteArrayBuffer bab = new ByteArrayBuffer(bos.toByteArray(), true);
-        return ContentUtil.decode(bab);
+        return s;
     }
 
 }



Mime
View raw message