The RMS is a persistent storage API in Java® ME.I found code in blackberry site and develop to store the name of record and value that need to store
This class creates object that holds data values
package rms;
import java.io.*;
public final class datastor
{
private String name;
private String value;
private String fullData;
private static final short NameD = 0;
private static final short valueD = 1;
public datastor(byte[] data) throws java.io.IOException
{
fromByteArray(data);
}
datastor(String name1, String value1)
{
name = name1;
value = value1;
fullData = name + ": " + value;
}
public String toString()
{
return fullData;
}
String getName()
{
return name;
}
String getvalue()
{
return value;
}
byte[] toByteArray() throws java.io.IOException
{
byte[] data;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeShort(NameD);
dos.writeUTF(name);
dos.writeShort(valueD);
dos.writeUTF(value);
data = baos.toByteArray();
return data;
}
private void fromByteArray(byte[] array) throws java.io.IOException
{
ByteArrayInputStream bais = new ByteArrayInputStream(array);
DataInputStream dis = new DataInputStream(bais);
short tag;
try
{
while(true)
{
tag = dis.readShort();
if (tag == NameD)
{
name = dis.readUTF();
}
else if (tag == valueD)
{
value = dis.readUTF();
}
else
{
dis.readUTF();
}
}
}
catch(EOFException e)
{
fullData = name + ": " + value;
}
}
}
This class use to perform the actions such as create delete data records in rms and also use to create datastore objecs
package rms;
import java.io.*;
import javax.microedition.rms.*;
public final class dataStorAcc
{
RecordStore _rs;
public dataStorAcc(String name) throws RecordStoreException, java.io.IOException
{
_rs = RecordStore.openRecordStore(name, true, RecordStore.AUTHMODE_ANY, false);
}
public dataStorAcc(String recordStoreName, String vendorName, String suiteName) throws RecordStoreException, java.io.IOException
{
_rs = RecordStore.openRecordStore( recordStoreName, vendorName, suiteName );
}
public synchronized int add(String name, String value) throws java.io.IOException, RecordStoreNotOpenException, RecordStoreException
{
datastor cd = new datastor(name, value);
//Creates object
byte[] data = cd.toByteArray();
//converts to byte
return _rs.addRecord(data, 0, data.length);
//add to record store
}
public synchronized void edit(int index, String artist, String title) throws java.io.IOException, RecordStoreNotOpenException, RecordStoreException
{
datastor cd = new datastor(artist, title);
byte[] data = cd.toByteArray();
_rs.setRecord(index, data, 0, data.length);
}
public datastor getDataobj(int recordID) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException, java.io.IOException
{
byte[] data = _rs.getRecord(recordID);
return new datastor(data);
}
public synchronized void delete(int recordId) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException
{
_rs.deleteRecord(recordId);
}
RecordEnumeration enumerate() throws RecordStoreNotOpenException
{
return _rs.enumerateRecords(null, null, true);
}
}
This is how we can use this within our application
01)Declare DataStorAcc object
dataStorAcc _db; // RMS reference
RecordEnumeration _enum;
_db = new dataStorAcc("My Music");//Creates new Datastore named My Music
_enum = _db.enumerate(); //Creates enumerator
02)Add new record to rms
_db.add(“name”,”value”);
03)Delete from rms
_db.delete(tecordID);
04)Retrive data
recordId = _enum.nextRecordId();
String data=getDataobj(recordId).toString();