Kaynağa Gözat

changed loading and saving of profiles

Daniel Lazar 10 yıl önce
ebeveyn
işleme
da11e6a667

+ 23 - 4
src/de/tudarmstadt/informatik/hostage/persistence/ProfileManager.java

@@ -7,11 +7,15 @@ import org.json.JSONObject;
 import android.content.Context;
 import android.content.SharedPreferences;
 
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
 import java.io.StreamCorruptedException;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -140,9 +144,18 @@ public class ProfileManager {
 	 */
 	public void loadData(){
 		try {
-			FileInputStream fin = MainActivity.getContext().openFileInput(PERSIST_FILENAME);
-			JSONArray arr = new JSONArray(readAll(fin));
-			fin.close();
+            String UTF8 = "utf8";
+            int BUFFER_SIZE = 8192;
+            BufferedReader fbr = new BufferedReader(new InputStreamReader(MainActivity.getContext().openFileInput(PERSIST_FILENAME), UTF8), BUFFER_SIZE);
+
+            StringBuilder sb = new StringBuilder();
+            String line;
+            while((line = fbr.readLine()) != null) {
+                sb.append(line);
+            }
+
+            JSONArray arr = new JSONArray(sb.toString());
+			fbr.close();
 
 			for(int i=0; i<arr.length(); i++){
 				JSONObject obj = arr.getJSONObject(i);
@@ -190,6 +203,8 @@ public class ProfileManager {
 	public void persistData(){
 		try {
 			FileOutputStream fout = MainActivity.getContext().openFileOutput(PERSIST_FILENAME, Context.MODE_PRIVATE);
+            int BUFFER_SIZE = 8192;
+            String UTF8 = "utf8";
 
 			JSONArray arr = new JSONArray();
 			for(Profile p: mProfiles.values()){
@@ -197,7 +212,10 @@ public class ProfileManager {
 			}
 
 			fout.write(arr.toString().getBytes());
-			fout.close();
+            BufferedWriter fnw = new BufferedWriter(new OutputStreamWriter(fout, UTF8), BUFFER_SIZE);
+            fout.close();
+			fnw.close();
+
 		} catch (FileNotFoundException e) {
 			e.printStackTrace();
 		} catch (IOException e) {
@@ -642,4 +660,5 @@ public class ProfileManager {
 
 		persistData();
 	}
+
 }