james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1482374 - in /james/mime4j/trunk: core/src/main/java/org/apache/james/mime4j/util/ core/src/test/java/org/apache/james/mime4j/codec/ dom/src/main/java/org/apache/james/mime4j/message/
Date Tue, 14 May 2013 15:10:13 GMT
Author: olegk
Date: Tue May 14 15:10:12 2013
New Revision: 1482374

URL: http://svn.apache.org/r1482374
Log:
Added builder class for BinaryPart and TextPart instances

Added:
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/BodyBuilder.java
  (with props)
Removed:
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/BasicBinaryBody.java
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/StringBody.java
Modified:
    james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java
    james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/Base64InputStreamTest.java
    james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/EncoderUtilTest.java
    james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableEncodeTest.java
    james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableTextEncodeTest.java
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/BasicBodyFactory.java

Modified: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java?rev=1482374&r1=1482373&r2=1482374&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java (original)
+++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java Tue
May 14 15:10:12 2013
@@ -19,10 +19,14 @@
 
 package org.apache.james.mime4j.util;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.Reader;
+import java.io.StringWriter;
 import java.io.UnsupportedEncodingException;
+import java.io.Writer;
 import java.nio.ByteBuffer;
 import java.nio.CharBuffer;
 import java.nio.charset.Charset;
@@ -54,6 +58,38 @@ public class ContentUtil {
     }
 
     /**
+     * Copies the contents of one stream to the other.
+     * @param in not null
+     * @param out not null
+     * @throws IOException
+     */
+    public static void copy(final Reader in, final Writer out) throws IOException {
+        final char[] buffer = new char[DEFAULT_COPY_BUFFER_SIZE];
+        int inputLength;
+        while (-1 != (inputLength = in.read(buffer))) {
+            out.write(buffer, 0, inputLength);
+        }
+    }
+
+    public static byte[] buffer(final InputStream in) throws IOException {
+        if (in == null) {
+            throw new IllegalArgumentException("Input stream may not be null");
+        }
+        ByteArrayOutputStream buf = new ByteArrayOutputStream();
+        copy(in, buf);
+        return buf.toByteArray();
+    }
+
+    public static String buffer(final Reader in) throws IOException {
+        if (in == null) {
+            throw new IllegalArgumentException("Reader may not be null");
+        }
+        StringWriter buf = new StringWriter();
+        copy(in, buf);
+        return buf.toString();
+    }
+
+    /**
      * Encodes the specified string into an immutable sequence of bytes using
      * the US-ASCII charset.
      *

Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/Base64InputStreamTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/Base64InputStreamTest.java?rev=1482374&r1=1482373&r2=1482374&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/Base64InputStreamTest.java
(original)
+++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/Base64InputStreamTest.java
Tue May 14 15:10:12 2013
@@ -232,10 +232,8 @@ public class Base64InputStreamTest {
     @Test
     public void testLenientUnexpectedEof() throws Exception {
         Base64InputStream decoder = create("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlI");
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        ContentUtil.copy(decoder, out);
-        Assert.assertEquals("This is the plain text message", ContentUtil.toAsciiString(
-                out.toByteArray()));
+        byte[] buf = ContentUtil.buffer(decoder);
+        Assert.assertEquals("This is the plain text message", ContentUtil.toAsciiString(buf));
     }
 
     @Test
@@ -252,10 +250,8 @@ public class Base64InputStreamTest {
     @Test
     public void testLenientUnexpectedPad() throws Exception {
         Base64InputStream decoder = create("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlI=");
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        ContentUtil.copy(decoder, out);
-        Assert.assertEquals("This is the plain text message", ContentUtil.toAsciiString(
-                out.toByteArray()));
+        byte[] buf = ContentUtil.buffer(decoder);
+        Assert.assertEquals("This is the plain text message", ContentUtil.toAsciiString(buf));
     }
 
 }

Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/EncoderUtilTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/EncoderUtilTest.java?rev=1482374&r1=1482373&r2=1482374&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/EncoderUtilTest.java
(original)
+++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/EncoderUtilTest.java
Tue May 14 15:10:12 2013
@@ -261,9 +261,8 @@ public class EncoderUtilTest {
         outb64.close();
 
         InputStream is = new Base64InputStream(InputStreams.create(out2.toByteArray()));
-        ByteArrayOutputStream outRoundtrip = new ByteArrayOutputStream();
-        ContentUtil.copy(is, outRoundtrip);
-        return new String(outRoundtrip.toByteArray());
+        byte[] buf = ContentUtil.buffer(is);
+        return ContentUtil.toAsciiString(buf);
     }
 
     /**
@@ -285,9 +284,8 @@ public class EncoderUtilTest {
         EncoderUtil.encodeB(InputStreams.createAscii(input), out);
 
         InputStream is = new Base64InputStream(InputStreams.create(out.toByteArray()));
-        ByteArrayOutputStream outRoundtrip = new ByteArrayOutputStream();
-        ContentUtil.copy(is, outRoundtrip);
-        return new String(outRoundtrip.toByteArray());
+        byte[] buf = ContentUtil.buffer(is);
+        return ContentUtil.toAsciiString(buf);
     }
 
 }

Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableEncodeTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableEncodeTest.java?rev=1482374&r1=1482373&r2=1482374&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableEncodeTest.java
(original)
+++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableEncodeTest.java
Tue May 14 15:10:12 2013
@@ -24,9 +24,9 @@ import java.io.InputStream;
 import java.nio.charset.Charset;
 import java.util.Arrays;
 
-import org.apache.commons.io.IOUtils;
 import org.apache.james.mime4j.Charsets;
 import org.apache.james.mime4j.io.InputStreams;
+import org.apache.james.mime4j.util.ContentUtil;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -115,9 +115,8 @@ public class QuotedPrintableEncodeTest {
         EncoderUtil.encodeQBinary(in, out);
         // read back through decoder
         in = new QuotedPrintableInputStream(InputStreams.create(out.toByteArray()));
-        out = new ByteArrayOutputStream();
-        IOUtils.copy(in, out);
-        assertEquals(content, out.toByteArray());
+        byte[] buf = ContentUtil.buffer(in);
+        assertEquals(content, buf);
     }
 
     private void check(byte[] content, byte[] expected) throws Exception {

Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableTextEncodeTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableTextEncodeTest.java?rev=1482374&r1=1482373&r2=1482374&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableTextEncodeTest.java
(original)
+++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableTextEncodeTest.java
Tue May 14 15:10:12 2013
@@ -24,7 +24,6 @@ import java.io.InputStream;
 import java.nio.charset.Charset;
 import java.util.Arrays;
 
-import org.apache.commons.io.IOUtils;
 import org.apache.james.mime4j.Charsets;
 import org.apache.james.mime4j.io.InputStreams;
 import org.apache.james.mime4j.util.ContentUtil;
@@ -159,9 +158,8 @@ public class QuotedPrintableTextEncodeTe
         EncoderUtil.encodeQ(in, out);
         // read back through decoder
         in = new QuotedPrintableInputStream(InputStreams.create(out.toByteArray()));
-        out = new ByteArrayOutputStream();
-        IOUtils.copy(in, out);
-        assertEquals(content, out.toByteArray());
+        byte[] buf = ContentUtil.buffer(in);
+        assertEquals(content, buf);
     }
 
     private void check(String content, String expected) throws Exception {

Modified: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/BasicBodyFactory.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/BasicBodyFactory.java?rev=1482374&r1=1482373&r2=1482374&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/BasicBodyFactory.java
(original)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/BasicBodyFactory.java
Tue May 14 15:10:12 2013
@@ -19,7 +19,6 @@
 
 package org.apache.james.mime4j.message;
 
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
@@ -36,7 +35,9 @@ import org.apache.james.mime4j.dom.TextB
 public class BasicBodyFactory implements BodyFactory {
 
     public BinaryBody binaryBody(final InputStream is) throws IOException {
-        return new BasicBinaryBody(bufferContent(is));
+        return BodyBuilder.create()
+                .readFrom(is).
+                buildBinary();
     }
 
     protected Charset resolveCharset(final String mimeCharset) throws UnsupportedEncodingException
{
@@ -48,34 +49,30 @@ public class BasicBodyFactory implements
     }
 
     public TextBody textBody(final InputStream is, final String mimeCharset) throws IOException
{
-        return new BasicTextBody(bufferContent(is), resolveCharset(mimeCharset));
-    }
-
-    private static byte[] bufferContent(final InputStream is) throws IOException {
-        if (is == null) {
-            throw new IllegalArgumentException("Input stream may not be null");
-        }
-        ByteArrayOutputStream buf = new ByteArrayOutputStream();
-        byte[] tmp = new byte[2048];
-        int l;
-        while ((l = is.read(tmp)) != -1) {
-            buf.write(tmp, 0, l);
-        }
-        return buf.toByteArray();
+        return BodyBuilder.create()
+                .readFrom(is)
+                .setCharset(resolveCharset(mimeCharset))
+                .buildText();
     }
 
     public TextBody textBody(final String text, final String mimeCharset) throws UnsupportedEncodingException
{
         if (text == null) {
             throw new IllegalArgumentException("Text may not be null");
         }
-        return new StringBody(text, resolveCharset(mimeCharset));
+        return BodyBuilder.create()
+                .setText(text)
+                .setCharset(resolveCharset(mimeCharset))
+                .buildText();
     }
 
     public TextBody textBody(final String text, final Charset charset) {
         if (text == null) {
             throw new IllegalArgumentException("Text may not be null");
         }
-        return new StringBody(text, charset);
+        return BodyBuilder.create()
+                .setText(text)
+                .setCharset(charset)
+                .buildText();
     }
 
     public TextBody textBody(final String text) {
@@ -83,7 +80,9 @@ public class BasicBodyFactory implements
     }
 
     public BinaryBody binaryBody(final byte[] buf) {
-        return new BasicBinaryBody(buf);
+        return BodyBuilder.create()
+                .setByteArray(buf)
+                .buildBinary();
     }
 
 }

Added: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/BodyBuilder.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/BodyBuilder.java?rev=1482374&view=auto
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/BodyBuilder.java
(added)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/BodyBuilder.java
Tue May 14 15:10:12 2013
@@ -0,0 +1,240 @@
+/****************************************************************
+ * 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.message;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+
+import org.apache.james.mime4j.Charsets;
+import org.apache.james.mime4j.dom.BinaryBody;
+import org.apache.james.mime4j.dom.Body;
+import org.apache.james.mime4j.dom.SingleBody;
+import org.apache.james.mime4j.dom.TextBody;
+import org.apache.james.mime4j.io.InputStreams;
+import org.apache.james.mime4j.util.ContentUtil;
+
+/**
+ * Builder for {@link TextBody} and {@link BinaryBody} instances.
+ */
+public class BodyBuilder {
+
+    public static BodyBuilder create() {
+        return new BodyBuilder();
+    }
+
+    private String text;
+    private byte[] bin;
+    private Charset charset;
+
+    BodyBuilder() {
+        super();
+    }
+
+    public BodyBuilder setText(final String text) {
+        this.text = text;
+        this.bin = null;
+        return this;
+    }
+
+    public BodyBuilder setByteArray(final byte[] bin) {
+        this.bin = bin;
+        this.text = null;
+        return this;
+    }
+
+    public BodyBuilder setCharset(final Charset charset) {
+        this.charset = charset;
+        return this;
+    }
+
+    public BodyBuilder readFrom(final InputStream in) throws IOException {
+        this.bin = ContentUtil.buffer(in);
+        this.text = null;
+        return this;
+    }
+
+    public BodyBuilder readFrom(final Reader in) throws IOException {
+        this.text = ContentUtil.buffer(in);
+        this.bin = null;
+        return this;
+    }
+
+    public TextBody buildText() {
+        Charset cs = this.charset != null ? this.charset : Charsets.DEFAULT_CHARSET;
+        if (this.text != null) {
+            return new StringBody1(this.text, cs);
+        } else if (this.bin != null) {
+            return new StringBody2(this.bin, cs);
+        } else {
+            return new StringBody2(new byte [] {}, cs);
+        }
+    }
+
+    public BinaryBody buildBinary() {
+        if (this.bin != null) {
+            return new BinaryBody1(this.bin);
+        } else if (this.text != null) {
+            return new BinaryBody2(this.text, this.charset);
+        } else {
+            return new BinaryBody1(new byte [] {});
+        }
+    }
+
+    public Body build() {
+        if (this.charset != null) {
+            return buildText();
+        } else {
+            return buildBinary();
+        }
+    }
+
+    static class StringBody1 extends TextBody {
+
+        private final String content;
+        private final Charset charset;
+
+        StringBody1(final String content, final Charset charset) {
+            super();
+            this.content = content;
+            this.charset = charset;
+        }
+
+        @Override
+        public String getMimeCharset() {
+            return this.charset != null ? this.charset.name() : null;
+        }
+
+        @Override
+        public Reader getReader() throws IOException {
+            return new StringReader(this.content);
+        }
+
+        @Override
+        public InputStream getInputStream() throws IOException {
+            return InputStreams.create(this.content,
+                    this.charset != null ? this.charset : Charsets.DEFAULT_CHARSET);
+        }
+
+        @Override
+        public void dispose() {
+        }
+
+        @Override
+        public SingleBody copy() {
+            return new StringBody1(this.content, this.charset);
+        }
+
+    }
+
+    static class StringBody2 extends TextBody {
+
+        private final byte[] content;
+        private final Charset charset;
+
+        StringBody2(final byte[] content, final Charset charset) {
+            super();
+            this.content = content;
+            this.charset = charset;
+        }
+
+        @Override
+        public String getMimeCharset() {
+            return this.charset != null ? this.charset.name() : null;
+        }
+
+        @Override
+        public Reader getReader() throws IOException {
+            return new InputStreamReader(InputStreams.create(this.content), this.charset);
+        }
+
+        @Override
+        public InputStream getInputStream() throws IOException {
+            return InputStreams.create(this.content);
+        }
+
+        @Override
+        public void dispose() {
+        }
+
+        @Override
+        public SingleBody copy() {
+            return new StringBody2(this.content, this.charset);
+        }
+
+    }
+
+    static class BinaryBody1 extends BinaryBody {
+
+        private final byte[] content;
+
+        BinaryBody1(final byte[] content) {
+            super();
+            this.content = content;
+        }
+
+        @Override
+        public InputStream getInputStream() throws IOException {
+            return InputStreams.create(this.content);
+        }
+
+        @Override
+        public void dispose() {
+        }
+
+        @Override
+        public SingleBody copy() {
+            return new BinaryBody1(this.content);
+        }
+
+    }
+
+    static class BinaryBody2 extends BinaryBody {
+
+        private final String content;
+        private final Charset charset;
+
+        BinaryBody2(final String content, final Charset charset) {
+            super();
+            this.content = content;
+            this.charset = charset;
+        }
+
+        @Override
+        public InputStream getInputStream() throws IOException {
+            return InputStreams.create(this.content,
+                    this.charset != null ? this.charset : Charsets.DEFAULT_CHARSET);
+        }
+
+        @Override
+        public void dispose() {
+        }
+
+        @Override
+        public SingleBody copy() {
+            return new BinaryBody2(this.content, this.charset);
+        }
+
+    }
+
+}

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

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

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



Mime
View raw message