2018-01-31 15:03:53 +00:00
|
|
|
package me.konloch.kontainer.io;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.File;
|
2021-04-12 20:19:12 +00:00
|
|
|
import java.io.FileReader;
|
2018-01-31 15:03:53 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
2021-08-26 21:59:07 +00:00
|
|
|
import java.util.Map;
|
2018-01-31 15:03:53 +00:00
|
|
|
import java.util.Random;
|
2021-04-12 20:19:12 +00:00
|
|
|
import the.bytecode.club.bytecodeviewer.util.EncodeUtils;
|
2018-01-31 15:03:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to load from the disk, optional caching
|
2018-01-31 15:41:24 +00:00
|
|
|
*
|
2018-01-31 15:03:53 +00:00
|
|
|
* @author Konloch
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class DiskReader {
|
|
|
|
|
2018-01-31 15:41:24 +00:00
|
|
|
public static Random random = new Random();
|
2021-08-26 21:59:07 +00:00
|
|
|
public static Map<String, ArrayList<String>> map = new HashMap<>();
|
2018-01-31 15:41:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to load from file, allows caching
|
|
|
|
*/
|
|
|
|
public synchronized static ArrayList<String> loadArrayList(String fileName,
|
|
|
|
boolean cache) {
|
2021-04-12 22:31:22 +00:00
|
|
|
ArrayList<String> array = new ArrayList<>();
|
2018-01-31 15:41:24 +00:00
|
|
|
if (!map.containsKey(fileName)) {
|
|
|
|
try {
|
|
|
|
File file = new File(fileName);
|
2021-07-25 16:54:08 +00:00
|
|
|
if (!file.exists()) // doesn't exist, return empty
|
2018-01-31 15:41:24 +00:00
|
|
|
return array;
|
|
|
|
|
2021-07-25 16:54:08 +00:00
|
|
|
try (FileReader fr = new FileReader(file);
|
|
|
|
BufferedReader reader = new BufferedReader(fr)) {
|
|
|
|
String add;
|
2018-01-31 15:41:24 +00:00
|
|
|
|
2021-07-25 16:54:08 +00:00
|
|
|
while ((add = reader.readLine()) != null)
|
|
|
|
array.add(add);
|
2018-01-31 15:41:24 +00:00
|
|
|
|
2021-07-25 16:54:08 +00:00
|
|
|
}
|
2018-01-31 15:41:24 +00:00
|
|
|
|
|
|
|
if (cache)
|
|
|
|
map.put(fileName, array);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
array = map.get(fileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to load from file
|
|
|
|
*/
|
2019-11-20 03:27:55 +00:00
|
|
|
public synchronized static String loadAsString(String fileName) throws Exception {
|
|
|
|
StringBuilder s = new StringBuilder();
|
2018-01-31 15:41:24 +00:00
|
|
|
|
2021-07-25 16:54:08 +00:00
|
|
|
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.getProperty("line.separator"));
|
|
|
|
}
|
2019-11-20 03:27:55 +00:00
|
|
|
}
|
2018-01-31 15:41:24 +00:00
|
|
|
|
2019-11-20 03:27:55 +00:00
|
|
|
return s.toString();
|
2018-01-31 15:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to load a string via line number lineNumber = -1 means random.
|
|
|
|
*/
|
|
|
|
public static String loadString(String fileName, int lineNumber,
|
|
|
|
boolean cache) throws Exception {
|
|
|
|
|
|
|
|
ArrayList<String> array;
|
|
|
|
if (!map.containsKey(fileName)) {
|
2021-04-12 22:31:22 +00:00
|
|
|
array = new ArrayList<>();
|
2018-01-31 15:41:24 +00:00
|
|
|
File file = new File(fileName);
|
|
|
|
|
2021-07-25 16:54:08 +00:00
|
|
|
try (FileReader fr = new FileReader(file);
|
|
|
|
BufferedReader reader = new BufferedReader(fr)) {
|
|
|
|
String add;
|
2018-01-31 15:41:24 +00:00
|
|
|
|
2021-07-25 16:54:08 +00:00
|
|
|
while ((add = reader.readLine()) != null)
|
|
|
|
array.add(add);
|
|
|
|
}
|
2018-01-31 15:41:24 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2018-01-31 15:03:53 +00:00
|
|
|
|
2021-08-26 21:59:07 +00:00
|
|
|
}
|