Skip to content

Instantly share code, notes, and snippets.

@criccomini
Created September 23, 2012 22:47
Show Gist options
  • Save criccomini/3773323 to your computer and use it in GitHub Desktop.
Save criccomini/3773323 to your computer and use it in GitHub Desktop.
BlackBerry Peristent Store
import net.rim.device.api.crypto.SHA1Digest;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.CodeSigningKey;
import net.rim.device.api.system.ControlledAccess;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.UiEngine;
import net.rim.device.api.ui.component.Dialog;
public class PersistentStore {
private CodeSigningKey codeSigningKey;
public PersistentStore() {
codeSigningKey = CodeSigningKey.get("ACME");
}
public void setObject(String keyString, Object val) {
long key = getLongKey(keyString);
PersistentObject pObject = PersistentStore.getPersistentObject(key);
pObject.setContents(new ControlledAccess(val, codeSigningKey));
pObject.commit();
}
public Object getObject(String keyString) {
Object object;
long key = getLongKey(keyString);
PersistentObject pObject = PersistentStore.getPersistentObject(key);
object = pObject.getContents(codeSigningKey);
return object;
}
private long getLongKey(String key) {
SHA1Digest sha1Digest = new SHA1Digest();
sha1Digest.update(key.getBytes());
byte[] hashValBytes = sha1Digest.getDigest();
long hashValLong = 0;
for(int i = 0; i < 8; ++i) {
hashValLong |= ((long)(hashValBytes[i]) & 0x0FF) << (8*i);
}
return hashValLong;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment