This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-email.git
commit 33dc4249c944a917c7b2375dd80a4268f7ca85cc Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sun Dec 10 15:26:09 2023 -0500 [EMAIL-207] Don't waste memory in MimeMessageParser#createDataSource() --- src/changes/changes.xml | 1 + .../java/org/apache/commons/mail/EmailUtils.java | 2 +- .../commons/mail/util/MimeMessageParser.java | 44 +++------------------- 3 files changed, 7 insertions(+), 40 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 2d671f6..c2b5233 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -54,6 +54,7 @@ </action> <action issue="EMAIL-176" type="add" dev="pschumacher">Add Automatic-Module-Name MANIFEST entry for Java 9 compatibility.</action> <action issue="EMAIL-207" type="add" dev="ggregory" due-to="Lee Jaeheon, Gary Gregory">Add org.apache.commons.mail.InputStreamDataSource.</action> + <action issue="EMAIL-207" type="add" dev="ggregory" due-to="Lee Jaeheon, Gary Gregory">Don't waste memory in MimeMessageParser#createDataSource().</action> <!-- UPDATE --> <action type="update" due-to="Dependabot, Gary Gregory" dev="ggregory"> Bump actions/cache from 2 to 3.0.11 #39, #48, #60, #70, #102. diff --git a/src/main/java/org/apache/commons/mail/EmailUtils.java b/src/main/java/org/apache/commons/mail/EmailUtils.java index 72cd503..fb0033f 100644 --- a/src/main/java/org/apache/commons/mail/EmailUtils.java +++ b/src/main/java/org/apache/commons/mail/EmailUtils.java @@ -269,7 +269,7 @@ final class EmailUtils final StringBuilder builder = new StringBuilder(); for (final byte c : input.getBytes(StandardCharsets.US_ASCII)) { - int b = c & 0xff; + final int b = c & 0xff; if (SAFE_URL.get(b)) { builder.append((char) b); diff --git a/src/main/java/org/apache/commons/mail/util/MimeMessageParser.java b/src/main/java/org/apache/commons/mail/util/MimeMessageParser.java index aef90df..ebcceac 100644 --- a/src/main/java/org/apache/commons/mail/util/MimeMessageParser.java +++ b/src/main/java/org/apache/commons/mail/util/MimeMessageParser.java @@ -16,11 +16,7 @@ */ package org.apache.commons.mail.util; -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Arrays; @@ -30,7 +26,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import javax.activation.DataHandler; import javax.activation.DataSource; import javax.mail.Address; import javax.mail.Message; @@ -44,7 +39,8 @@ import javax.mail.internet.MimeMessage; import javax.mail.internet.MimePart; import javax.mail.internet.MimeUtility; import javax.mail.internet.ParseException; -import javax.mail.util.ByteArrayDataSource; + +import org.apache.commons.mail.InputStreamDataSource; /** * Parses a MimeMessage and stores the individual parts such a plain text, @@ -264,21 +260,14 @@ public class MimeMessageParser * @throws MessagingException creating the DataSource failed * @throws IOException error getting InputStream or unsupported encoding */ + @SuppressWarnings("resource") // Caller closes InputStream protected DataSource createDataSource(final Multipart parent, final MimePart part) throws MessagingException, IOException { - final DataHandler dataHandler = part.getDataHandler(); - final DataSource dataSource = dataHandler.getDataSource(); + final DataSource dataSource = part.getDataHandler().getDataSource(); final String contentType = getBaseMimeType(dataSource.getContentType()); - byte[] content; - try (InputStream inputStream = dataSource.getInputStream()) - { - content = this.getContent(inputStream); - } - final ByteArrayDataSource result = new ByteArrayDataSource(content, contentType); final String dataSourceName = getDataSourceName(part, dataSource); - result.setName(dataSourceName); - return result; + return new InputStreamDataSource(dataSource.getInputStream(), contentType, dataSourceName); } /** @return Returns the mimeMessage. */ @@ -410,29 +399,6 @@ public class MimeMessageParser return result; } - /** - * Read the content of the input stream. - * - * @param is the input stream to process - * @return the content of the input stream - * @throws IOException reading the input stream failed - */ - private byte[] getContent(final InputStream is) - throws IOException - { - final ByteArrayOutputStream os = new ByteArrayOutputStream(); - final BufferedInputStream isReader = new BufferedInputStream(is); - try (BufferedOutputStream osWriter = new BufferedOutputStream(os)) { - int ch; - while ((ch = isReader.read()) != -1) - { - osWriter.write(ch); - } - osWriter.flush(); - return os.toByteArray(); - } - } - /** * Parses the mimeType. *