2014-02-17 23:58:40 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Copyright the original author or authors
|
|
|
|
*
|
|
|
|
* 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.smack.util;
|
|
|
|
|
2014-02-23 17:48:07 +01:00
|
|
|
import java.io.BufferedReader;
|
2014-03-20 13:26:57 +01:00
|
|
|
import java.io.File;
|
2018-08-15 17:36:29 +02:00
|
|
|
import java.io.FileInputStream;
|
2014-03-20 13:26:57 +01:00
|
|
|
import java.io.FileNotFoundException;
|
2018-08-15 17:36:29 +02:00
|
|
|
import java.io.FileOutputStream;
|
2014-03-20 13:26:57 +01:00
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.FileWriter;
|
2014-02-17 23:58:40 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2014-02-23 17:48:07 +01:00
|
|
|
import java.io.InputStreamReader;
|
2014-03-20 13:26:57 +01:00
|
|
|
import java.io.Reader;
|
2014-02-17 23:58:40 +01:00
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URI;
|
2018-04-04 16:41:24 +02:00
|
|
|
import java.net.URL;
|
2019-05-08 11:34:40 +02:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2014-02-17 23:58:40 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2014-02-23 17:48:07 +01:00
|
|
|
import java.util.Set;
|
2014-03-20 13:26:57 +01:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
public final class FileUtils {
|
|
|
|
|
2014-03-20 13:26:57 +01:00
|
|
|
private static final Logger LOGGER = Logger.getLogger(FileUtils.class.getName());
|
|
|
|
|
2021-03-25 14:58:44 +01:00
|
|
|
public static InputStream getInputStreamForClasspathFile(String path) {
|
|
|
|
return getInputStreamForClasspathFile(path, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static InputStream getInputStreamForClasspathFile(String path, ClassLoader loader) {
|
|
|
|
try {
|
|
|
|
return getStreamForClasspathFile(path, loader);
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOGGER.log(Level.FINE, "Suppressed IOException in getInputStreamForClasspathFile", e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-04 16:41:24 +02:00
|
|
|
public static InputStream getStreamForClasspathFile(String path, ClassLoader loader) throws IOException {
|
|
|
|
// Get an array of class loaders to try loading the providers files from.
|
|
|
|
List<ClassLoader> classLoaders = getClassLoaders();
|
|
|
|
if (loader != null) {
|
|
|
|
classLoaders.add(0, loader);
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
2018-04-04 16:41:24 +02:00
|
|
|
for (ClassLoader classLoader : classLoaders) {
|
|
|
|
InputStream is = classLoader.getResourceAsStream(path);
|
2014-02-23 17:48:07 +01:00
|
|
|
|
2018-04-04 16:41:24 +02:00
|
|
|
if (is != null) {
|
|
|
|
return is;
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
}
|
2018-04-04 16:41:24 +02:00
|
|
|
throw new IOException("Unable to get '" + path + "' from classpath. Tried ClassLoaders:" + classLoaders);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static InputStream getStreamForUri(URI uri, ClassLoader loader) throws IOException {
|
|
|
|
String protocol = uri.getScheme();
|
|
|
|
if (protocol.equals("classpath")) {
|
|
|
|
String path = uri.getSchemeSpecificPart();
|
|
|
|
return getStreamForClasspathFile(path, loader);
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
2018-04-04 16:41:24 +02:00
|
|
|
|
|
|
|
URL url = uri.toURL();
|
|
|
|
return url.openStream();
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
2014-02-23 17:48:07 +01:00
|
|
|
|
2014-02-17 23:58:40 +01:00
|
|
|
/**
|
|
|
|
* Returns default classloaders.
|
|
|
|
*
|
2014-05-15 15:04:46 +02:00
|
|
|
* @return a List of ClassLoader instances.
|
2014-02-17 23:58:40 +01:00
|
|
|
*/
|
2014-05-15 15:04:46 +02:00
|
|
|
public static List<ClassLoader> getClassLoaders() {
|
2014-02-17 23:58:40 +01:00
|
|
|
ClassLoader[] classLoaders = new ClassLoader[2];
|
|
|
|
classLoaders[0] = FileUtils.class.getClassLoader();
|
|
|
|
classLoaders[1] = Thread.currentThread().getContextClassLoader();
|
|
|
|
|
2014-05-15 15:04:46 +02:00
|
|
|
// Clean up possible null values. Note that #getClassLoader may return a null value.
|
|
|
|
List<ClassLoader> loaders = new ArrayList<ClassLoader>(classLoaders.length);
|
2014-02-17 23:58:40 +01:00
|
|
|
for (ClassLoader classLoader : classLoaders) {
|
|
|
|
if (classLoader != null) {
|
|
|
|
loaders.add(classLoader);
|
|
|
|
}
|
|
|
|
}
|
2014-05-15 15:04:46 +02:00
|
|
|
return loaders;
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
2018-04-04 16:41:24 +02:00
|
|
|
public static boolean addLines(String uriString, Set<String> set) throws MalformedURLException, IOException {
|
|
|
|
URI uri = URI.create(uriString);
|
|
|
|
InputStream is = getStreamForUri(uri, null);
|
2019-05-08 11:34:40 +02:00
|
|
|
InputStreamReader sr = new InputStreamReader(is, StandardCharsets.UTF_8);
|
2017-02-11 16:16:41 +01:00
|
|
|
BufferedReader br = new BufferedReader(sr);
|
2018-08-13 16:39:56 +02:00
|
|
|
try {
|
|
|
|
String line;
|
|
|
|
while ((line = br.readLine()) != null) {
|
|
|
|
set.add(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
br.close();
|
2014-02-23 17:48:07 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2014-03-20 13:26:57 +01:00
|
|
|
|
|
|
|
/**
|
2015-03-29 12:15:32 +02:00
|
|
|
* Reads the contents of a File.
|
2014-03-20 13:26:57 +01:00
|
|
|
*
|
2019-08-30 12:08:30 +02:00
|
|
|
* @param file TODO javadoc me please
|
2014-03-20 13:26:57 +01:00
|
|
|
* @return the content of file or null in case of an error
|
2019-10-30 12:02:36 +01:00
|
|
|
* @throws IOException if an I/O error occurred.
|
2014-03-20 13:26:57 +01:00
|
|
|
*/
|
2017-02-11 16:16:41 +01:00
|
|
|
@SuppressWarnings("DefaultCharset")
|
2017-12-23 20:21:19 +01:00
|
|
|
public static String readFileOrThrow(File file) throws IOException {
|
2019-02-04 09:36:37 +01:00
|
|
|
try (Reader reader = new FileReader(file)) {
|
2014-08-15 23:16:18 +02:00
|
|
|
char[] buf = new char[8192];
|
2014-03-20 13:26:57 +01:00
|
|
|
int len;
|
|
|
|
StringBuilder s = new StringBuilder();
|
|
|
|
while ((len = reader.read(buf)) >= 0) {
|
|
|
|
s.append(buf, 0, len);
|
|
|
|
}
|
|
|
|
return s.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String readFile(File file) {
|
|
|
|
try {
|
|
|
|
return readFileOrThrow(file);
|
2015-01-06 16:11:57 +01:00
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
LOGGER.log(Level.FINE, "readFile", e);
|
|
|
|
} catch (IOException e) {
|
2014-03-20 13:26:57 +01:00
|
|
|
LOGGER.log(Level.WARNING, "readFile", e);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-02-11 16:16:41 +01:00
|
|
|
@SuppressWarnings("DefaultCharset")
|
2015-09-29 22:35:50 +02:00
|
|
|
public static void writeFileOrThrow(File file, CharSequence content) throws IOException {
|
2014-03-20 13:26:57 +01:00
|
|
|
FileWriter writer = new FileWriter(file, false);
|
2015-09-29 22:35:50 +02:00
|
|
|
try {
|
|
|
|
writer.write(content.toString());
|
|
|
|
} finally {
|
|
|
|
writer.close();
|
|
|
|
}
|
2014-03-20 13:26:57 +01:00
|
|
|
}
|
|
|
|
|
2015-09-29 22:35:50 +02:00
|
|
|
public static boolean writeFile(File file, CharSequence content) {
|
2014-03-20 13:26:57 +01:00
|
|
|
try {
|
|
|
|
writeFileOrThrow(file, content);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (IOException e) {
|
|
|
|
LOGGER.log(Level.WARNING, "writeFile", e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-08-15 17:36:29 +02:00
|
|
|
|
|
|
|
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.");
|
|
|
|
}
|
|
|
|
}
|
2018-08-17 12:27:31 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2018-08-17 12:39:28 +02:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|