@@ -49,7 +49,12 @@ public Holocron(Context context){
49
49
mGson = new Gson ();
50
50
51
51
mForce = new Force (mContext );
52
- readConfiguration ();
52
+
53
+ try {
54
+ readConfiguration (); //Ensure we have configuration file
55
+ }catch (OutOfMemoryError error ){
56
+ if (debug ) error .printStackTrace ();
57
+ }
53
58
}
54
59
55
60
/**
@@ -66,8 +71,12 @@ public Holocron(final Context context, @Nullable final HolocronInitListener call
66
71
@ Override
67
72
public void run () {
68
73
mForce = new Force (context );
69
- readConfiguration ();
70
- if (callback !=null ) callback .onHolocronInitComplete ();
74
+ try {
75
+ readConfiguration (); //Ensure we have configuration file
76
+ if (callback !=null ) callback .onHolocronInitComplete ();
77
+ }catch (OutOfMemoryError error ){
78
+ if (debug ) error .printStackTrace ();
79
+ }
71
80
}
72
81
}).start ();
73
82
}
@@ -85,7 +94,13 @@ public void enableDebug(boolean debug){
85
94
* @param o The object to be saved
86
95
*/
87
96
public boolean put (Object o ,long id ){
88
- if (mConfiguration ==null )readConfiguration (); //Ensure we have configuration file
97
+ if (mConfiguration ==null ){
98
+ try {
99
+ readConfiguration (); //Ensure we have configuration file
100
+ }catch (OutOfMemoryError error ){
101
+ return false ;
102
+ }
103
+ }
89
104
String classHash = mConfiguration .getClassHash (o .getClass ());
90
105
if (classHash ==null ){
91
106
classHash = mConfiguration .addClassHash (o .getClass ());
@@ -103,52 +118,62 @@ public boolean put(Object o,long id){
103
118
*/
104
119
public <T >List <T > getAll (Class <T > c ) throws OutOfMemoryError {
105
120
List <T > objects = new ArrayList <>();
106
- if (mConfiguration ==null )readConfiguration (); //Ensure we have configuration file
107
- final String classHash = mConfiguration .getClassHash (c );
108
- if (classHash ==null )return objects ;
109
- File filesDir = mContext .getFilesDir ();
110
- File [] objectFiles = filesDir .listFiles (new FilenameFilter () {
111
- @ Override
112
- public boolean accept (File dir , String name ) {
113
- return name .contains (classHash );
121
+ if (mConfiguration ==null ){
122
+ try {
123
+ readConfiguration (); //Ensure we have configuration file
124
+ }catch (OutOfMemoryError error ){
125
+ return objects ;
114
126
}
115
- });
116
-
117
- //sort object files by name
118
- //Bubble sort algorithm (sort by name) //TODO upgrade to a faster sort algorithm
119
- //Log.i(TAG,"Sort started");
120
-
121
- boolean sorted = false ;
122
- while (!sorted ) {
123
- boolean switched = false ;
124
- for (int i = 0 ; i < (objectFiles .length - 1 ); i ++) {
125
- File a = objectFiles [i ];
126
- File b = objectFiles [i +1 ];
127
-
128
- Long idAlong = Long .parseLong (a .getName ().split ("_" )[1 ]);
129
- Long idBlong = Long .parseLong (b .getName ().split ("_" )[1 ]);
130
-
131
- if (idBlong <idAlong ){
132
- objectFiles [i ] = b ;
133
- objectFiles [i +1 ] = a ;
134
- if (debug )Log .i (TAG , "BUBLESORT@" + i + " SWITCHED: " + idAlong + " & " + idBlong );
135
- switched = true ;
127
+ }
128
+ final String classHash = mConfiguration .getClassHash (c );
129
+ if (classHash ==null ){
130
+ return objects ;
131
+ }else {
132
+ File filesDir = mContext .getFilesDir ();
133
+ File [] objectFiles = filesDir .listFiles (new FilenameFilter () {
134
+ @ Override
135
+ public boolean accept (File dir , String name ) {
136
+ return name .contains (classHash );
137
+ }
138
+ });
139
+
140
+ //sort object files by name
141
+ //Bubble sort algorithm (sort by name) //TODO upgrade to a faster sort algorithm
142
+ //Log.i(TAG,"Sort started");
143
+
144
+ boolean sorted = false ;
145
+ while (!sorted ) {
146
+ boolean switched = false ;
147
+ for (int i = 0 ; i < (objectFiles .length - 1 ); i ++) {
148
+ File a = objectFiles [i ];
149
+ File b = objectFiles [i + 1 ];
150
+
151
+ Long idAlong = Long .parseLong (a .getName ().split ("_" )[1 ]);
152
+ Long idBlong = Long .parseLong (b .getName ().split ("_" )[1 ]);
153
+
154
+ if (idBlong < idAlong ) {
155
+ objectFiles [i ] = b ;
156
+ objectFiles [i + 1 ] = a ;
157
+ if (debug )
158
+ Log .i (TAG , "BUBLESORT@" + i + " SWITCHED: " + idAlong + " & " + idBlong );
159
+ switched = true ;
160
+ }
161
+ }
162
+ if (!switched ) {
163
+ sorted = true ;
136
164
}
137
165
}
138
- if (!switched ) {
139
- sorted = true ;
140
- }
141
- }
142
- //Log.i(TAG,"Sorted!");
166
+ //Log.i(TAG,"Sorted!");
143
167
144
- for (File f : objectFiles ) {
145
- if (f .isFile ()) {
146
- String objectJson = readObject (f .getName ());
147
- if (objectJson != null )
148
- objects .add (mGson .fromJson (objectJson , c ));
168
+ for (File f : objectFiles ) {
169
+ if (f .isFile ()) {
170
+ String objectJson = readObject (f .getName ());
171
+ if (objectJson != null )
172
+ objects .add (mGson .fromJson (objectJson , c ));
173
+ }
149
174
}
175
+ return objects ;
150
176
}
151
- return objects ;
152
177
}
153
178
154
179
/**
@@ -213,7 +238,13 @@ public boolean remove(final Class c,final long id) {
213
238
* @return true if all objects of c were removed
214
239
*/
215
240
public boolean removeAll (Class c ){
216
- if (mConfiguration ==null )readConfiguration (); //Ensure we have configuration file
241
+ if (mConfiguration ==null ){
242
+ try {
243
+ readConfiguration (); //Ensure we have configuration file
244
+ }catch (OutOfMemoryError error ){
245
+ return false ;
246
+ }
247
+ }
217
248
final String classHash = mConfiguration .getClassHash (c );
218
249
if (classHash ==null )return true ;
219
250
File filesDir = mContext .getFilesDir ();
@@ -249,7 +280,13 @@ public void run() {
249
280
250
281
@ Nullable
251
282
private String getObjectFileName (Class c , long id ){
252
- if (mConfiguration ==null )readConfiguration ();
283
+ if (mConfiguration ==null ){
284
+ try {
285
+ readConfiguration (); //Ensure we have configuration file
286
+ }catch (OutOfMemoryError error ){
287
+ return null ;
288
+ }
289
+ }
253
290
String classHash = mConfiguration .getClassHash (c );
254
291
if (classHash == null )return null ;
255
292
return classHash + "_" + String .valueOf (id );
@@ -265,7 +302,7 @@ public Configuration getConfiguration(){
265
302
/**
266
303
* Retrieves a saved Holocron configuration object
267
304
*/
268
- private void readConfiguration (){
305
+ private void readConfiguration () throws OutOfMemoryError {
269
306
File file = new File (mContext .getFilesDir ().getAbsolutePath ()+"/" +"cfg.j" );
270
307
String json ;
271
308
if (file .exists ()) {
0 commit comments