james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1427827 - in /james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message: BasicBodyFactory.java BasicTextBody.java StringBody.java
Date Wed, 02 Jan 2013 15:38:20 GMT
Author: olegk
Date: Wed Jan  2 15:38:19 2013
New Revision: 1427827

URL: http://svn.apache.org/viewvc?rev=1427827&view=rev
Log:
MIME4J-218: made it easier to map a charset name to Charset implementation (added BasicBodyFactory#resolveCharset
method)

Modified:
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/BasicBodyFactory.java
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/BasicTextBody.java
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/StringBody.java

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=1427827&r1=1427826&r2=1427827&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
Wed Jan  2 15:38:19 2013
@@ -24,7 +24,6 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
 import java.nio.charset.Charset;
-import java.nio.charset.UnsupportedCharsetException;
 
 import org.apache.james.mime4j.dom.BinaryBody;
 import org.apache.james.mime4j.dom.TextBody;
@@ -39,8 +38,12 @@ public class BasicBodyFactory implements
         return new BasicBinaryBody(bufferContent(is));
     }
 
+    protected Charset resolveCharset(final String mimeCharset) {
+        return mimeCharset != null ? Charset.forName(mimeCharset) : null;
+    }
+
     public TextBody textBody(final InputStream is, final String mimeCharset) throws IOException
{
-        return new BasicTextBody(bufferContent(is), mimeCharset);
+        return new BasicTextBody(bufferContent(is), resolveCharset(mimeCharset));
     }
 
     private static byte[] bufferContent(final InputStream is) throws IOException {
@@ -60,12 +63,7 @@ public class BasicBodyFactory implements
         if (text == null) {
             throw new IllegalArgumentException("Text may not be null");
         }
-        Charset charset = Charset.forName(mimeCharset);
-        try {
-            return new StringBody(text, charset);
-        } catch (UnsupportedCharsetException ex) {
-            throw new UnsupportedEncodingException(ex.getMessage());
-        }
+        return new StringBody(text, resolveCharset(mimeCharset));
     }
 
     public TextBody textBody(final String text, final Charset charset) {

Modified: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/BasicTextBody.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/BasicTextBody.java?rev=1427827&r1=1427826&r2=1427827&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/BasicTextBody.java
(original)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/BasicTextBody.java
Wed Jan  2 15:38:19 2013
@@ -24,16 +24,18 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
+import java.nio.charset.Charset;
 
 import org.apache.james.mime4j.dom.SingleBody;
 import org.apache.james.mime4j.dom.TextBody;
+import org.apache.james.mime4j.util.CharsetUtil;
 
 class BasicTextBody extends TextBody {
 
     private final byte[] content;
-    private final String charset;
+    private final Charset charset;
 
-    BasicTextBody(final byte[] content, final String charset) {
+    BasicTextBody(final byte[] content, final Charset charset) {
         super();
         this.content = content;
         this.charset = charset;
@@ -41,12 +43,13 @@ class BasicTextBody extends TextBody {
 
     @Override
     public String getMimeCharset() {
-        return this.charset;
+        return this.charset != null ? this.charset.name() : null;
     }
 
     @Override
     public Reader getReader() throws IOException {
-        return new InputStreamReader(getInputStream(), this.charset);
+        return new InputStreamReader(getInputStream(),
+                this.charset != null ? this.charset : CharsetUtil.DEFAULT_CHARSET);
     }
 
     @Override

Modified: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/StringBody.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/StringBody.java?rev=1427827&r1=1427826&r2=1427827&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/StringBody.java (original)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/StringBody.java Wed
Jan  2 15:38:19 2013
@@ -27,6 +27,7 @@ import java.nio.charset.Charset;
 
 import org.apache.james.mime4j.dom.SingleBody;
 import org.apache.james.mime4j.dom.TextBody;
+import org.apache.james.mime4j.util.CharsetUtil;
 
 class StringBody extends TextBody {
 
@@ -41,7 +42,7 @@ class StringBody extends TextBody {
 
     @Override
     public String getMimeCharset() {
-        return this.charset.name();
+        return this.charset != null ? this.charset.name() : null;
     }
 
     @Override
@@ -51,7 +52,8 @@ class StringBody extends TextBody {
 
     @Override
     public InputStream getInputStream() throws IOException {
-        return new StringInputStream(this.content, this.charset, 2048);
+        return new StringInputStream(this.content,
+                this.charset != null ? this.charset : CharsetUtil.DEFAULT_CHARSET, 2048);
     }
 
     @Override



Mime
View raw message