The PersistentStorage MIDlet implements four different RMS usage approaches:
Basic approach, where the record store is kept open between the get operations
Bad approach, where the record store is inefficiently opened with each read operation
Caching approach, which is an extension of the basic approach, where the get operation is performed only once and its results are cached
File approach, where the record store is implemented with operations similar to using files
To implement the basic RMS usage approach:
Create the BasicRms
class and set it to implement the RmsConnection
interface.
import javax.microedition.rms.RecordStore; import javax.microedition.rms.RecordStoreException; public class BasicRms implements RmsConnection { private String rmsName = "BasicRms"; RecordStore rms;
Use the open
and close
methods to open and close
the record store, and the get
and set
methods to handle the record store operations.
public void open() { try { rms = RecordStore.openRecordStore(rmsName, true); } catch(RecordStoreException rse) { rse.printStackTrace(); } } public byte[] get(){ byte[] data = null; try { data = rms.getRecord(1); } catch(RecordStoreException rse) { rse.printStackTrace(); } return data; } public void set(byte[] data) { try { rms.addRecord(data, 0, data.length); } catch(RecordStoreException rse) { rse.printStackTrace(); } } public void close() { try { rms.closeRecordStore(); } catch(RecordStoreException rse) { //rse.printStackTrace(); } } }
To implement the bad RMS usage approach:
Create the BadRms
class and set it to implement the RmsConnection
interface.
import javax.microedition.rms.RecordStore; import javax.microedition.rms.RecordStoreException; public class BadRms implements RmsConnection { private String rmsName = "BadRms";
Use the open
and close
methods to open and close
the record store, and the get
and set
methods to handle the record store operations.
The get
and set
methods are inefficient
since they open and close the record store during each operation.
public void open() { } public byte[] get(){ byte[] data = null; try { RecordStore r = RecordStore.openRecordStore(rmsName, true); data = r.getRecord(1); r.closeRecordStore(); } catch(RecordStoreException rse) { rse.printStackTrace(); } return data; } public void set(byte[] data) { try { RecordStore r = RecordStore.openRecordStore(rmsName, true); r.addRecord(data, 0, data.length); r.closeRecordStore(); } catch(RecordStoreException rse) { rse.printStackTrace(); } } public void close() { } }
To implement the caching RMS usage approach:
Create the CacheRms
class and set it to extend the BasicRms
class.
public class CacheRms extends BasicRms { byte[] cachedData;
Use the open
method to cache data. In the get
method,
retrieve the data from the cache instead of the record store.
public void open() { super.open(); cachedData = super.get(); } public byte[] get() { return cachedData; } }
To implement the file RMS usage approach:
Create the FileRms
class and set it to implement the RmsConnection
interface.
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.microedition.io.Connector; import javax.microedition.io.file.FileConnection; public class FileRms implements RmsConnection { private String rootDir; private String rmsName = "FileRms"; FileConnection fc; public FileRms() { rootDir = System.getProperty("fileconn.dir.private"); }
Handle the record
store operations as you would handle file operations. Note that you
need the delete
method in addition to the normal open
, close
, get
, and set
methods.
public void open() { try { fc = (FileConnection) Connector.open(rootDir + rmsName); if (!fc.exists()) { fc.create(); } } catch(IOException e) { e.printStackTrace(); } } public byte[] get() { byte[] data = null; try { InputStream is = fc.openInputStream(); int len = (int)fc.fileSize(); data = new byte[len]; int readBytes = is.read(data); if (readBytes != len) { System.out.println("read size mismatch"); } is.close(); } catch(IOException e) { e.printStackTrace(); } return data; } public void set(byte[] data) { try { OutputStream os = fc.openOutputStream(); os.write(data); os.close(); } catch(IOException e) { e.printStackTrace(); } } public void close() { try { fc.close(); } catch (IOException e) { //e.printStackTrace(); } catch (NullPointerException e) { //e.printStackTrace(); } } public void delete() { try { FileConnection file = (FileConnection) Connector.open(rootDir + rmsName); if (file.exists()) { file.delete(); } file.close(); } catch(IOException e) { e.printStackTrace(); } } }