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 23312acd1..406eebeda 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 @@ -196,4 +196,15 @@ public final class FileUtils { throw new FileNotFoundException("File " + file.getAbsolutePath() + " not found."); } } + + public static void maybeDeleteFileOrThrow(File file) throws IOException { + if (!file.exists()) { + return; + } + + boolean successfullyDeleted = file.delete(); + if (!successfullyDeleted) { + throw new IOException("Could not delete file " + file); + } + } } diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpKeyStore.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpKeyStore.java index 277ca3986..3935dd44a 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpKeyStore.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpKeyStore.java @@ -18,7 +18,6 @@ package org.jivesoftware.smackx.ox.store.filebased; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; import java.util.Date; @@ -68,12 +67,7 @@ public class FileBasedOpenPgpKeyStore extends AbstractOpenPgpKeyStore { File file = getPublicKeyRingPath(owner); if (publicKeys == null) { - if (!file.exists()) { - return; - } - if (!file.delete()) { - throw new IOException("Could not delete file " + file.getAbsolutePath()); - } + FileUtils.maybeDeleteFileOrThrow(file); return; } @@ -91,12 +85,7 @@ public class FileBasedOpenPgpKeyStore extends AbstractOpenPgpKeyStore { File file = getSecretKeyRingPath(owner); if (secretKeys == null) { - if (!file.exists()) { - return; - } - if (!file.delete()) { - throw new IOException("Could not delete file " + file.getAbsolutePath()); - } + FileUtils.maybeDeleteFileOrThrow(file); return; } 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 dc2943c1f..25bba77bc 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 @@ -122,12 +122,7 @@ public class FileBasedOpenPgpMetadataStore extends AbstractOpenPgpMetadataStore throws IOException { if (data == null || data.isEmpty()) { - if (!destination.exists()) { - return; - } - if (!destination.delete()) { - throw new IOException("Cannot delete file " + destination.getAbsolutePath()); - } + FileUtils.maybeDeleteFileOrThrow(destination); return; } 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 22d8f0870..3eb298721 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 @@ -99,12 +99,7 @@ public class FileBasedOpenPgpTrustStore extends AbstractOpenPgpTrustStore { File file = getTrustPath(owner, fingerprint); if (trust == null || trust == Trust.undecided) { - if (!file.exists()) { - return; - } - if (!file.delete()) { - throw new IOException("Could not delete file " + file.getAbsolutePath()); - } + FileUtils.maybeDeleteFileOrThrow(file); } File parent = file.getParentFile();