mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-09 17:19:39 +02:00
Consolidate FileUtils from smack-openpgp into smack-core
This commit is contained in:
parent
a00aa726fe
commit
3e65cb31c3
5 changed files with 43 additions and 69 deletions
|
@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue