diff --git a/CodenameOne/src/com/codename1/io/Preferences.java b/CodenameOne/src/com/codename1/io/Preferences.java index 0645884840..d7c6c4c985 100644 --- a/CodenameOne/src/com/codename1/io/Preferences.java +++ b/CodenameOne/src/com/codename1/io/Preferences.java @@ -82,7 +82,7 @@ public static void setPreferencesLocation(String storageFileName) { private synchronized static Hashtable get() { if (p == null) { if (Storage.getInstance().exists(preferencesLocation)) { - p = (Hashtable) Storage.getInstance().readObject(preferencesLocation); + p = (Hashtable) Storage.getInstance().readObject(preferencesLocation, false); } if (p == null) { p = new Hashtable(); @@ -92,7 +92,7 @@ private synchronized static Hashtable get() { } private static synchronized void save() { - Storage.getInstance().writeObject(preferencesLocation, p); + Storage.getInstance().writeObject(preferencesLocation, p, false); } /** diff --git a/CodenameOne/src/com/codename1/io/Storage.java b/CodenameOne/src/com/codename1/io/Storage.java index b65e71dc63..7cfe222d08 100644 --- a/CodenameOne/src/com/codename1/io/Storage.java +++ b/CodenameOne/src/com/codename1/io/Storage.java @@ -233,6 +233,24 @@ public int entrySize(String name) { * @return true for success, false for failure */ public boolean writeObject(String name, Object o) { + return writeObject(name, o, true); + } + + /** + *

Writes the given object to storage assuming it is an externalizable type + * or one of the supported types.

+ * + *

+ * The sample below demonstrates the usage and registration of the {@link com.codename1.io.Externalizable} interface: + *

+ * + * + * @param name store name + * @param o object to store + * @param includeLogging During app initialization, the logging on error might impact the apps stability + * @return true for success, false for failure + */ + public boolean writeObject(String name, Object o, boolean includeLogging) { name = fixFileName(name); cache.put(name, o); DataOutputStream d = null; @@ -242,9 +260,11 @@ public boolean writeObject(String name, Object o) { d.close(); return true; } catch (Exception err) { - Log.e(err); - if (Log.isCrashBound()) { - Log.sendLog(); + if(includeLogging) { + Log.e(err); + if (Log.isCrashBound()) { + Log.sendLog(); + } } Util.getImplementation().deleteStorageFile(name); Util.getImplementation().cleanup(d); @@ -263,6 +283,21 @@ public boolean writeObject(String name, Object o) { * @return object stored under that name */ public Object readObject(String name) { + return readObject(name, true); + } + + /** + *

Reads the object from the storage, returns null if the object isn't there

+ *

+ * The sample below demonstrates the usage and registration of the {@link com.codename1.io.Externalizable} interface: + *

+ * + * + * @param name name of the store + * @param includeLogging During app initialization, the logging on error might impact the apps stability + * @return object stored under that name + */ + public Object readObject(String name, boolean includeLogging) { name = fixFileName(name); Object o = cache.get(name); if (o != null) { @@ -279,10 +314,12 @@ public Object readObject(String name) { cache.put(name, o); return o; } catch (Throwable err) { - Log.p("Error while reading: " + name); - Log.e(err); - if (Log.isCrashBound()) { - Log.sendLog(); + if(includeLogging) { + Log.p("Error while reading: " + name); + Log.e(err); + if (Log.isCrashBound()) { + Log.sendLog(); + } } Util.getImplementation().cleanup(d); return null;