diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java index 2df3127..187e167 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java @@ -16,7 +16,7 @@ import java.nio.file.StandardCopyOption; import java.util.Scanner; /** - * A holding class for methods shared between classes. + * A holding class for methods shared between classes * * @author Kristian Knarvik * @version 1.0.0 @@ -199,9 +199,10 @@ public final class CommonFunctions { StringBuilder text = new StringBuilder(); char[] readCharacters = new char[1000]; while (reader.ready()) { - reader.read(readCharacters); - text.append(readCharacters); - readCharacters = new char[1000]; + if (reader.read(readCharacters) > 0) { + text.append(readCharacters); + readCharacters = new char[1000]; + } } return text.toString().trim(); } @@ -244,4 +245,31 @@ public final class CommonFunctions { public static boolean nameIsValid(String name) { return !name.equals("") && name.matches("[^!?;,]+"); } + + /** + * Removes all files within a folder + * @param target

The folder to delete from

+ */ + 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"); + } + } + } + } + } diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/Updater.java b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/Updater.java index c087e39..2b14493 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/Updater.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/Updater.java @@ -5,7 +5,6 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import net.knarcraft.minecraftserverlauncher.Main; -import net.knarcraft.minecraftserverlauncher.userinterface.GUI; import javax.swing.*; import java.io.*; diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarDownloaderTest.java b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarDownloaderTest.java index 409ba0b..1e0a550 100644 --- a/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarDownloaderTest.java +++ b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarDownloaderTest.java @@ -109,12 +109,6 @@ public class JarDownloaderTest { if (!target.exists() && !target.mkdirs()) { throw new IllegalArgumentException("Unable to create the test files directory"); } - File[] oldFiles = target.listFiles(); - if (oldFiles == null) { - throw new IllegalArgumentException("Unable to list files in jar test directory"); - } - for (File file : oldFiles) { - assertTrue(file.delete()); - } + CommonFunctions.removeFilesRecursively(target); } }