Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EMAIL-204: Disable eager attachment loading on MimeMessageParser #135

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 5 additions & 49 deletions src/main/java/org/apache/commons/mail/util/MimeMessageParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,17 @@
*/
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;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.internet.ContentType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimePart;
import javax.mail.internet.MimeUtility;
import javax.mail.internet.ParseException;
import javax.mail.internet.*;
HiuKwok marked this conversation as resolved.
Show resolved Hide resolved
import javax.mail.util.ByteArrayDataSource;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;

/**
* Parses a MimeMessage and stores the individual parts such a plain text,
Expand Down Expand Up @@ -270,12 +253,7 @@ protected DataSource createDataSource(final Multipart parent, final MimePart par
final DataHandler dataHandler = part.getDataHandler();
final DataSource dataSource = dataHandler.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 ByteArrayDataSource result = new ByteArrayDataSource(dataSource.getInputStream(), contentType);
final String dataSourceName = getDataSourceName(part, dataSource);
result.setName(dataSourceName);
return result;
Expand Down Expand Up @@ -411,28 +389,6 @@ protected String getDataSourceName(final Part part, final DataSource dataSource)
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.
Expand Down