Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CodenameOne/src/com/codename1/io/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static void setPreferencesLocation(String storageFileName) {
private synchronized static Hashtable<String, Object> get() {
if (p == null) {
if (Storage.getInstance().exists(preferencesLocation)) {
p = (Hashtable<String, Object>) Storage.getInstance().readObject(preferencesLocation);
p = (Hashtable<String, Object>) Storage.getInstance().readObject(preferencesLocation, false);
}
if (p == null) {
p = new Hashtable<String, Object>();
Expand All @@ -92,7 +92,7 @@ private synchronized static Hashtable<String, Object> get() {
}

private static synchronized void save() {
Storage.getInstance().writeObject(preferencesLocation, p);
Storage.getInstance().writeObject(preferencesLocation, p, false);
}

/**
Expand Down
51 changes: 44 additions & 7 deletions CodenameOne/src/com/codename1/io/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
* <p>Writes the given object to storage assuming it is an externalizable type
* or one of the supported types.</p>
*
* <p>
* The sample below demonstrates the usage and registration of the {@link com.codename1.io.Externalizable} interface:
* </p>
* <script src="https://gist.github.com/codenameone/858d8634e3cf1a82a1eb.js"></script>
*
* @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;
Expand All @@ -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);
Expand All @@ -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);
}

/**
* <p>Reads the object from the storage, returns null if the object isn't there</p>
* <p>
* The sample below demonstrates the usage and registration of the {@link com.codename1.io.Externalizable} interface:
* </p>
* <script src="https://gist.github.com/codenameone/858d8634e3cf1a82a1eb.js"></script>
*
* @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) {
Expand All @@ -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;
Expand Down
Loading