diff --git a/src/main/java/me/konloch/kontainer/io/DiskReader.java b/src/main/java/me/konloch/kontainer/io/DiskReader.java deleted file mode 100644 index 1dc07c95..00000000 --- a/src/main/java/me/konloch/kontainer/io/DiskReader.java +++ /dev/null @@ -1,120 +0,0 @@ -package me.konloch.kontainer.io; - -import the.bytecode.club.bytecodeviewer.util.EncodeUtils; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.util.*; - -/** - * Used to load from the disk, optional caching - * - * @author Konloch - */ - -public class DiskReader -{ - - public static Random random = new Random(); - public static Map> map = new HashMap<>(); - - /** - * Used to load from file, allows caching - */ - public synchronized static List loadArrayList(String fileName, boolean cache) - { - List array = new ArrayList<>(); - if (!map.containsKey(fileName)) - { - try - { - File file = new File(fileName); - if (!file.exists()) // doesn't exist, return empty - return array; - - try (FileReader fr = new FileReader(file); - BufferedReader reader = new BufferedReader(fr)) - { - String add; - - while ((add = reader.readLine()) != null) - array.add(add); - - } - - if (cache) - map.put(fileName, array); - } - catch (Exception e) - { - e.printStackTrace(); - } - } - else - { - array = map.get(fileName); - } - - return array; - - } - - /** - * Used to load from file - */ - public synchronized static String loadAsString(String fileName) throws Exception - { - StringBuilder s = new StringBuilder(); - - try (FileReader fr = new FileReader(fileName); - BufferedReader reader = new BufferedReader(fr)) - { - for (String add = reader.readLine(); add != null; add = reader.readLine()) - { - s.append(EncodeUtils.unicodeToString(add)).append(System.lineSeparator()); - } - } - - return s.toString(); - } - - /** - * Used to load a string via line number lineNumber = -1 means random. - */ - public static String loadString(String fileName, int lineNumber, boolean cache) throws Exception - { - - List array; - if (!map.containsKey(fileName)) - { - array = new ArrayList<>(); - File file = new File(fileName); - - try (FileReader fr = new FileReader(file); - BufferedReader reader = new BufferedReader(fr)) - { - String add; - - while ((add = reader.readLine()) != null) - array.add(add); - } - - if (cache) - map.put(fileName, array); - } - else - { - array = map.get(fileName); - } - - if (lineNumber == -1) - { - int size = array.size(); - return array.get(random.nextInt(size)); - } - else - return array.get(lineNumber); - } - -} diff --git a/src/main/java/me/konloch/kontainer/io/DiskWriter.java b/src/main/java/me/konloch/kontainer/io/DiskWriter.java deleted file mode 100644 index 86b07712..00000000 --- a/src/main/java/me/konloch/kontainer/io/DiskWriter.java +++ /dev/null @@ -1,214 +0,0 @@ -package me.konloch.kontainer.io; - -import java.io.*; -import java.util.Arrays; - -/** - * This method will save to disk - * - * @author Konloch - */ - -public class DiskWriter -{ - - /** - * Used to insert a difference string with preserving the file extension - * - * @param fileName The file name - * @param difference Normally an integer - * @return The filename with the difference inserted and the file extension - * preserved - */ - public static String insertFileName(String fileName, String difference) - { - String[] babe = fileName.split("\\."); - int count = 0; - int math = babe.length; - StringBuilder m = new StringBuilder(); - - for (String s2 : babe) - { - m.append(s2); - if (math - 2 == count) - m.append(difference).append("."); - else if (math - 1 != count) - m.append("."); - count++; - } - - return m.toString(); - } - - /** - * Writes a new line to the file, if it doesn't exist it will automatically - * create it. - * - * @param filename - * @param fileContents - * @param debug - */ - public static synchronized void writeNewLine(String filename, byte[] fileContents, boolean debug) - { - new File(filename).getParentFile().mkdirs(); - String original = filename; - int counter = 0; - - boolean saved = false; - int failSafe = 0; - while (!saved && failSafe++ <= 42069) - { - try (FileWriter fr = new FileWriter(filename, true); BufferedWriter bw = new BufferedWriter(fr); PrintWriter writer = new PrintWriter(bw)) - { - writer.println(Arrays.toString(fileContents)); - if (debug) - System.out.println("Saved " + filename + " to disk"); - saved = true; - } - catch (Exception e) - { - if (debug) - System.out.println("Failed saving, trying to save as " + filename); - if (original.contains(".")) - { - filename = insertFileName(original, "" + counter); - } - else - filename = original + counter; - counter++; - } - } - } - - /** - * Writes a string to the file - */ - public static void writeNewLine(String filename, String lineToWrite) - { - writeNewLine(filename, lineToWrite, false); - } - - /** - * Writes a string to the file - */ - public static synchronized void writeNewLine(String filename, String lineToWrite, boolean debug) - { - new File(filename).getParentFile().mkdirs(); - String original = filename; - int counter = 0; - - boolean saved = false; - int failSafe = 0; - while (!saved && failSafe++ <= 42069) - { - try (FileWriter fr = new FileWriter(filename, true); BufferedWriter bw = new BufferedWriter(fr); PrintWriter writer = new PrintWriter(bw)) - { - writer.println(lineToWrite); - if (debug) - System.out.println("Saved " + filename + ">" + lineToWrite + " to disk"); - saved = true; - } - catch (Exception e) - { - if (debug) - System.out.println("Failed saving, trying to save as " + filename); - if (original.contains(".")) - { - filename = insertFileName(original, "" + counter); - } - else - filename = original + counter; - counter++; - } - } - } - - /** - * Deletes the original file if it exists, then writes the fileContents[] to - * the file. - * - * @param filename - * @param fileContents - * @param debug - */ - public static synchronized void replaceFileBytes(String filename, byte[] fileContents, boolean debug) - { - new File(filename).getParentFile().mkdirs(); - File f = new File(filename); - if (f.exists()) - f.delete(); - - String original = filename; - int counter = 0; - - boolean saved = false; - int failSafe = 0; - while (!saved && failSafe++ <= 42069) - { - try (FileOutputStream stream = new FileOutputStream(filename)) - { - stream.write(fileContents); - stream.flush(); - if (debug) - System.out.println("Saved " + filename + " to disk"); - saved = true; - } - catch (Exception e) - { - if (debug) - System.out.println("Failed saving, trying to save as " + filename); - if (original.contains(".")) - { - filename = insertFileName(original, "" + counter); - } - else - filename = original + counter; - counter++; - } - } - } - - /** - * Deletes the original file if it exists, then writes the lineToWrite to - * the file. - * - * @param filename - * @param lineToWrite - * @param debug - */ - public static synchronized void replaceFile(String filename, String lineToWrite, boolean debug) - { - new File(filename).getParentFile().mkdirs(); - File f = new File(filename); - if (f.exists()) - f.delete(); - String original = filename; - int counter = 0; - - boolean saved = false; - int failSafe = 0; - while (!saved && failSafe++ <= 42069) - { - try (FileWriter fr = new FileWriter(filename, true); BufferedWriter bw = new BufferedWriter(fr); PrintWriter writer = new PrintWriter(bw)) - { - writer.println(lineToWrite); - if (debug) - System.out.println("Saved " + filename + ">" + lineToWrite + " to disk"); - saved = true; - } - catch (Exception e) - { - if (debug) - System.out.println("Failed saving, trying to save as " + filename + "_"); - if (original.contains(".")) - { - filename = insertFileName(original, "" + counter); - } - else - filename = original + counter; - counter++; - } - } - } - -}