From 3e65cb31c3440c7a1c3764c531cd59e0abc88b9c Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 15 Aug 2018 17:36:29 +0200 Subject: [PATCH] Consolidate FileUtils from smack-openpgp into smack-core --- .../jivesoftware/smack/util/FileUtils.java | 36 +++++++++++ .../filebased/FileBasedOpenPgpKeyStore.java | 12 ++-- .../FileBasedOpenPgpMetadataStore.java | 2 +- .../filebased/FileBasedOpenPgpTrustStore.java | 2 +- .../smackx/ox/util/FileUtils.java | 60 ------------------- 5 files changed, 43 insertions(+), 69 deletions(-) delete mode 100644 smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/util/FileUtils.java 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 ac7d82da6..23312acd1 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 @@ -18,7 +18,9 @@ package org.jivesoftware.smack.util; import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; @@ -160,4 +162,38 @@ public final class FileUtils { return false; } } + + public static FileOutputStream prepareFileOutputStream(File file) throws IOException { + if (!file.exists()) { + + // Create parent directory + File parent = file.getParentFile(); + if (!parent.exists() && !parent.mkdirs()) { + throw new IOException("Cannot create directory " + parent.getAbsolutePath()); + } + + // Create file + if (!file.createNewFile()) { + throw new IOException("Cannot create file " + file.getAbsolutePath()); + } + } + + if (file.isDirectory()) { + throw new AssertionError("File " + file.getAbsolutePath() + " is not a file!"); + } + + return new FileOutputStream(file); + } + + public static FileInputStream prepareFileInputStream(File file) throws IOException { + if (file.exists()) { + if (file.isFile()) { + return new FileInputStream(file); + } else { + throw new IOException("File " + file.getAbsolutePath() + " is not a file!"); + } + } else { + throw new FileNotFoundException("File " + file.getAbsolutePath() + " not found."); + } + } } 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 4dc8da07d..f516d2011 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 @@ -16,9 +16,6 @@ */ package org.jivesoftware.smackx.ox.store.filebased; -import static org.jivesoftware.smackx.ox.util.FileUtils.prepareFileInputStream; -import static org.jivesoftware.smackx.ox.util.FileUtils.prepareFileOutputStream; - import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -28,6 +25,7 @@ import java.util.Date; import java.util.Map; import org.jivesoftware.smack.util.CloseableUtil; +import org.jivesoftware.smack.util.FileUtils; import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpKeyStore; import org.jivesoftware.smackx.ox.store.definition.OpenPgpKeyStore; @@ -81,7 +79,7 @@ public class FileBasedOpenPgpKeyStore extends AbstractOpenPgpKeyStore { OutputStream outputStream = null; try { - outputStream = prepareFileOutputStream(file); + outputStream = FileUtils.prepareFileOutputStream(file); publicKeys.encode(outputStream); } finally { CloseableUtil.maybeClose(outputStream, LOGGER); @@ -104,7 +102,7 @@ public class FileBasedOpenPgpKeyStore extends AbstractOpenPgpKeyStore { OutputStream outputStream = null; try { - outputStream = prepareFileOutputStream(file); + outputStream = FileUtils.prepareFileOutputStream(file); secretKeys.encode(outputStream); } finally { CloseableUtil.maybeClose(outputStream, LOGGER); @@ -118,7 +116,7 @@ public class FileBasedOpenPgpKeyStore extends AbstractOpenPgpKeyStore { FileInputStream inputStream; try { - inputStream = prepareFileInputStream(file); + inputStream = FileUtils.prepareFileInputStream(file); } catch (FileNotFoundException e) { return null; } @@ -134,7 +132,7 @@ public class FileBasedOpenPgpKeyStore extends AbstractOpenPgpKeyStore { FileInputStream inputStream; try { - inputStream = prepareFileInputStream(file); + inputStream = FileUtils.prepareFileInputStream(file); } catch (FileNotFoundException e) { return null; } 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 88b4df546..8bc42d116 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 @@ -32,9 +32,9 @@ import java.util.logging.Level; import java.util.logging.Logger; import org.jivesoftware.smack.util.CloseableUtil; +import org.jivesoftware.smack.util.FileUtils; import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpMetadataStore; import org.jivesoftware.smackx.ox.store.definition.OpenPgpMetadataStore; -import org.jivesoftware.smackx.ox.util.FileUtils; import org.jivesoftware.smackx.ox.util.Util; import org.jxmpp.jid.BareJid; 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 c72b85ffb..1699f8745 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 @@ -29,9 +29,9 @@ import java.util.logging.Level; import java.util.logging.Logger; import org.jivesoftware.smack.util.CloseableUtil; +import org.jivesoftware.smack.util.FileUtils; import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpTrustStore; import org.jivesoftware.smackx.ox.store.definition.OpenPgpTrustStore; -import org.jivesoftware.smackx.ox.util.FileUtils; import org.jivesoftware.smackx.ox.util.Util; import org.jxmpp.jid.BareJid; diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/util/FileUtils.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/util/FileUtils.java deleted file mode 100644 index 89f94d3aa..000000000 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/util/FileUtils.java +++ /dev/null @@ -1,60 +0,0 @@ -/** - * - * Copyright 2017 Florian Schmaus, 2018 Paul Schaub. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jivesoftware.smackx.ox.util; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; - -public class FileUtils { - - public static FileOutputStream prepareFileOutputStream(File file) throws IOException { - if (!file.exists()) { - - // Create parent directory - File parent = file.getParentFile(); - if (!parent.exists() && !parent.mkdirs()) { - throw new IOException("Cannot create directory " + parent.getAbsolutePath()); - } - - // Create file - if (!file.createNewFile()) { - throw new IOException("Cannot create file " + file.getAbsolutePath()); - } - } - - if (file.isDirectory()) { - throw new AssertionError("File " + file.getAbsolutePath() + " is not a file!"); - } - - return new FileOutputStream(file); - } - - public static FileInputStream prepareFileInputStream(File file) throws IOException { - if (file.exists()) { - if (file.isFile()) { - return new FileInputStream(file); - } else { - throw new IOException("File " + file.getAbsolutePath() + " is not a file!"); - } - } else { - throw new FileNotFoundException("File " + file.getAbsolutePath() + " not found."); - } - } -}