midp: persistant storage

Post on 26-May-2015

1.495 Views

Category:

Education

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

MIDP Development:Persistent Storage

Jussi PohjolainenTAMK University of Applied Sciences

Persistent Storage

• The nature of mobile device is different from PC:– All the actions done by the user will

be saved automatically. (Mobile Device works like pen and paper)

– There is no "Save as.." function.

• In MIDP-programming persistent storage is done by Record Store.

Record Store

MIDletMIDlet

MIDlet Suite MIDlet Suite

Device Persistent StorageRAM, Flash ROM, etc

MIDletMIDlet

MIDletMIDlet

MIDletMIDlet

Record StoreRecord Store Record StoreRecord StoreRecord Store Record StoreRecord Store

Handling Record Store

• Opening record store– public static RecordStore openRecordStore(String recordStoreName, boolean createIfNecessary) throws RecordStoreException, RecordStoreFullException, RecordStoreNotFoundException

• In Practice– RecordStore rs = RecordStore.openRecordStore(“Address”, true);

Closing and Deleting

• Closing– rs.closeRecordStore();

• To get all the Record Stores:– public static String[] listRecordStores();

• Deleting record store– rs.deleteRecordStore()

Record Store's other methods

• int getSize()– Returns the amount of space, in bytes, that the record store occupies.

• int getSizeAvailable()– Returns the amount of additional room (in bytes) available for

this record store to grow• int getVersion()

– Each time a record store is modified (by addRecord, setRecord, or deleteRecord methods) its version is incremented

• long getLastModified() – Returns the last time the record store was modified, in the

format used by System.currentTimeMillis().

RecordStore

int idint id byte [] databyte [] data

RecordStoreconsists of

Records

RecordStoreconsists of

Records

Reco

rdSt

ore

int idint id byte [] databyte [] data

int idint id byte [] databyte [] data

int idint id byte [] databyte [] data

Handling Records

• Adding Record to Record Store– public int addRecord(byte[] data, int offset, int numBytes) throws RecordStoreNotOpenException, RecordStoreException, RecordStoreFullException

• Example– String record = "Save this.."

– byte [] data = record.getBytes();

– int id = rs.addRecord(data, 0, data.length);

Getting the Record

• To get the record– public byte[] getRecord(int recordId) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException

• Example– byte[] retrieved = rs.getRecord(id);

Deleting and Replacing a Record

• Deleting– void deleteRecord(int id)

• Replacing– void setRecord(int id, byte[ ] newData, int offset, int numbytes);

Example// SavingRecordStore rs = RecordStore.openRecordStore(”name”, true);String t = “This will be saved”byte [] tb = t.getBytes();int id = rs.addRecord(tb, 0, tb.length); // Adds a Record// Replacingt = “Replace the old record”tb = t.getBytes();rs.setRecord(id, tb, 0, tb.length);// Gettingbyte [] fromrecord = rs.getRecord(id);String string = new String(fromrecord);// Closingrs.closeRecordStore();

What happens if addRecord fails?try{ RecordStore rs =

RecordStore.openRecordStore(”name”, true); String t = “This will be saved” byte [] tb = t.getBytes(); int id = rs.addRecord(tb, 0, tb.length); // Adds a

record rs.closeRecordStore();}catch(RecordStoreException e){

System.out.println(”Some problem...”)}

SolutionRecordStore rs = null;try{ rs = RecordStore.openRecordStore(”name”, true); String t = “This will be saved” byte [] tb = t.getBytes(); int id = rs.addRecord(tb, 0, tb.length); // Add's a record }catch(RecordStoreException e){

System.out.println(”Problem...”)}finally{

try{ if(rs != null) rs.closeRecordStore(); }

catch(Exception e){}}

Record Store Events

• It is possible to listen to Record Store Events. (If there is a change in Record Store)– addRecordListener(RecordListener listener)

• Methods in the interface– recordAdded()– recordChanged()– recordDeleted()

Retrieving results from the RecordStore: Enumeration

• RecordStore– public RecordEnumeration

enumerateRecords(RecordFilter filter, RecordComparator comparator, boolean keepUpdated)

• RecordFilter = What is retrieved from the Store

• RecordComparator = Order of the result

RecordFilter

class MyFilter implements RecordFilter{

public boolean matches(byte [] candidate){

if(candidate[0] == 7)

return true;

else

return false;

}

}

RecordComparatorclass MyComparator implements RecordComparator{

public int compare(byte [] rec1, byte[] rec2){

// Make comparing between two byte-arrays

if(...)

return PRECEDES; // rec1 on ennen rec2

else if(...)

return FOLLOWS; // rec2 on ennen rec1

else

return EQUIVALENT; // samat

}

}

Enumeration// Open RecordStore

// Create RecordFilter rf

// Create RecordComparator rc

RecordEnumeration re = rs.enumerateRecords(rf, rc, false);

while(re.hasNextElement()){

byte [] rb = re.nextRecord();

.

.

}

Can be also null!

Enumeration example

RecordStore rs = RecordStore.openRecordStore(rs_name, true);

RecordEnumeration re = rs.enumerateRecords(null, null, false);

while(re.hasNextElement()){byte [] x = re.nextRecord();String merkkijonoksi = new String(x);

// Do something}

// Deleting all the recordswhile(re.hasNextElement()){

int id = re.nextRecordId();rs.deleteRecord(id);

}

top related