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,491

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,491
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
    • 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.
  • 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
    • Opening record store
      • public static RecordStore openRecordStore(String recordStoreName, boolean createIfNecessary) throws RecordStoreException, RecordStoreFullException, RecordStoreNotFoundException
    • In Practice
      • RecordStore rs = RecordStore.openRecordStore(“Address”, true);
  • 5. Closing and Deleting
    • Closing
      • rs.closeRecordStore();
    • To get all the Record Stores:
      • public static String[] listRecordStores();
    • Deleting record store
      • rs.deleteRecordStore()
  • 6. 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().
  • 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
    • 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);
  • 9. Getting the Record
    • To get the record
      • public byte[] getRecord(int recordId) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException
    • Example
      • byte[] retrieved = rs.getRecord(id);
  • 10. Deleting and Replacing a Record
    • Deleting
      • void deleteRecord(int id)
    • Replacing
      • void setRecord(int id, byte[ ] newData, int offset, int numbytes);
  • 11. Example
    • // Saving
    • 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
    • // Replacing
    • t = “Replace the old record”
    • tb = t.getBytes();
    • rs.setRecord(id, tb, 0, tb.length);
    • // Getting
    • byte [] fromrecord = rs.getRecord(id);
    • String string = new String(fromrecord);
    • // Closing
    • rs.closeRecordStore();
  • 12. 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...”)
    • }
  • 13. Solution
    • RecordStore 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){}
    • }
  • 14. 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()
  • 15. 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
  • 16. RecordFilter
    • class MyFilter implements RecordFilter{
    • public boolean matches(byte [] candidate){
    • if(candidate[0] == 7)
    • return true;
    • else
    • return false;
    • }
    • }
  • 17. RecordComparator
    • class 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
    • }
    • }
  • 18. 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!
  • 19. 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 records
    • while(re.hasNextElement()){
    • int id = re.nextRecordId();
    • rs.deleteRecord(id);
    • }

×