Consolidate FileUtils from smack-openpgp into smack-core

This commit is contained in:
Florian Schmaus 2018-08-15 17:36:29 +02:00
parent a00aa726fe
commit 3e65cb31c3
5 changed files with 43 additions and 69 deletions

View File

@ -18,7 +18,9 @@ package org.jivesoftware.smack.util;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader; import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
@ -160,4 +162,38 @@ public final class FileUtils {
return false; 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.");
}
}
} }

View File

@ -16,9 +16,6 @@
*/ */
package org.jivesoftware.smackx.ox.store.filebased; 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.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
@ -28,6 +25,7 @@ import java.util.Date;
import java.util.Map; import java.util.Map;
import org.jivesoftware.smack.util.CloseableUtil; import org.jivesoftware.smack.util.CloseableUtil;
import org.jivesoftware.smack.util.FileUtils;
import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpKeyStore; import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpKeyStore;
import org.jivesoftware.smackx.ox.store.definition.OpenPgpKeyStore; import org.jivesoftware.smackx.ox.store.definition.OpenPgpKeyStore;
@ -81,7 +79,7 @@ public class FileBasedOpenPgpKeyStore extends AbstractOpenPgpKeyStore {
OutputStream outputStream = null; OutputStream outputStream = null;
try { try {
outputStream = prepareFileOutputStream(file); outputStream = FileUtils.prepareFileOutputStream(file);
publicKeys.encode(outputStream); publicKeys.encode(outputStream);
} finally { } finally {
CloseableUtil.maybeClose(outputStream, LOGGER); CloseableUtil.maybeClose(outputStream, LOGGER);
@ -104,7 +102,7 @@ public class FileBasedOpenPgpKeyStore extends AbstractOpenPgpKeyStore {
OutputStream outputStream = null; OutputStream outputStream = null;
try { try {
outputStream = prepareFileOutputStream(file); outputStream = FileUtils.prepareFileOutputStream(file);
secretKeys.encode(outputStream); secretKeys.encode(outputStream);
} finally { } finally {
CloseableUtil.maybeClose(outputStream, LOGGER); CloseableUtil.maybeClose(outputStream, LOGGER);
@ -118,7 +116,7 @@ public class FileBasedOpenPgpKeyStore extends AbstractOpenPgpKeyStore {
FileInputStream inputStream; FileInputStream inputStream;
try { try {
inputStream = prepareFileInputStream(file); inputStream = FileUtils.prepareFileInputStream(file);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
return null; return null;
} }
@ -134,7 +132,7 @@ public class FileBasedOpenPgpKeyStore extends AbstractOpenPgpKeyStore {
FileInputStream inputStream; FileInputStream inputStream;
try { try {
inputStream = prepareFileInputStream(file); inputStream = FileUtils.prepareFileInputStream(file);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
return null; return null;
} }

View File

@ -32,9 +32,9 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.jivesoftware.smack.util.CloseableUtil; 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.abstr.AbstractOpenPgpMetadataStore;
import org.jivesoftware.smackx.ox.store.definition.OpenPgpMetadataStore; import org.jivesoftware.smackx.ox.store.definition.OpenPgpMetadataStore;
import org.jivesoftware.smackx.ox.util.FileUtils;
import org.jivesoftware.smackx.ox.util.Util; import org.jivesoftware.smackx.ox.util.Util;
import org.jxmpp.jid.BareJid; import org.jxmpp.jid.BareJid;

View File

@ -29,9 +29,9 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.jivesoftware.smack.util.CloseableUtil; 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.abstr.AbstractOpenPgpTrustStore;
import org.jivesoftware.smackx.ox.store.definition.OpenPgpTrustStore; import org.jivesoftware.smackx.ox.store.definition.OpenPgpTrustStore;
import org.jivesoftware.smackx.ox.util.FileUtils;
import org.jivesoftware.smackx.ox.util.Util; import org.jivesoftware.smackx.ox.util.Util;
import org.jxmpp.jid.BareJid; import org.jxmpp.jid.BareJid;

View File

@ -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.");
}
}
}