Record management in J2ME MIDP

MIDP doesn’t use filesystem to save application data; in turn it uses a non-volatile memory using a system called Record Management System

The MIDP Record Management System (RMS) provides a means to store application data that persists across invocations of a MIDlet. You can visualize the RMS record store as a very simple database, where each row consists of two columns: one containing a unique row identifier, the other a series of bytes representing the data in the record.

Record ID Data
1 Array of bytes
2 Array of bytes
3 Array of bytes

The unique row identifier is an integer value. The first entry will have the ID of 1, the next of 2, and so on. Row identifiers are not re-used if a row is deleted. That is, if we have three rows in a table with the IDs 1, 2, and 3, deleting ID 2 will remove this identifier permanently from the record store. If we were to add another row to this table, it would have an identifier of 4.

Record stores are identified by name. A name may consist of up to 32 characters, and all characters are case sensitive. No two record stores within a MIDlet suite (that is, a collection of one or more MIDlets packaged together) may contain the same name.

Each record store has a version number as well as a date/time stamp. Both values are updated whenever a record is added, replaced, or deleted.

Record management system provides a file system like environment that is used to store and maintain persistence in a small computing device. RMS is a combination of file system and Database management system that enables you to store data in columns and rows similar to the organization of data in a table of database.

The operations which can be performed in RMS are you can insert records, read records, search for a particular records and sort records stored in RMS.

Creating/Opening a Record Store:

openRecordStore() is called to create and open an existing record store. Accepts 2 parameters first parameter is a string-containing name of the record store, second parameter is a Boolean value (true) causes the record store to open if the record store already exists or else it will create.

RecordStore rs = RecordStore.openRecordStore (“MyAppointments”, true);

Here “MyAppointments”is the Record store name

Inserting records:

The MIDlet invokes the addRecord() method of javax.microedition.rms.RecordStore class to insert a new record into the record store. This is a blocking atomic operation and returns the recordId for the new record. The record is written to persistent storage before the method returns.

public int addRecord(byte[] data, int offset, int numBytes) inserts a record represented by an array of bytes data with offset as its starting index and numBytes as its length.

String appt = “new record”;

byte bytes[] = appt.getBytes();

rs.addRecord(bytes,0,bytes.length);

Updating Records in a Record Store:

Updating a particular record involves getting a handle for that record and setting new information.

public int getRecord(int recordId, byte[] buffer, int offset) returns the data stored in the given record in the byte array represented by buffer. public byte[] getRecord(int recorded) returns a copy of the data represented by recordId. public void setRecord(int recordId, byte[] newData, int offset, int numBytes) sets new information, a stream of bytes (newData) with offset as its starting index and numBytes as its length, at the record location represented by recordId.

String newappt = “update record”;

Byte data = newappt.getBytes();

Rs.setRecord(1, data, 0, data.length());

Closing a record store:

Internal resources are utilized to make an open record store available to MIDlets with in a MIDlet suite. You can reuse the resources utilized by the record store by closing the record store, which you don’t need closeRecordStore() method is used to close the record store.

Rs.closeRecordStore();

Deleting a Record Store:

A record store can be deleted by using deleteRecordStore(String). It requires one parameter which is a string containing the name of the record store.

RecordStore. deleteRecordStore(“MyAppointments”);

Writing Mixed Datatypes Into A RecordStore:

Private RecordStore recordstore=null;

Try

{

byte [] outputRecord;

String outputString=”First Record”;

int outputInteger= 15;

Boolean outputBooelan=true;

ByteOutputStream outputStream=new ByteOutputStream ();

DataOutputStream outputDataStream=new DataOutputStream(outputStream);

outputDataStream.writeUTF (outputString);

outputDataStream.writeUTF(outputInteger);

outputDataStream.writeUTF(outputBooelan);

outputDataStream.flush();

outputRecord= outputDataStream.toByteArray ();

recordStore.addRecord (outputRecord,0,outputRecord.length);

outputStream.close();

OutputDataStream.close ();

}

catch (Exception error)

{

// handle proper exceptions…

}

Reading Mixed Datatypes From A RecordStore:

Try

{

String inputString=null;

int inputInteger= 0;

boolean inputBooelan=false;

byte [] byteInputData= new byte[100];

ByteArrayInputStream inputStream=new ByteArrayInputStream(byteInputData);

DataInputStream inputDataStream=new DataInputStream (inputStream);

for ( int i=0;recordstore.getnumRecords(); i++)

{

recordstore.getRecord (i, byteInputData, 0);

inputString=inputDataStream.readUTF();

inputBooelan= inputDataStream.readBoolean();

inputInteger = inputDataStream.readInt();

inputStream.reset();

}

inputStream.close();

inputDataStream.close();

}

catch (Exception error)

{

// handle proper exceptions…

}

Leave a Reply