This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-mime4j.git
commit 432b786a4b7ca565b3d3b9c94640f3d03c8f58eb Author: Benoit Tellier <btell...@linagora.com> AuthorDate: Wed Jun 29 08:23:08 2022 +0700 MIME4J-318 Add a TextBody::getCharset This avoids client application to re-parse the charset --- .../main/java/org/apache/james/mime4j/dom/TextBody.java | 3 +++ .../org/apache/james/mime4j/message/BasicBodyFactory.java | 15 +++++++++++++++ .../apache/james/mime4j/message/SingleBodyBuilder.java | 11 +---------- .../org/apache/james/mime4j/storage/StorageTextBody.java | 5 +++++ .../org/apache/james/mime4j/storage/StringTextBody.java | 5 +++++ 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/dom/src/main/java/org/apache/james/mime4j/dom/TextBody.java b/dom/src/main/java/org/apache/james/mime4j/dom/TextBody.java index 9929199d..5b49f8c8 100644 --- a/dom/src/main/java/org/apache/james/mime4j/dom/TextBody.java +++ b/dom/src/main/java/org/apache/james/mime4j/dom/TextBody.java @@ -21,6 +21,7 @@ package org.apache.james.mime4j.dom; import java.io.IOException; import java.io.Reader; +import java.nio.charset.Charset; /** * Encapsulates the contents of a <code>text/*</code> entity body. @@ -40,6 +41,8 @@ public abstract class TextBody extends SingleBody { */ public abstract String getMimeCharset(); + public abstract Charset getCharset(); + /** * Gets a <code>Reader</code> which may be used to read out the contents * of this body. diff --git a/dom/src/main/java/org/apache/james/mime4j/message/BasicBodyFactory.java b/dom/src/main/java/org/apache/james/mime4j/message/BasicBodyFactory.java index 10df514b..225ec295 100644 --- a/dom/src/main/java/org/apache/james/mime4j/message/BasicBodyFactory.java +++ b/dom/src/main/java/org/apache/james/mime4j/message/BasicBodyFactory.java @@ -162,6 +162,11 @@ public class BasicBodyFactory implements BodyFactory { return this.charset != null ? this.charset.name() : null; } + @Override + public Charset getCharset() { + return charset; + } + @Override public Reader getReader() throws IOException { return new StringReader(this.content); @@ -200,6 +205,11 @@ public class BasicBodyFactory implements BodyFactory { return this.charset != null ? this.charset.name() : null; } + @Override + public Charset getCharset() { + return charset; + } + @Override public Reader getReader() throws IOException { return new InputStreamReader(InputStreams.create(this.content), this.charset); @@ -237,6 +247,11 @@ public class BasicBodyFactory implements BodyFactory { return this.charset != null ? this.charset.name() : null; } + @Override + public Charset getCharset() { + return charset; + } + @Override public Reader getReader() throws IOException { return new InputStreamReader(this.content.getValue().toInputStream(), this.charset); diff --git a/dom/src/main/java/org/apache/james/mime4j/message/SingleBodyBuilder.java b/dom/src/main/java/org/apache/james/mime4j/message/SingleBodyBuilder.java index 66d99caf..82f192a2 100644 --- a/dom/src/main/java/org/apache/james/mime4j/message/SingleBodyBuilder.java +++ b/dom/src/main/java/org/apache/james/mime4j/message/SingleBodyBuilder.java @@ -96,16 +96,7 @@ public class SingleBodyBuilder { return this; } if (other instanceof TextBody) { - String charsetName = ((TextBody) other).getMimeCharset(); - if (charsetName != null) { - try { - this.charset = Charset.forName(charsetName); - } catch (IllegalCharsetNameException ex) { - throw new UnsupportedEncodingException(charsetName); - } catch (UnsupportedCharsetException ex) { - throw new UnsupportedEncodingException(charsetName); - } - } + this.charset = ((TextBody) other).getCharset(); } this.bin = ContentUtil.buffer(other.getInputStream()); return this; diff --git a/storage/src/main/java/org/apache/james/mime4j/storage/StorageTextBody.java b/storage/src/main/java/org/apache/james/mime4j/storage/StorageTextBody.java index 87bd33b1..85486ef2 100644 --- a/storage/src/main/java/org/apache/james/mime4j/storage/StorageTextBody.java +++ b/storage/src/main/java/org/apache/james/mime4j/storage/StorageTextBody.java @@ -45,6 +45,11 @@ class StorageTextBody extends TextBody { return charset.name(); } + @Override + public Charset getCharset() { + return charset; + } + @Override public Reader getReader() throws IOException { return new InputStreamReader(storage.getInputStream(), charset); diff --git a/storage/src/main/java/org/apache/james/mime4j/storage/StringTextBody.java b/storage/src/main/java/org/apache/james/mime4j/storage/StringTextBody.java index ed5b31d3..8340d87b 100644 --- a/storage/src/main/java/org/apache/james/mime4j/storage/StringTextBody.java +++ b/storage/src/main/java/org/apache/james/mime4j/storage/StringTextBody.java @@ -49,6 +49,11 @@ class StringTextBody extends TextBody { return charset.name(); } + @Override + public Charset getCharset() { + return charset; + } + @Override public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(text.getBytes(charset.name())); --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org