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

Improve for support for Android < 8.0 (Fix NoClassDefFoundError) #356

Merged
merged 1 commit into from
Dec 24, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,9 +38,9 @@ private static List<CliDeclaredOptionSpec> produceCliDeclaredOptionSpec() {
if (!CLI_DATAFILE.exists()) {
LOGGER.info("Initial cli.data not found, writing to (one time action): {}", CLI_DATAFILE);
List<CliDeclaredOptionSpec> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,7 +48,7 @@ public final class TherapiJavadocHelper {
private static Map<String, MethodJavadoc> 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);
}
Expand All @@ -60,7 +60,7 @@ private static Map<String, MethodJavadoc> 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down