1 / 23

Persistent Storage

Explore mini-databases, RecordStores, RMS vs. FileConnection API, file and record management, personal information management, and more in this comprehensive mobile data storage guide.

arnoldjones
Download Presentation

Persistent Storage

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Persistent Storage • Record Stores • mini databases – represent data as byte records • Record Management System (RMS) API • Files and Directories • FileConnection API • Contacts and Calendars • Personal Information Management (PIM) API

  2. RMS vs FileConnection API • FileConnection • familiar (simpler) to use • provides access to images, sounds, etc. • needs security privileges • may not be supported on older devices

  3. RMS vs FileConnection API • FileConnection • familiar (simpler) to use • provides access to images, sounds, etc. • needs security privileges • may not be supported on older devices • RMS • available on all MIDP devices • does not require security privileges • may be more appropriate for some applications • more cumbersome to use

  4. RecordStores • Mini databases that consist of a collection of records (byte arrays) • Could be shared across MIDlets

  5. RecordStores • Mini databases that consist of a collection of records (byte arrays) • Could be shared across MIDlets • Creating Record Stores (via static methods) RecordStore openRecordStore(String name, boolean create) RecordStore openRecordStore(String name, boolean create, int auth, boolean writable) auth = AUTHMODE_ANY – accessible by all midlets AUTHMODE_PRIVATE – restricted to this midlet

  6. RecordStores • Mini databases that consist of a collection of records (byte arrays) • Could be shared across MIDlets • Creating Record Stores (via static methods) RecordStore openRecordStore(String name, boolean create) RecordStore openRecordStore(String name, boolean create, int auth, boolean writable) auth = AUTHMODE_ANY – accessible by all midlets AUTHMODE_PRIVATE – restricted to this midlet • Manipulating Record Stores int addRecord(byte[] data, int offset, int numBytes) void deleteRecord(int recordID) byte[] getRecord(int recordID) void setRecord(int recordID, byte[] data, int offset, int numBytes) int getNumRecords() int getRecordSize(int recordID)

  7. Writing to RecordStore void saveRecords(String rsName) { RecordStore rs = RecordStore.openRecordStore(rsName, true); String[] data = {“Mickey”, “Minnie”, “Donald”, “Tom”, “Jerry”}; for (int i = 0; i < data.length; ++i) { byte[] bytes = data[i].getBytes(); rs.addRecord(bytes, 0, bytes.length); } rs.closeRecordStore(); } // NOTE: The RecordStore methods throw exceptions – put try/catch.

  8. Reading from RecordStore void readRecords(String rsName) { RecordStore rs = RecordStore.openRecordStore(rsName, true); for (int i = 1; i <= rs.getNumRecords(); ++i) { byte[] record = rs.getRecord(i); String name = new String(record); mainScreen.append(name); } rs.closeRecordStore(); } // NOTE: The RecordStore methods throw exceptions – put try/catch. // Typically will use RecordEnumeration to traverse

  9. Enumerating Records • RecordEnumeration – an interface for sequential traversal of a RecordStore interface RecordEnumeration { bool hasNextElement(); bool hasPreviousElement(); byte[] nextRecord(); byte[] previousRecord(); int numRecords(); ... ... ... }

  10. Enumerating Records • RecordEnumeration – an interface for sequential traversal of a RecordStore interface RecordEnumeration { bool hasNextElement(); bool hasPreviousElement(); byte[] nextRecord(); byte[] previousRecord(); int numRecords(); ... ... ... } • To get an enumerator for a RecordStore: RecordEnumeration enumerateRecords(RecordFilter f, RecordComparator c, boolen keepUpdated); selection criterion ordering criterion

  11. Enumerating Records void processRecords(String rsName) { RecordStore rs = RecordStore.openRecordStore(rsName, true); RecordEnumeration records = rs.enumerateRecords(null, null, true); while (records.hasNextElement()) { byte[] rawData = records.nextRecord(); String name = new String(rawData); mainScreen.append(name); } rs.closeRecordStore(); }

  12. RecordFilter Interface • Use to retrieve only those records that match a certain criterion interface RecordFilter { boolean matches(byte[] record); } return true iff record satisfies the criterion

  13. RecordFilter Interface • Use to retrieve only those records that match a certain criterion interface RecordFilter { boolean matches(byte[] record); } • Filter for records that start with CompSci: class CompSciFilter implements RecordFilter { boolean matches(byte[] record) { String data = new String(record); return data.startsWith(“CompSci”); } } return true iff record satisfies the criterion

  14. RecordComparator Interface • Use to create an ordering criterion for retrieving the records interface RecordComparator { int compare(byte[] r1, byte[] r2) } return FOLLOWSif r1 comes after r2 in sorted order PRECEDES if r1 comes before r2 in sorted order EQUIVALENTif r1 and r2 are considered equal

  15. RecordComparator Interface • Retrieve by decreasing size class DescSizeSorter implements RecordComparator { boolean compare(byte[] r1, byte[] r2) { String s1 = new String(r1); String s2 = new String(r2); if (s1.size() > s2.size()) { return ??? } else if (s1.size() < s2.size()) { return ??? } else { return ??? } }

  16. Enumerating Records • Traverse only CompSci records in decreasing order of length void processRecords(String rsName) { RecordStore rs = RecordStore.openRecordStore(rsName, true); CompSciFilter filter = new CompSciFilter(); DecSizeSorter order = new DecSizeSorter(); RecordEnumeration records = rs.enumerateRecords(filter, order, true); while (records.hasNextElement()) { byte[] rawData = records.nextRecord(); String name = new String(rawData); mainScreen.append(name); } rs.closeRecordStore(); }

  17. Input/Output Streams • An abstract representation of stream of data • ByteArrayInputStream/ByteArrayOutputStream --- abstraction for reading from / writing to a byte array OutputStream InputStream ByteArray OutputStream Data OutputStream ByteArray InputStream Data InputStream

  18. ByteArrayInput/Output Streams • Provide a mechanism for reading from / writing to a byte array (only bytes) • Allow the buffer to grow as needed (on writing to) • Selected methods (ByteArrayInput): int size() – current size of buffer void write(int b) – write a byte to the buffer void write(byte[] b, int off, int len) – write len bytes starting off byte[] toByteArray() – return the buffer in a byte array • Example: ByteArrayOutputStream os = new ByteArrayOutputStream(); os.write(‘C’); os.write(‘S’); os.write(1); byte[] data = os.toByteArray();

  19. DataInput/Output Stream • Provide convenient mechanism for reading from / writing to a stream (supports all of the primitive types) • Selected methods (DataOutputStream): DataOutputStream(OutputStream os) void writeInt(int v) void writeDouble(double v) void writeUTF(String s) -- write machine-independentt encoding of v void flush() -- forces any written values to be written to stream • Selected methods (DataInputStream): DataInputStream(InputStream os) int readInt() double readDouble() String readUTF() long skip(long n) – skip n bytes (return how many actually skipped) int available() – how many bytes can be read without waiting

  20. DataInput/Output Stream • Example (writing) ByteArrayOutputStream os = new ByteArrayOutputStream(); DataOutputStream output = new DataOutputStream(os); output.writeUTF(‘Comp Sci’); output.writeInt(391); output.writeDouble(4.3); byte[] = os.toByteArray(); • Example (reading) byte[] data = ... // data stored in somehow ByteArrayInputStream is = new ByteArrayInputStream(data); DataInputStream input = new DataOutputStream(is); String name = input.readUTF(); int code = input.readInt(); double maxgrade = input.readDouble(4.3);

  21. DataInput/Output Stream • Example (writing) ByteArrayOutputStream os = new ByteArrayOutputStream(); DataOutputStream output = new DataOutputStream(os); output.writeUTF(‘Comp Sci’); output.writeInt(391); output.writeDouble(4.3); byte[] = os.toByteArray(); • Example (reading) byte[] data = ... // data stored in somehow ByteArrayInputStream is = new ByteArrayInputStream(data); DataInputStream input = new DataOutputStream(is); String name = input.readUTF(); int code = input.readInt(); double maxgrade = input.readDouble(4.3);

  22. Files and Directories • Generic Connection Framework (Connections and Connector classes) • Connector TypeConnection = (TypeConnection) Connector.open(url) Connection void close() Input Connection Output Connection DataInputStream openDataInputStream() DataOutputStream openDataOutputStream() OutputStream openOutputStream() InputStream openInputStream() FileConnection SocketConnection HttpConnection int getPort() String getHost() String getURL() String getResponseMessage() int getPort() String getAddress() String getLocalAddress()

  23. File Manipulation • Example (writing) FileConnection fc = (FileConnection) Connector.open( filename ); DataOutputStream output = fc.getDataOutputStream(); output.writeInt(2); output.writeUTF(“CS 112”); output.writeUTF(“CS 391”); output.close(); fc.close(); • Example (reading) FileConnection fc = (FileConnection) Connector.open( filename ); DataInputStream input = fc.getDataInputStream(); int numRecords = input.readInt(); String name1 = input.readUTF(); String name2 = input.readUTF(); output.close(); fc.close();

More Related