diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/FileUtils.java b/smack-core/src/main/java/org/jivesoftware/smack/util/FileUtils.java index 406eebeda..a470a6005 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/FileUtils.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/FileUtils.java @@ -207,4 +207,28 @@ public final class FileUtils { throw new IOException("Could not delete file " + file); } } + + public static void maybeCreateFileWithParentDirectories(File file) throws IOException { + File parent = file.getParentFile(); + if (!parent.exists() && !parent.mkdirs()) { + throw new IOException("Cannot create directory " + parent); + } + + if (file.isFile()) { + return; + } + + if (!file.exists()) { + if (file.createNewFile()) { + return; + } + throw new IOException("Cannot create file " + file); + } + + if (file.isDirectory()) { + throw new IOException("File " + file + " exists, but is a directory."); + } else { + throw new IOException("File " + file + " exists, but is neither a file nor a directory"); + } + } } diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpMetadataStore.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpMetadataStore.java index 25bba77bc..79144b09b 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpMetadataStore.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpMetadataStore.java @@ -120,26 +120,12 @@ public class FileBasedOpenPgpMetadataStore extends AbstractOpenPgpMetadataStore static void writeFingerprintsAndDates(Map data, File destination) throws IOException { - if (data == null || data.isEmpty()) { FileUtils.maybeDeleteFileOrThrow(destination); return; } - if (!destination.exists()) { - File parent = destination.getParentFile(); - if (!parent.exists() && !parent.mkdirs()) { - throw new IOException("Cannot create directory " + parent.getAbsolutePath()); - } - - if (!destination.createNewFile()) { - throw new IOException("Cannot create file " + destination.getAbsolutePath()); - } - } - - if (destination.isDirectory()) { - throw new IOException("File " + destination.getAbsolutePath() + " is a directory."); - } + FileUtils.maybeCreateFileWithParentDirectories(destination); BufferedWriter writer = null; try { diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpTrustStore.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpTrustStore.java index 3eb298721..38c56e95e 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpTrustStore.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpTrustStore.java @@ -102,19 +102,7 @@ public class FileBasedOpenPgpTrustStore extends AbstractOpenPgpTrustStore { FileUtils.maybeDeleteFileOrThrow(file); } - File parent = file.getParentFile(); - if (!parent.exists() && !parent.mkdirs()) { - throw new IOException("Cannot create directory " + parent.getAbsolutePath()); - } - if (!file.exists()) { - if (!file.createNewFile()) { - throw new IOException("Cannot create file " + file.getAbsolutePath()); - } - } else { - if (file.isDirectory()) { - throw new IOException("File " + file.getAbsolutePath() + " is a directory."); - } - } + FileUtils.maybeCreateFileWithParentDirectories(file); BufferedWriter writer = null; try {