diff --git a/modules/cli-module/src/main/java/org/simplejavamail/internal/clisupport/CliSupport.java b/modules/cli-module/src/main/java/org/simplejavamail/internal/clisupport/CliSupport.java index 85e45b99..bd9b2b7f 100644 --- a/modules/cli-module/src/main/java/org/simplejavamail/internal/clisupport/CliSupport.java +++ b/modules/cli-module/src/main/java/org/simplejavamail/internal/clisupport/CliSupport.java @@ -7,7 +7,7 @@ import org.simplejavamail.api.mailer.MailerFromSessionBuilder; import org.simplejavamail.api.mailer.MailerRegularBuilder; import org.simplejavamail.internal.clisupport.serialization.SerializationUtil; -import org.simplejavamail.internal.util.MiscUtil; +import org.simplejavamail.internal.util.FileUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import picocli.CommandLine; @@ -38,9 +38,9 @@ private static List produceCliDeclaredOptionSpec() { if (!CLI_DATAFILE.exists()) { LOGGER.info("Initial cli.data not found, writing to (one time action): {}", CLI_DATAFILE); List declaredOptions = generateOptionsFromBuilderApi(RELEVANT_BUILDER_ROOT_API); - MiscUtil.writeFileBytes(CLI_DATAFILE, SerializationUtil.serialize(declaredOptions)); + FileUtil.writeFileBytes(CLI_DATAFILE, SerializationUtil.serialize(declaredOptions)); } - return SerializationUtil.deserialize(MiscUtil.readFileBytes(CLI_DATAFILE)); + return SerializationUtil.deserialize(FileUtil.readFileBytes(CLI_DATAFILE)); } catch (IOException e) { throw new CliExecutionException(ERROR_INVOKING_BUILDER_API, e); } diff --git a/modules/cli-module/src/main/java/org/simplejavamail/internal/clisupport/therapijavadoc/TherapiJavadocHelper.java b/modules/cli-module/src/main/java/org/simplejavamail/internal/clisupport/therapijavadoc/TherapiJavadocHelper.java index e3d3c84f..84aadafd 100644 --- a/modules/cli-module/src/main/java/org/simplejavamail/internal/clisupport/therapijavadoc/TherapiJavadocHelper.java +++ b/modules/cli-module/src/main/java/org/simplejavamail/internal/clisupport/therapijavadoc/TherapiJavadocHelper.java @@ -17,7 +17,7 @@ import org.jetbrains.annotations.TestOnly; import org.simplejavamail.internal.clisupport.CliDataLocator; import org.simplejavamail.internal.clisupport.serialization.SerializationUtil; -import org.simplejavamail.internal.util.MiscUtil; +import org.simplejavamail.internal.util.FileUtil; import java.io.File; import java.io.IOException; @@ -48,7 +48,7 @@ public final class TherapiJavadocHelper { private static Map loadTherapiCache() { if (THERAPI_DATAFILE.exists()) { try { - return SerializationUtil.deserialize(MiscUtil.readFileBytes(THERAPI_DATAFILE)); + return SerializationUtil.deserialize(FileUtil.readFileBytes(THERAPI_DATAFILE)); } catch (IOException e) { throw new RuntimeException(e); } @@ -60,7 +60,7 @@ private static Map loadTherapiCache() { @TestOnly public static void persistCache() { try { - MiscUtil.writeFileBytes(THERAPI_DATAFILE, SerializationUtil.serialize(THERAPI_CACHE)); + FileUtil.writeFileBytes(THERAPI_DATAFILE, SerializationUtil.serialize(THERAPI_CACHE)); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/modules/core-module/src/main/java/org/simplejavamail/internal/util/FileUtil.java b/modules/core-module/src/main/java/org/simplejavamail/internal/util/FileUtil.java new file mode 100644 index 00000000..cfa064fc --- /dev/null +++ b/modules/core-module/src/main/java/org/simplejavamail/internal/util/FileUtil.java @@ -0,0 +1,36 @@ +package org.simplejavamail.internal.util; + +import org.jetbrains.annotations.NotNull; + +import java.io.File; +import java.io.IOException; +import java.nio.file.FileAlreadyExistsException; +import java.nio.file.Files; + +import static java.lang.String.format; +import static java.nio.charset.StandardCharsets.UTF_8; + +// FileUtil uses java.nio.file, which is not available on Android < 8.0 and will throw there a NoClassDefFoundError. +// See https://developer.android.com/reference/java/nio/file/package-summary. +// Therefore, keep imports of java.nio.file.* inside this class. +public class FileUtil { + public static String readFileContent(@NotNull final File file) throws IOException { + return new String(readFileBytes(file), UTF_8); + } + + public static byte[] readFileBytes(@NotNull final File file) throws IOException { + if (!file.exists()) { + throw new IllegalArgumentException(format("File not found: %s", file)); + } + return Files.readAllBytes(file.toPath()); + } + + public static void writeFileBytes(@NotNull final File file, final byte[] bytes) throws IOException { + try { + Files.createFile(file.toPath()); + } catch (FileAlreadyExistsException e) { + // ignore + } + Files.write(file.toPath(), bytes); + } +} diff --git a/modules/core-module/src/main/java/org/simplejavamail/internal/util/MiscUtil.java b/modules/core-module/src/main/java/org/simplejavamail/internal/util/MiscUtil.java index f70efd2e..6cd37319 100644 --- a/modules/core-module/src/main/java/org/simplejavamail/internal/util/MiscUtil.java +++ b/modules/core-module/src/main/java/org/simplejavamail/internal/util/MiscUtil.java @@ -23,8 +23,6 @@ import java.lang.reflect.Method; import java.net.URL; import java.nio.charset.Charset; -import java.nio.file.FileAlreadyExistsException; -import java.nio.file.Files; import java.util.AbstractMap; import java.util.ArrayList; import java.util.Collection; @@ -227,26 +225,6 @@ private static boolean containsNullableAnnotation(final Annotation[] annotations return false; } - public static String readFileContent(@NotNull final File file) throws IOException { - return new String(readFileBytes(file), UTF_8); - } - - public static byte[] readFileBytes(@NotNull final File file) throws IOException { - if (!file.exists()) { - throw new IllegalArgumentException(format("File not found: %s", file)); - } - return Files.readAllBytes(file.toPath()); - } - - public static void writeFileBytes(@NotNull final File file, final byte[] bytes) throws IOException { - try { - Files.createFile(file.toPath()); - } catch (FileAlreadyExistsException e) { - // ignore - } - Files.write(file.toPath(), bytes); - } - @Nullable public static DataSource tryResolveImageFileDataSourceFromDisk(final @Nullable String baseDir, final boolean allowOutsideBaseDir, final @NotNull String srcLocation) { DataSource dataSource; diff --git a/modules/simple-java-mail/src/main/java/org/simplejavamail/email/internal/EmailPopulatingBuilderImpl.java b/modules/simple-java-mail/src/main/java/org/simplejavamail/email/internal/EmailPopulatingBuilderImpl.java index 7de9420e..b33f7582 100644 --- a/modules/simple-java-mail/src/main/java/org/simplejavamail/email/internal/EmailPopulatingBuilderImpl.java +++ b/modules/simple-java-mail/src/main/java/org/simplejavamail/email/internal/EmailPopulatingBuilderImpl.java @@ -16,6 +16,7 @@ import org.simplejavamail.api.mailer.config.Pkcs12Config; import org.simplejavamail.email.EmailBuilder; import org.simplejavamail.internal.util.CertificationUtil; +import org.simplejavamail.internal.util.FileUtil; import org.simplejavamail.internal.util.MiscUtil; import org.simplejavamail.internal.util.NamedDataSource; @@ -642,7 +643,7 @@ public InternalEmailPopulatingBuilder withForward(@Nullable final MimeMessage em @Cli.OptionNameOverride("withPlainTextFromFile") public EmailPopulatingBuilder withPlainText(@NotNull final File textFile) { try { - return withPlainText(MiscUtil.readFileContent(textFile)); + return withPlainText(FileUtil.readFileContent(textFile)); } catch (IOException e) { throw new EmailException(format(ERROR_READING_FROM_FILE, textFile), e); } @@ -664,7 +665,7 @@ public EmailPopulatingBuilder withPlainText(@Nullable final String text) { @Cli.OptionNameOverride("prependTextFromFile") public EmailPopulatingBuilder prependText(@NotNull final File textFile) { try { - return prependText(MiscUtil.readFileContent(textFile)); + return prependText(FileUtil.readFileContent(textFile)); } catch (IOException e) { throw new EmailException(format(ERROR_READING_FROM_FILE, textFile), e); } @@ -686,7 +687,7 @@ public EmailPopulatingBuilder prependText(@NotNull final String text) { @Cli.OptionNameOverride("appendTextFromFile") public EmailPopulatingBuilder appendText(@NotNull final File textFile) { try { - return appendText(MiscUtil.readFileContent(textFile)); + return appendText(FileUtil.readFileContent(textFile)); } catch (IOException e) { throw new EmailException(format(ERROR_READING_FROM_FILE, textFile), e); } @@ -708,7 +709,7 @@ public EmailPopulatingBuilder appendText(@NotNull final String text) { @Cli.OptionNameOverride("withHTMLTextFromFile") public EmailPopulatingBuilder withHTMLText(@NotNull final File textHTMLFile) { try { - return withHTMLText(MiscUtil.readFileContent(textHTMLFile)); + return withHTMLText(FileUtil.readFileContent(textHTMLFile)); } catch (IOException e) { throw new EmailException(format(ERROR_READING_FROM_FILE, textHTMLFile), e); } @@ -730,7 +731,7 @@ public EmailPopulatingBuilder withHTMLText(@Nullable final String textHTML) { @Cli.OptionNameOverride("prependTextHTMLFromFile") public EmailPopulatingBuilder prependTextHTML(@NotNull final File textHTMLFile) { try { - return prependTextHTML(MiscUtil.readFileContent(textHTMLFile)); + return prependTextHTML(FileUtil.readFileContent(textHTMLFile)); } catch (IOException e) { throw new EmailException(format(ERROR_READING_FROM_FILE, textHTMLFile), e); } @@ -752,7 +753,7 @@ public EmailPopulatingBuilder prependTextHTML(@NotNull final String textHTML) { @Cli.OptionNameOverride("appendTextHTMLFromFile") public EmailPopulatingBuilder appendTextHTML(@NotNull final File textHTMLFile) { try { - return appendTextHTML(MiscUtil.readFileContent(textHTMLFile)); + return appendTextHTML(FileUtil.readFileContent(textHTMLFile)); } catch (IOException e) { throw new EmailException(format(ERROR_READING_FROM_FILE, textHTMLFile), e); } diff --git a/modules/simple-java-mail/src/test/java/org/simplejavamail/internal/util/MiscUtilTest.java b/modules/simple-java-mail/src/test/java/org/simplejavamail/internal/util/MiscUtilTest.java index a10fe769..0b90986c 100644 --- a/modules/simple-java-mail/src/test/java/org/simplejavamail/internal/util/MiscUtilTest.java +++ b/modules/simple-java-mail/src/test/java/org/simplejavamail/internal/util/MiscUtilTest.java @@ -162,22 +162,22 @@ public void testCountMandatoryParameters() { @Test public void testReadFileContent() throws IOException { - assertThatThrownBy(() -> MiscUtil.readFileContent(new File("moo"))) + assertThatThrownBy(() -> FileUtil.readFileContent(new File("moo"))) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("File not found: moo"); - assertThat(MiscUtil.readFileContent(new File("src/test/resources/ignore.properties"))) + assertThat(FileUtil.readFileContent(new File("src/test/resources/ignore.properties"))) .contains("simplejavamail.defaults.bcc.address=moo"); } @Test public void testWriteFileContent() throws IOException { - MiscUtil.writeFileBytes(new File("target/test.file"), "This is a test".getBytes()); + FileUtil.writeFileBytes(new File("target/test.file"), "This is a test".getBytes()); - assertThat(MiscUtil.readFileBytes(new File("target/test.file"))) + assertThat(FileUtil.readFileBytes(new File("target/test.file"))) .isEqualTo("This is a test".getBytes()); - assertThat(MiscUtil.readFileContent(new File("target/test.file"))) + assertThat(FileUtil.readFileContent(new File("target/test.file"))) .isEqualTo("This is a test"); }