Record Management System is a subsystem of the MIDP, providing the persistent storage function of the data. This article is not based on the basics of Record Management System, but is proposed to efficiently use the Record Management System for the programming perspective. If you are not enough for RMS, please refer to this topic.
The data stored in the RecordStore is present in bytes, and there is no data in the MIDP specification that can be stored in the RMS as long as he can convert to byte an array. So what questions should we pay attention to when reading and writing these bytes? Since the access speed of non-volatile memory is slower, we should write less to the RMS as much as possible. Of course, this is also related to the equipment, and some of the devices are very good resources. When reading data, we should try to multiplex the objects, avoid a lot of creating objects and discard the object, which will cause a small burden to Heap and GC.
Look at the two different code snippets of the following read data / / piece 1RecordStore RS = ....; // an open record Store
Try {int lastid = rs.getNextRecordId (); byte [] data; for (int i = 0; i // Pieces 2RecordStore RS = ....; // An Open Record Store Try {recordenumeration enum = rs.enumerateRecords (NULL, NULL, FALSE); while (enum.hasnextelement ()) {byte [] data = enum.nexTrecord (); .... // do something with the data}} catch (Exception E) {// error} The problem of the above code is that the system must create a new byte array object each time you read the record, which is obviously not efficient enough. In fact, we can reuse the byte arrays and adjust its size appropriately. RecordStore RS = ....; // An Open Record Store Try {recordenumeration enum = rs.enumerateRecords (null, null, false); byte [] data = new byte [100]; int Len = 0; while (enum.hasnextelement ()) {int id = enum.nexTrecordID (); LEN = rs.getRecordsize (id); if (len> data.length) {// add a fundh factor data = new byte [len 40];} Rs.getRecord (ID, DATA, 0); // do Something With the data}} catch (exception e) {// error} When we read the data, you will usually use Javaio, such as ByteArrayInputStream and DataInputStream class, then we should try to use them when using them. For example, when we read records from RMS, it is assumed that records include a Boolean and two integer data. RecordStoreEnumeration enum = ...; // get a record enumerationbyte [] data = new byte [9]; // record sizeByteArrayInputStream bin = new ByteArrayInputStream (data); DataInputStream din = new DataInputStream (bin); While (enum.hasnextelement ()) {Int id = enum.nexTrecordId (); getRecord (ID, data, 0); Din.Reset (); //move stream back to start boolean first = din.readboolean (); int Second = din.readint (); int third = DIN.READINT (); // do sometying here} Let's take a packaged Record class where you can use it Import java.io. *; Import javax.microedition.rms when using RMS. Public class record imports datainput { Private recordstore _rs; private bote [] _data; private int _length; private int _id; private datainputstream _din Public Record (RecordStore RS) {THIS (RS, 100); Public Record (RecordStore RS, INT InitialRecordsize) {_RS = rs; _data = new byte [InitialRecordsize]; _din = new datainputstream (_LETH = -1;}) Public Byte [] getByteArray () {return_data; Public int getLength () {return_length;} public byte [] moveTo (int id) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException, IOException {_length = _rs.getRecordSize (id); if (_length> _data.length) {_data = new byte [_length 40]; _din = new DataInputStream (New ByteArrayinputStream (_DATA)); _rs.getRecord (ID, _DATA, 0); _ID = id; _din.reset (); Return_Data; Public void readfully (byte b []) throws oException {_din.readfully (b);} Public void readfully (Byte B [], INT OFF, INT LEN "{_din.readfully (b, off, len);} return_din.skipbytes (n);} Public Boolean ReadBoolean () throws oException {return_din.readboolean ();}}} ooException {return_din.readbyte (); Public int ooException {return_din.readunsignedbyte (); Public short readshort () throws oException {return_din.readshort (); Public int readyPtion {return _din.readunsignedshort (); Public char readchar () throws oException {return_din.readchar ();}}}}} ooException {return_din.readint (); Public long readlong () throws ioException {return_din.readlong (); Public string readf () throws oException {return_din.readutf ();}} Very simple to try {rs = RecordStore.openRecordStore ( "mydata", true); // Write two records to the record bout = new ByteArrayOutputStream storeByteArrayOutputStream (); DataOutputStream dout = new DataOutputStream (bout); byte [] data; DOUT.WRITEUTF ("this is a test"); dout.writeint (1); dout.flush (); DATA = bout.tobyteaRray (); Rs.addRecord (Data, 0, Data.Length); Bout.reset (); DOUT.WRITEUTF ("this is another test"); dout.writeint (99); dout.flush (); data = bout.tobyteaRray (); rs.addrecord (Data, 0, Data.length); // Now Read Through The Record Store Record record = new Record (rs); int lastID = rs.getNextRecordID (); RecordEnumeration enum = rs.enumerateRecords (null, null, while (enum.hasNextElement ()) {int id = enum.nextRecordId (); record.moveTo (ID); system.out.println (Record.Readutf () "" Record.Readint ()); rs.closecordStore ();} catch (exception e) {// handle error}