From 0e46725dab522c11d536e891e0338788aabf60c6 Mon Sep 17 00:00:00 2001 From: Pepich Date: Wed, 1 Feb 2017 17:11:16 +0100 Subject: [PATCH] Made file saving run async --- src/com/redstoner/misc/JsonManager.java | 76 +++++++++++++++---------- 1 file changed, 47 insertions(+), 29 deletions(-) diff --git a/src/com/redstoner/misc/JsonManager.java b/src/com/redstoner/misc/JsonManager.java index be7da92..998d137 100644 --- a/src/com/redstoner/misc/JsonManager.java +++ b/src/com/redstoner/misc/JsonManager.java @@ -15,7 +15,7 @@ import com.redstoner.annotations.Version; /** This class provides simple JSON handling, like storing and loading from and to files. * * @author Pepich */ -@Version(major = 1, minor = 0, revision = 0, compatible = -1) +@Version(major = 1, minor = 0, revision = 1, compatible = -1) public class JsonManager { private JsonManager() @@ -43,26 +43,35 @@ public class JsonManager return null; } - /** Saves a JSONObject to a file. Will create the necessary FileStructure like folders and the file itself. + /** Saves a JSONObject to a file. Will create the necessary FileStructure like folders and the file itself.
+ * Note that this operation will be run on a different thread and you do not need to take care of that yourself. * * @param object the JSONObject to save. * @param destination the file to write to. */ public static void save(JSONObject object, File destination) { - if (destination.exists()) - destination.delete(); - else if (!destination.getParentFile().exists()) - destination.getParentFile().mkdirs(); - try + Thread t = new Thread(new Runnable() { - destination.createNewFile(); - FileWriter writer = new FileWriter(destination); - object.writeJSONString(writer); - writer.flush(); - writer.close(); - } - catch (IOException e) - {} + @Override + public void run() + { + if (destination.exists()) + destination.delete(); + else if (!destination.getParentFile().exists()) + destination.getParentFile().mkdirs(); + try + { + destination.createNewFile(); + FileWriter writer = new FileWriter(destination); + object.writeJSONString(writer); + writer.flush(); + writer.close(); + } + catch (IOException e) + {} + } + }); + t.start(); } /** Loads a JSONArray from a file. @@ -85,25 +94,34 @@ public class JsonManager return null; } - /** Saves a JSONArray to a file. Will create the necessary FileStructure like folders and the file itself. + /** Saves a JSONArray to a file. Will create the necessary FileStructure like folders and the file itself.
+ * Note that this operation will be run on a different thread and you do not need to take care of that yourself. * * @param object the JSONArray to save. * @param destination the file to write to. */ public static void save(JSONArray array, File destination) { - if (destination.exists()) - destination.delete(); - else if (!destination.getParentFile().exists()) - destination.getParentFile().mkdirs(); - try + Thread t = new Thread(new Runnable() { - destination.createNewFile(); - FileWriter writer = new FileWriter(destination); - array.writeJSONString(writer); - writer.flush(); - writer.close(); - } - catch (IOException e) - {} + @Override + public void run() + { + if (destination.exists()) + destination.delete(); + else if (!destination.getParentFile().exists()) + destination.getParentFile().mkdirs(); + try + { + destination.createNewFile(); + FileWriter writer = new FileWriter(destination); + array.writeJSONString(writer); + writer.flush(); + writer.close(); + } + catch (IOException e) + {} + } + }); + t.start(); } }