All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good
Extracts backup code to its own class Adds a new GUI to display progress and the file copied
285 lines
9.8 KiB
Java
285 lines
9.8 KiB
Java
package net.knarcraft.minecraftserverlauncher.utility;
|
|
|
|
import net.knarcraft.minecraftserverlauncher.Main;
|
|
import net.knarcraft.minecraftserverlauncher.userinterface.WebBrowser;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.BufferedWriter;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileOutputStream;
|
|
import java.io.FileWriter;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.io.OutputStreamWriter;
|
|
import java.net.URI;
|
|
import java.net.URISyntaxException;
|
|
import java.net.URL;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.StandardCopyOption;
|
|
import java.util.Scanner;
|
|
|
|
/**
|
|
* A holding class for methods shared between classes
|
|
*
|
|
* @author Kristian Knarvik <kristian.knarvik@knett.no>
|
|
* @version 1.0.0
|
|
* @since 1.0.0
|
|
*/
|
|
public final class CommonFunctions {
|
|
|
|
private static final String filesDirectory = Main.getApplicationWorkDirectory() + File.separator + "files";
|
|
|
|
/**
|
|
* Creates all folders necessary for tests and normal operation
|
|
*
|
|
* @throws FileNotFoundException <p>If unable to create a folder</p>
|
|
*/
|
|
public static void createAllFolders() throws FileNotFoundException {
|
|
createFolder(new File(filesDirectory));
|
|
createFolder(new File(filesDirectory + File.separator + "Jars"));
|
|
}
|
|
|
|
/**
|
|
* Creates a given folder
|
|
*
|
|
* @param folder <p>The folder to create</p>
|
|
* @throws FileNotFoundException <p>If unable to create the folder</p>
|
|
*/
|
|
public static void createFolder(File folder) throws FileNotFoundException {
|
|
if (!folder.exists()) {
|
|
if (!folder.mkdirs()) {
|
|
throw new FileNotFoundException("Cannot create necessary directory.");
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets a resource as an InputStream
|
|
*
|
|
* @param resourceName <p>The name of the resource you want to readFromServer</p>
|
|
* @return <p>An input stream which can be used to access the resource</p>
|
|
*/
|
|
public static InputStream getResourceAsStream(String resourceName) {
|
|
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
|
|
return classloader.getResourceAsStream(resourceName);
|
|
}
|
|
|
|
/**
|
|
* Gets a resource as a Scanner
|
|
*
|
|
* @param resourceName <p>The name of the resource you want to readFromServer</p>
|
|
* @return <p>A scanner which can be used to readFromServer contents of the resource</p>
|
|
* @throws FileNotFoundException <p>If the resource is not found</p>
|
|
*/
|
|
public static Scanner getResourceAsScanner(String resourceName) throws FileNotFoundException {
|
|
InputStream is = getResourceAsStream(resourceName);
|
|
if (is == null) {
|
|
throw new FileNotFoundException("The resource was not found.");
|
|
}
|
|
return new Scanner(is);
|
|
}
|
|
|
|
/**
|
|
* Finds a substring between two substrings in a string
|
|
*
|
|
* @param string <p>The string containing the substrings</p>
|
|
* @param start <p>The substring before the wanted substring</p>
|
|
* @param end <p>The substring after the wanted substring</p>
|
|
* @return <p>The wanted substring</p>
|
|
*/
|
|
public static String stringBetween(String string, String start, String end) {
|
|
int startPos = string.indexOf(start) + start.length();
|
|
if (!string.contains(start) || string.indexOf(end, startPos) < startPos) {
|
|
return "";
|
|
}
|
|
return string.substring(startPos, string.indexOf(end, startPos));
|
|
}
|
|
|
|
/**
|
|
* Reads a file from a website
|
|
*
|
|
* <p>This is used to find the newest version of jars and the software.</p>
|
|
*
|
|
* @param path <p>The full url of the file to readFromServer</p>
|
|
* @return <p>True if successful. False otherwise</p>
|
|
* @throws IOException <p>If unable to find or read the file</p>
|
|
*/
|
|
public static String readRemoteFile(String path) throws IOException {
|
|
URL url = new URL(path);
|
|
return new Scanner(url.openStream()).useDelimiter("\\Z").next();
|
|
}
|
|
|
|
/**
|
|
* Gets a buffered reader for reading a given file
|
|
*
|
|
* @param path <p>The path of the file to read</p>
|
|
* @return <p>A buffered reader for reading the file</p>
|
|
* @throws FileNotFoundException <p>If the file does not exist</p>
|
|
*/
|
|
public static BufferedReader getFileReader(String path) throws FileNotFoundException {
|
|
return new BufferedReader(new InputStreamReader(new FileInputStream(path)));
|
|
}
|
|
|
|
/**
|
|
* Gets a buffered writer for writing to a given file
|
|
* @param path <p>The path to the file to write to</p>
|
|
* @return <p>A buffered writer for writing to the file</p>
|
|
* @throws FileNotFoundException <p>If the file does not exist</p>
|
|
*/
|
|
public static BufferedWriter getFileWriter(String path) throws FileNotFoundException {
|
|
return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path)));
|
|
}
|
|
|
|
/**
|
|
* Reads a file from disk
|
|
*
|
|
* @param path <p>The path of the file to read</p>
|
|
* @return <p>The contents of the file</p>
|
|
* @throws IOException <p>If unable to find or read the file</p>
|
|
*/
|
|
public static String readFile(String path) throws IOException {
|
|
return CommonFunctions.readBufferedReader(getFileReader(path));
|
|
}
|
|
|
|
|
|
/**
|
|
* Writes text to a file and adds a trailing newline
|
|
*
|
|
* @param path <p>The path of the file to write to</p>
|
|
* @param text <p>The text to write</p>
|
|
* @throws IOException <p>If unable to write to the file</p>
|
|
*/
|
|
public static void writeFile(String path, String text) throws IOException {
|
|
writeFile(path, text, !text.equals(""));
|
|
}
|
|
|
|
/**
|
|
* Writes text to a file
|
|
*
|
|
* @param path <p>The path of the file to write to</p>
|
|
* @param text <p>The text to write</p>
|
|
* @param addTrailingNewline <p>Whether to add a new line at the end of the file</p>
|
|
* @throws IOException <p>If unable to write to the file</p>
|
|
*/
|
|
public static void writeFile(String path, String text, Boolean addTrailingNewline) throws IOException {
|
|
BufferedWriter writer = getFileWriter(path);
|
|
writer.write(text);
|
|
if (addTrailingNewline) {
|
|
writer.newLine();
|
|
}
|
|
writer.close();
|
|
}
|
|
|
|
/**
|
|
* Appends text to a file
|
|
*
|
|
* @param path <p>The path to the file to append to</p>
|
|
* @param text <p>The text to append</p>
|
|
* @throws IOException <p>If unable to append to the file</p>
|
|
*/
|
|
public static void appendFile(String path, String text) throws IOException {
|
|
BufferedWriter writer = new BufferedWriter(new FileWriter(path, true));
|
|
writer.write(text);
|
|
writer.newLine();
|
|
writer.close();
|
|
}
|
|
|
|
/**
|
|
* Downloads a file from a website and replaces the target file
|
|
*
|
|
* @param path <p>The full url of the file to download</p>
|
|
* @param outfile <p>The file to save to</p>
|
|
* @return <p>True if successful. False otherwise</p>
|
|
*/
|
|
public static boolean downloadFile(String path, Path outfile) {
|
|
try {
|
|
URL url = new URL(path);
|
|
InputStream in = url.openStream();
|
|
Files.copy(in, outfile, StandardCopyOption.REPLACE_EXISTING);
|
|
return true;
|
|
} catch (IOException e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Opens an url in the user's default application.
|
|
*
|
|
* @param url <p>The URL to open</p>
|
|
*/
|
|
public static void goToURL(String url) {
|
|
java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
|
|
try {
|
|
desktop.browse(new URI(url));
|
|
} catch (URISyntaxException | IOException | UnsupportedOperationException e1) {
|
|
WebBrowser.displayPage(url);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Reads all lines from a buffered reader
|
|
*
|
|
* @param reader <p>The buffered reader to read from</p>
|
|
* @return <p>All lines currently readable from the reader, split by the \n character</p>
|
|
* @throws IOException <p>If unable to read from the buffered reader</p>
|
|
*/
|
|
public static String readBufferedReader(BufferedReader reader) throws IOException {
|
|
//String line;
|
|
StringBuilder text = new StringBuilder();
|
|
char[] readCharacters = new char[1000];
|
|
while (reader.ready()) {
|
|
if (reader.read(readCharacters) > 0) {
|
|
text.append(readCharacters);
|
|
readCharacters = new char[1000];
|
|
} else {
|
|
return text.toString().trim();
|
|
}
|
|
}
|
|
return text.toString().trim();
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Validates that a name is not empty and does not contain invalid characters
|
|
*
|
|
* @param name <p>The name to check</p>
|
|
* @return <p>True if the name is valid</p>
|
|
*/
|
|
public static boolean nameIsValid(String name) {
|
|
return name != null && !name.equals("") && name.matches("[^!?;,]+");
|
|
}
|
|
|
|
/**
|
|
* Removes all files within a folder
|
|
*
|
|
* @param target <p>The folder to delete from</p>
|
|
*/
|
|
static void removeFilesRecursively(File target) {
|
|
File[] oldFiles = target.listFiles();
|
|
if (oldFiles == null) {
|
|
throw new IllegalArgumentException("Unable to list files in directory");
|
|
}
|
|
for (File file : oldFiles) {
|
|
if (file.isFile()) {
|
|
if (!file.delete()) {
|
|
throw new IllegalArgumentException("Unable to delete a file from the directory");
|
|
}
|
|
}
|
|
}
|
|
for (File file : oldFiles) {
|
|
if (file.isDirectory()) {
|
|
removeFilesRecursively(file);
|
|
if (!file.delete()) {
|
|
throw new IllegalArgumentException("Unable to delete a file from the directory");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|