-
-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #356 from vonox7/android-7-crashfix
Fix NoClassDefFoundError on Android < 8.0
- Loading branch information
Showing
6 changed files
with
54 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
modules/core-module/src/main/java/org/simplejavamail/internal/util/FileUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters