The Record Management System (RMS) provides a mechanism through which MIDlets can persistently store data and retrieve it later. In a record-oriented approach, J2ME RMS comprises multiple record stores. Each record store can be visualized as a collection of records, which will remain persistent across multiple invocations of the MIDlet. The device platform is responsible for maintaining the integrity of the record stores.
A record store is created in platform-dependent locations, like nonvolatile device memory, which are not directly exposed to the MIDlets. The RMS classes call into the platform-specific native code to perform the actual database operations. Record store implementations ensure that all individual record store operations are atomic, synchronous, and serialized, so no corruption of data will occur with multiple accesses. The record store is timestamped to denote the last time it was modified. It also maintains a version, which is an integer that is incremented for each operation that modifies the contents of the record store. Versions and timestamps are useful for synchronization purposes.
The javax.microedition.rms.RecordStore class represents a RMS record store. Each record in a record store is an array of bytes and has a unique integer identifier. The openRecordStore() is used to open a record store. The record store should be opened to perform any operation in it. Sample code for opening the store is given below.
RecordStore store = RecordStore.openRecordStore(recordStoreName, true );
The second parameter determines whether to create the record store, if it does not exist.
Once all operations are done, the closeRecordStore() is called to close the record store. Also, the deleteRecordStore() can be used to delete the record store. This function accepts the name of the store as the parameter.
Sample code for inserting a record to the record store is given below:
public void createRecord(String data) throws RecordStoreException,
RecordStoreFullException,
RecordStoreNotFoundException,
IOException
{
ByteArrayOutputStream bout = null;
DataOutputStream dout = null;
RecordStore store = null;
try
{
store = RecordStore.openRecordStore("RECORD_STORE_NAME", true );
bout = new ByteArrayOutputStream();
dout = new DataOutputStream( bout );
dout.writeUTF( data );
dout.flush();
int numBytes = bout.size();
byte[] record = bout.getByteArray();
store.addRecord(record, 0, numBytes);
}
finally
{
try
{
if ( bout != null)
bout.close();
if (dout != null)
dout.close();
if (store != null)
store.closeRecordStore();
}
catch (Exception ex)
{
/* Do Nothing */
}
}
}
Sample function for reading a record from the record store is given below. Each record in the record store is uniquely identified by a record number. So the calling function should have the logic for determining the record id.
public String getRecord(int recordID) throws RecordStoreNotOpenException,
InvalidRecordIDException,
RecordStoreException,
IOException,
Exception
{
ByteArrayInputStream bin = null;
DataInputStream din = null;
RecordStore store = null;
String record = "";
try
{
store = RecordStore.openRecordStore("RECORD_STORE_NAME", false );
byte[] rawData = store.getRecord(recordId);
bin = new ByteArrayInputStream( rawData );
din = new DataInputStream( bin );
record = din.readUTF();
}
finally
{
try
{
if (bin != null)
bin.close();
if (din != null)
din.close();
if (store != null)
store.closeRecordStore();
}
catch (Exception ex)
{
/* Do Nothing */
}
}
return record;
}
Updating a particular record involves getting a handle for that record and setting new information. The setRecord() is used to update the record, which accepts the record id as one of its parameters. Similarly, the deleteRecord() is used to delete a record from the record store. This function also accepts the record id as the parameter.
One point to be noted is; since the memory available in mobile devices is limited, there is strict limitation for storage. The application should have proper logic for cleanup and data management, wherever necessary.
No comments:
Post a Comment