remade Cacher.java

This commit is contained in:
sirjonasxx 2018-09-28 12:42:21 +02:00
parent 9a49f74c9a
commit 9bde74d19d
3 changed files with 41 additions and 153 deletions

BIN
src/json-simple-1.1.1.jar Normal file

Binary file not shown.

View File

@ -69,7 +69,7 @@ public abstract class Extension {
}
if (getArgument(args, PORT_FLAG) == null) {
System.err.println("Don't forget to include G-Earth's port as program parameters (-p {port})");
System.err.println("Don't forget to include G-Earth's port in your program parameters (-p {port})");
isCorrupted = true;
}
}

View File

@ -1,183 +1,71 @@
package main.misc;
import java.io.*;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Created by Jonas on 05/04/18.
* Created by Jonas on 28/09/18.
*/
public class Cacher {
private static final String CACHEFILENAME = "jsoncache.json";
private static String getCacheDir() {
return System.getProperty("user.home") + File.separator + ".G-Earth" + File.separator;
}
public static boolean exists(String key) {
File f = new File(getCacheDir(), "cache.txt");
if (f.exists()) {
private static boolean cacheFileExists() {
File f = new File(getCacheDir(), CACHEFILENAME);
return (f.exists() && !f.isDirectory());
}
private static JSONObject getCacheContents() {
if (cacheFileExists()) {
try {
List<String> lines = Files.readAllLines(f.toPath());
File f = new File(getCacheDir(), CACHEFILENAME);
String contents = String.join("\n", Files.readAllLines(f.toPath()));
for (String line : lines) {
if (line.startsWith(key+":")) {
return true;
}
}
} catch (IOException e) {
JSONParser parser = new JSONParser();
return (JSONObject) parser.parse(contents);
} catch (IOException | ParseException e) {
e.printStackTrace();
}
}
return false;
return new JSONObject();
}
private static void updateCache(JSONObject contents) {
try (FileWriter file = new FileWriter(new File(getCacheDir(), CACHEFILENAME))) {
public static String get(String key) {
File f = new File(getCacheDir(), "cache.txt");
if (f.exists()) {
try {
List<String> lines = Files.readAllLines(f.toPath());
file.write(contents.toJSONString());
file.flush();
for (String line : lines) {
if (line.startsWith(key+":")) {
return line.split(":")[1];
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
return "";
}
public static void remove(String key) {
File targetFile = new File(getCacheDir() + File.separator + "cache.txt");
File parent = targetFile.getParentFile();
if (!parent.exists() && !parent.mkdirs()) {
throw new IllegalStateException("Couldn't create dir: " + parent);
}
if (!targetFile.exists()) {
try {
targetFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try
{
ArrayList<String> lines = new ArrayList<String>();
File f1 = new File(getCacheDir() + File.separator + "cache.txt");
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
String line = null;
while ((line = br.readLine()) != null)
{
if (!line.startsWith(key + ":"))
lines.add(line);
}
fr.close();
br.close();
FileWriter fw = new FileWriter(f1);
BufferedWriter out = new BufferedWriter(fw);
for (int i = 0; i < lines.size(); i++) {
out.write(lines.get(i));
if (i != lines.size() - 1) out.write("\n");
}
out.flush();
out.close();
}
catch (Exception ex)
{
ex.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void add(String key, String value) {
File targetFile = new File(getCacheDir() + File.separator + "cache.txt");
File parent = targetFile.getParentFile();
if (!parent.exists() && !parent.mkdirs()) {
throw new IllegalStateException("Couldn't create dir: " + parent);
}
if (!targetFile.exists()) {
try {
targetFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void put(String key, Object val) {
JSONObject object = getCacheContents();
if (object.containsKey(key)) object.remove(key);
// File f = new File(getCacheDir(), "cache.txt");
// if (!f.exists()) {
// try {
// PrintWriter writer = new PrintWriter(f.getPath(), "UTF-8");
// writer.write("");
// writer.close();
// } catch (FileNotFoundException | UnsupportedEncodingException e) {
// e.printStackTrace();
// }
// }
try
{
ArrayList<String> lines = new ArrayList<String>();
File f1 = new File(getCacheDir() + File.separator + "cache.txt");
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
String line = null;
boolean containmmm = false;
while ((line = br.readLine()) != null)
{
if (line.startsWith(key+":"))
containmmm = true;
lines.add(line);
}
fr.close();
br.close();
FileWriter fw = new FileWriter(f1);
BufferedWriter out = new BufferedWriter(fw);
if (!containmmm) {
out.write(key+":"+value);
}
for (int i = 0; i < lines.size(); i++) {
out.write("\n"+ lines.get(i));
}
out.flush();
out.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
object.put(key, val);
updateCache(object);
}
public static Object get(String key) {
JSONObject object = getCacheContents();
public static void update(String key, String value) {
remove(key);
add(key, value);
return object.get(key);
}
public static void main(String[] args) {
// System.out.println(exists("hallo"));
// System.out.println(get("hallo"));
//
// add("hallo","doei");
//
// System.out.println(exists("hallo"));
// System.out.println(get("hallo"));
//
// remove("hallo");
// System.out.println(get("hallo"));
System.out.println(get("PRODUCTION-201804032203-770536283-pingHeader"));
public static void clear() {
updateCache(new JSONObject());
}
}