You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/06/09 13:19:03 UTC
[commons-email] 04/06: Use try-with-resources
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 29cce9a570322340d97cd95dd95d8a8e9d19057c
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Jun 9 09:06:56 2022 -0400
Use try-with-resources
---
.../mail/resolver/DataSourceClassPathResolver.java | 21 ++++++-----------
.../apache/commons/mail/util/MimeMessageUtils.java | 27 +++++-----------------
2 files changed, 13 insertions(+), 35 deletions(-)
diff --git a/src/main/java/org/apache/commons/mail/resolver/DataSourceClassPathResolver.java b/src/main/java/org/apache/commons/mail/resolver/DataSourceClassPathResolver.java
index a53e051..4c34ef4 100644
--- a/src/main/java/org/apache/commons/mail/resolver/DataSourceClassPathResolver.java
+++ b/src/main/java/org/apache/commons/mail/resolver/DataSourceClassPathResolver.java
@@ -90,27 +90,20 @@ public class DataSourceClassPathResolver extends DataSourceBaseResolver
{
final String mimeType = FileTypeMap.getDefaultFileTypeMap().getContentType(resourceLocation);
final String resourceName = getResourceName(resourceLocation);
- final InputStream is = DataSourceClassPathResolver.class.getResourceAsStream(resourceName);
-
- if (is == null) {
- if (isLenient)
- {
- return null;
+ try (InputStream is = DataSourceClassPathResolver.class.getResourceAsStream(resourceName)){
+ if (is == null) {
+ if (isLenient)
+ {
+ return null;
+ }
+ throw new IOException("The following class path resource was not found : " + resourceLocation);
}
- throw new IOException("The following class path resource was not found : " + resourceLocation);
- }
- try
- {
final ByteArrayDataSource ds = new ByteArrayDataSource(is, mimeType);
// EMAIL-125: set the name of the DataSource to the normalized resource URL
// similar to other DataSource implementations, e.g. FileDataSource, URLDataSource
ds.setName(DataSourceClassPathResolver.class.getResource(resourceName).toString());
result = ds;
}
- finally
- {
- is.close();
- }
}
diff --git a/src/main/java/org/apache/commons/mail/util/MimeMessageUtils.java b/src/main/java/org/apache/commons/mail/util/MimeMessageUtils.java
index 40cbf53..1cfa4c0 100644
--- a/src/main/java/org/apache/commons/mail/util/MimeMessageUtils.java
+++ b/src/main/java/org/apache/commons/mail/util/MimeMessageUtils.java
@@ -131,30 +131,15 @@ public final class MimeMessageUtils
public static void writeMimeMessage(final MimeMessage mimeMessage, final File resultFile)
throws MessagingException, IOException
{
-
- FileOutputStream fos = null;
-
- try
+ if (!resultFile.getParentFile().exists() && !resultFile.getParentFile().mkdirs())
{
- if (!resultFile.getParentFile().exists() && !resultFile.getParentFile().mkdirs())
- {
- throw new IOException(
- "Failed to create the following parent directories: "
- + resultFile.getParentFile());
- }
-
- fos = new FileOutputStream(resultFile);
+ throw new IOException(
+ "Failed to create the following parent directories: "
+ + resultFile.getParentFile());
+ }
+ try (FileOutputStream fos = new FileOutputStream(resultFile)) {
mimeMessage.writeTo(fos);
fos.flush();
- fos.close();
- fos = null;
- }
- finally
- {
- if (fos != null)
- {
- fos.close();
- }
}
}
}