Your SlideShare is downloading. ×
0
MIDP: Persistant Storage
MIDP: Persistant Storage
MIDP: Persistant Storage
MIDP: Persistant Storage
MIDP: Persistant Storage
MIDP: Persistant Storage
MIDP: Persistant Storage
MIDP: Persistant Storage
MIDP: Persistant Storage
MIDP: Persistant Storage
MIDP: Persistant Storage
MIDP: Persistant Storage
MIDP: Persistant Storage
MIDP: Persistant Storage
MIDP: Persistant Storage
MIDP: Persistant Storage
MIDP: Persistant Storage
MIDP: Persistant Storage
MIDP: Persistant Storage
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

MIDP: Persistant Storage

1,500

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,500
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
38
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. MIDP Development: Persistent Storage Jussi Pohjolainen TAMK University of Applied Sciences
  • 2. Persistent Storage <ul><li>The nature of mobile device is different from PC: </li></ul><ul><ul><li>All the actions done by the user will be saved automatically. (Mobile Device works like pen and paper) </li></ul></ul><ul><ul><li>There is no &quot;Save as..&quot; function. </li></ul></ul><ul><li>In MIDP-programming persistent storage is done by Record Store. </li></ul>
  • 3. Record Store MIDlet MIDlet Suite MIDlet Suite Device Persistent Storage RAM, Flash ROM, etc MIDlet MIDlet MIDlet Record Store Record Store Record Store Record Store
  • 4. Handling Record Store <ul><li>Opening record store </li></ul><ul><ul><li>public static RecordStore openRecordStore(String recordStoreName, boolean createIfNecessary) throws RecordStoreException, RecordStoreFullException, RecordStoreNotFoundException </li></ul></ul><ul><li>In Practice </li></ul><ul><ul><li>RecordStore rs = RecordStore.openRecordStore(“Address”, true); </li></ul></ul>
  • 5. Closing and Deleting <ul><li>Closing </li></ul><ul><ul><li>rs.closeRecordStore(); </li></ul></ul><ul><li>To get all the Record Stores: </li></ul><ul><ul><li>public static String[] listRecordStores(); </li></ul></ul><ul><li>Deleting record store </li></ul><ul><ul><li>rs.deleteRecordStore() </li></ul></ul>
  • 6. Record Store's other methods <ul><li>int getSize() </li></ul><ul><ul><li>Returns the amount of space, in bytes, that the record store occupies. </li></ul></ul><ul><li>int getSizeAvailable() </li></ul><ul><ul><li>Returns the amount of additional room (in bytes) available for this record store to grow </li></ul></ul><ul><li>int getVersion() </li></ul><ul><ul><li>Each time a record store is modified (by addRecord, setRecord, or deleteRecord methods) its version is incremented </li></ul></ul><ul><li>long getLastModified() </li></ul><ul><ul><li>Returns the last time the record store was modified, in the format used by System.currentTimeMillis(). </li></ul></ul>
  • 7. RecordStore int id byte [] data RecordStore consists of Records RecordStore int id byte [] data int id byte [] data int id byte [] data
  • 8. Handling Records <ul><li>Adding Record to Record Store </li></ul><ul><ul><li>public int addRecord(byte[] data, int offset, int numBytes) throws RecordStoreNotOpenException, RecordStoreException, RecordStoreFullException </li></ul></ul><ul><li>Example </li></ul><ul><ul><li>String record = &quot;Save this..&quot; </li></ul></ul><ul><ul><li>byte [] data = record.getBytes(); </li></ul></ul><ul><ul><li>int id = rs.addRecord(data, 0, data.length); </li></ul></ul>
  • 9. Getting the Record <ul><li>To get the record </li></ul><ul><ul><li>public byte[] getRecord(int recordId) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException </li></ul></ul><ul><li>Example </li></ul><ul><ul><li>byte[] retrieved = rs.getRecord(id); </li></ul></ul>
  • 10. Deleting and Replacing a Record <ul><li>Deleting </li></ul><ul><ul><li>void deleteRecord(int id) </li></ul></ul><ul><li>Replacing </li></ul><ul><ul><li>void setRecord(int id, byte[ ] newData, int offset, int numbytes); </li></ul></ul>
  • 11. Example <ul><li>// Saving </li></ul><ul><li>RecordStore rs = RecordStore.openRecordStore(”name”, true); </li></ul><ul><li>String t = “This will be saved” </li></ul><ul><li>byte [] tb = t.getBytes(); </li></ul><ul><li>int id = rs.addRecord(tb, 0, tb.length); // Adds a Record </li></ul><ul><li>// Replacing </li></ul><ul><li>t = “Replace the old record” </li></ul><ul><li>tb = t.getBytes(); </li></ul><ul><li>rs.setRecord(id, tb, 0, tb.length); </li></ul><ul><li>// Getting </li></ul><ul><li>byte [] fromrecord = rs.getRecord(id); </li></ul><ul><li>String string = new String(fromrecord); </li></ul><ul><li>// Closing </li></ul><ul><li>rs.closeRecordStore(); </li></ul>
  • 12. What happens if addRecord fails? <ul><li>try{ </li></ul><ul><li>RecordStore rs = RecordStore.openRecordStore(”name”, true); </li></ul><ul><li>String t = “This will be saved” </li></ul><ul><li>byte [] tb = t.getBytes(); </li></ul><ul><li>int id = rs.addRecord(tb, 0, tb.length); // Adds a record </li></ul><ul><li>rs.closeRecordStore(); </li></ul><ul><li>} </li></ul><ul><li>catch(RecordStoreException e){ </li></ul><ul><li>System.out.println(”Some problem...”) </li></ul><ul><li>} </li></ul>
  • 13. Solution <ul><li>RecordStore rs = null; </li></ul><ul><li>try{ </li></ul><ul><li>rs = RecordStore.openRecordStore(”name”, true); </li></ul><ul><li>String t = “This will be saved” </li></ul><ul><li>byte [] tb = t.getBytes(); </li></ul><ul><li>int id = rs.addRecord(tb, 0, tb.length); // Add's a record </li></ul><ul><li>} </li></ul><ul><li>catch(RecordStoreException e){ </li></ul><ul><li>System.out.println(”Problem...”) </li></ul><ul><li>} </li></ul><ul><li>finally{ </li></ul><ul><li>try{ </li></ul><ul><li> if(rs != null) </li></ul><ul><li> rs.closeRecordStore(); </li></ul><ul><li>} </li></ul><ul><li>catch(Exception e){} </li></ul><ul><li>} </li></ul>
  • 14. Record Store Events <ul><li>It is possible to listen to Record Store Events. (If there is a change in Record Store) </li></ul><ul><ul><li>addRecordListener(RecordListener listener) </li></ul></ul><ul><li>Methods in the interface </li></ul><ul><ul><li>recordAdded() </li></ul></ul><ul><ul><li>recordChanged() </li></ul></ul><ul><ul><li>recordDeleted() </li></ul></ul>
  • 15. Retrieving results from the RecordStore: Enumeration <ul><li>RecordStore </li></ul><ul><ul><li>public RecordEnumeration enumerateRecords(RecordFilter filter, RecordComparator comparator, boolean keepUpdated) </li></ul></ul><ul><li>RecordFilter = What is retrieved from the Store </li></ul><ul><li>RecordComparator = Order of the result </li></ul>
  • 16. RecordFilter <ul><li>class MyFilter implements RecordFilter{ </li></ul><ul><li>public boolean matches(byte [] candidate){ </li></ul><ul><li>if(candidate[0] == 7) </li></ul><ul><li>return true; </li></ul><ul><li>else </li></ul><ul><li>return false; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  • 17. RecordComparator <ul><li>class MyComparator implements RecordComparator{ </li></ul><ul><li>public int compare(byte [] rec1, byte[] rec2){ </li></ul><ul><li>// Make comparing between two byte-arrays </li></ul><ul><li>if(...) </li></ul><ul><li> return PRECEDES; // rec1 on ennen rec2 </li></ul><ul><li>else if(...) </li></ul><ul><li> return FOLLOWS; // rec2 on ennen rec1 </li></ul><ul><li>else </li></ul><ul><li> return EQUIVALENT; // samat </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  • 18. Enumeration <ul><li>// Open RecordStore </li></ul><ul><li>// Create RecordFilter rf </li></ul><ul><li>// Create RecordComparator rc </li></ul><ul><li>RecordEnumeration re = rs.enumerateRecords(rf, rc, false); </li></ul><ul><li>while(re.hasNextElement()){ </li></ul><ul><li>byte [] rb = re.nextRecord(); </li></ul><ul><li>. </li></ul><ul><li>. </li></ul><ul><li>} </li></ul>Can be also null!
  • 19. Enumeration example <ul><li>RecordStore rs = RecordStore.openRecordStore(rs_name, true); </li></ul><ul><li>RecordEnumeration re = rs.enumerateRecords(null, null, false); </li></ul><ul><li>while(re.hasNextElement()){ </li></ul><ul><li>byte [] x = re.nextRecord(); </li></ul><ul><li>String merkkijonoksi = new String(x); </li></ul><ul><li>// Do something </li></ul><ul><li>} </li></ul><ul><li>// Deleting all the records </li></ul><ul><li>while(re.hasNextElement()){ </li></ul><ul><li>int id = re.nextRecordId(); </li></ul><ul><li>rs.deleteRecord(id); </li></ul><ul><li>} </li></ul>

×