2626import javax .crypto .spec .SecretKeySpec ;
2727import java .io .*;
2828import java .nio .charset .StandardCharsets ;
29+ import java .nio .file .Files ;
2930import java .nio .file .Path ;
3031import java .nio .file .Paths ;
3132import java .security .MessageDigest ;
@@ -42,49 +43,18 @@ public class JSONManager {
4243 private static SecretKeySpec secretKey ;
4344
4445 /**
45- * Read JSONArray from file.
46+ * Read contents of a file.
4647 *
47- * @return JSONArray with JSON data .
48+ * @return Contents of a file .
4849 */
49- public static JSONArray readJSONArray (File file ) {
50- JSONArray object = null ;
50+ public static String readFile (File file ) {
51+ byte [] encoded = new byte [ 0 ] ;
5152 try {
52- BufferedReader br = new BufferedReader (new FileReader (file ));
53- StringBuilder sb = new StringBuilder ();
54- String line = br .readLine ();
55- while (line != null ) {
56- sb .append (line );
57- line = br .readLine ();
58- }
59- object = new JSONArray (sb .toString ());
60- br .close ();
61- } catch (Exception e ) {
62- Main .logger .error ("Unable to read file!" , e );
63- }
64- return object ;
65- }
66-
67- /**
68- * Read JSONObject from file.
69- *
70- * @return JSONObject with JSON data.
71- */
72- public static JSONObject readJSONObject (File file ) {
73- JSONObject object = null ;
74- try {
75- BufferedReader br = new BufferedReader (new FileReader (file ));
76- StringBuilder sb = new StringBuilder ();
77- String line = br .readLine ();
78- while (line != null ) {
79- sb .append (line );
80- line = br .readLine ();
81- }
82- object = new JSONObject (sb .toString ());
83- br .close ();
84- } catch (Exception e ) {
85- Main .logger .error ("Unable to read file!" , e );
53+ encoded = Files .readAllBytes (file .toPath ());
54+ } catch (IOException e ) {
55+ Main .logger .error (e );
8656 }
87- return object ;
57+ return new String ( encoded , StandardCharsets . UTF_8 ) ;
8858 }
8959
9060 /**
@@ -109,7 +79,7 @@ public static void writeFile(Object json, File file) {
10979 */
11080 public static List <Account > getAccounts () {
11181 List <Account > accounts = new ArrayList <>();
112- JSONArray accountsJSON = readJSONArray ( accountsFile );
82+ JSONArray accountsJSON = new JSONArray ( readFile ( accountsFile ) );
11383 for (int i = 0 ; i < accountsJSON .length (); i ++) {
11484 JSONObject currentAccount = accountsJSON .getJSONObject (i );
11585 String username = (String ) currentAccount .get ("username" );
@@ -126,7 +96,7 @@ public static List<Account> getAccounts() {
12696 * @param encryptedPassword Account encrypted password.
12797 */
12898 public static void addNewAccount (String username , String encryptedPassword ) {
129- JSONArray accountsJSON = readJSONArray ( accountsFile );
99+ JSONArray accountsJSON = new JSONArray ( readFile ( accountsFile ) );
130100 JSONObject newAccount = new JSONObject ();
131101 newAccount .put ("username" , username );
132102 newAccount .put ("password" , encryptedPassword );
@@ -140,13 +110,13 @@ public static void addNewAccount(String username, String encryptedPassword) {
140110 * @param account Account to delete.
141111 */
142112 public static void deleteAccount (Account account ) {
143- JSONArray accountsJSON = readJSONArray ( accountsFile );
113+ JSONArray accountsJSON = new JSONArray ( readFile ( accountsFile ) );
144114 accountsJSON .remove (getAccountIndex (account ));
145115 writeFile (accountsJSON , accountsFile );
146116 }
147117
148118 public static int getAccountIndex (Account account ) {
149- JSONArray accountsJSON = readJSONArray ( accountsFile );
119+ JSONArray accountsJSON = new JSONArray ( readFile ( accountsFile ) );
150120 String username = account .getUsername ();
151121 for (int i = 0 ; i < accountsJSON .length (); i ++) {
152122 JSONObject currentAccount = accountsJSON .getJSONObject (i );
@@ -217,7 +187,7 @@ public static String decrypt(String strToDecrypt, String secret) {
217187 * @param remove Should we remove this entry or add this entry?
218188 */
219189 public static void editConfig (String key , Object value , boolean remove ) {
220- JSONObject config = readJSONObject ( configFile );
190+ JSONObject config = new JSONObject ( readFile ( configFile ) );
221191 if (remove ) {
222192 config .remove (key );
223193 } else {
@@ -232,7 +202,22 @@ public static void editConfig(String key, Object value, boolean remove) {
232202 * @return Yes/no if we should.
233203 */
234204 public static boolean shouldWeUpdate () {
235- JSONObject config = readJSONObject ( configFile );
205+ JSONObject config = new JSONObject ( readFile ( configFile ) );
236206 return config .getBoolean ("autoCheckTTRUpdates" );
237207 }
208+
209+ public static void convertToNewFormat () {
210+ JSONObject oldFile = new JSONObject (readFile (accountsFile ));
211+ JSONArray newFile = new JSONArray ();
212+ Iterator <String > keys = oldFile .keys ();
213+
214+ while (keys .hasNext ()) {
215+ String key = keys .next ();
216+ if (oldFile .get (key ) instanceof JSONObject ) {
217+ JSONObject temp = (JSONObject ) oldFile .get (key );
218+ newFile .put (temp );
219+ }
220+ }
221+ writeFile (newFile , accountsFile );
222+ }
238223}
0 commit comments