RMS (record Management System) is a very important subsystem in MIDP because it is the only way to J2ME applications for persistent storage. Of course, if your system supports JSR75, then you can use Fileconnection to manipulate the file, which is beyond the scope of this article. Persistent storage is often used when we write applications, such as recording the ranking of games, recording user names and passwords entered by users, and so on. This article will mainly from the basic concept of RMS and the use of guidelines to introduce, the purpose is to give readers a certain direction.
RMS was first proposed in MIDP1.0, where the package is javax.microedition.rms, which includes four interfaces, a class, and five exceptions in total. This shows that the RMS design is very small, this is to meet the Mobile information equipment resources constrained requirements. Let's figure out a few concepts first. What is persistent storage.
The simple understanding of persistent storage is that data is not lost because of the program's exit, and the variables we declare in the program are stored on stack or heap, and the data is purged to release resources after the program exits. Data stored in RMS is not purged. Where is the RMS data stored.
The MIDP specification does not specify where the RMS data must be stored, but is implemented by the vendor. Generally stored in non-volatile memory space. So this is transparent to the programmer. The capacity of the RMS is minimal.
MIDP requires the manufacturer to implement RMS when the storage space provided can not be less than 8KB, such as the author's Nokia 6108 RMS space is 30KB. In RMS, the ID is not equal to the index, stored according to the record.
The difference between IDs and indexes is still very large, with IDs starting at 1, and the 0 starting counts for this and the array are somewhat different. IDs can be discontinuous, and when an ID tag record is deleted, the corresponding ID becomes invalid. IDS cannot be reused. Does RMS have specific requirements for the format of the data stored in it?
The answer is no, as long as the data can be converted to byte[] then this data can be stored in RMS, which is still byte[when taken out. So this requires US developers to depict the data, because RMS is only responsible for writing and reading data according to byte[. In a MIDlet suite, RecordStore can be shared.
Can a recordstore in a midlet suite be accessed by another recordstore?
In the MIDP1.0 can not be, in the MIDP2.0 introduced a sharing mechanism, through sharing can be achieved.
The above 7-point situation summarizes the basic concepts that need to be noted in RMS, and let's look at how to use RMS. When beginners learn rms, they usually get the idea of how to do it, because many methods look similar. Here I make a summary of the following to provide some guidelines for you to use.
First readers should be aware that RecordStore is the equivalent of a database, you must create a new database to start using RMS to store read data. The new recordstore is very simple, and you can use the following static method.
Static RecordStore Openrecordstore (String recordstorename, Boolean createifnecessary)
Note that recordstorename should be a Unicode character of no more than 32 bits in length, case-sensitive and unique within the MIDlet suite, followed by a Boolean type of createifnecessary that, if marked true, Then RecordStore does not exist to create it. Close RecordStore use Closerecordstore (). Another important concept in RMS is the record, which is like a row of data in a database. Here we first distinguish the methods in RecordStore, some of which are used to obtain recordstore information, and some are used to obtain record information. Get recordstore Information
int getversion ()
int getsize ()
String getName ()
Long getlastmodified ()
Get record information
int getnumrecords ()
int getnextrecordid ()
int getrecordsize (int recordid)
The following describes how to manipulate a record, mainly including additions, modifications, reads, and deletions. Reading Records
Byte[] Getrecord (int recordid)
int Getrecord (int recordid, byte[] buffer, int offset)
Add a record
int AddRecord (byte[] data, int offset, int numbytes)
Update records
Setrecord (int recordid, byte[] newdata, int offset, int numbytes)
Delete a record
DeleteRecord (int RecordID)
We mentioned earlier that ID and index are different because IDs may not be contiguous, so how do we traverse the data? Many people may think of using a for loop, but because IDs may be discontinuous, the result is unpredictable. The program is likely to fail. It is for this reason that an important interface recordenumeration is provided in RMS. It can traverse the data in RecordStore. Let's look at the following method.
Recordenumeration Enumeraterecords (recordfilter filter, Recordcomparator Comparator, Boolean keepupdated)
Also included in this method are two other interfaces in RMS Recordfilter and Recordcomparator, which are designed to tailor the traversal result set, and you can implement recordfilter to determine what kind of data to filter out, Determine the sort of data by implementing Recordcomparator. The last parameter keepupdated, if set to true, will track the changes in the data in RecordStore and will be reflected in the result set we listed, knowing that this is a very cost-intensive operation and that it is recommended to set to false. Recordenumeration is equivalent to a two-way data link list. You can keep moving by calling Nextrecordid () and Previousrecordid (). Other methods about Recordenumeration readers can refer to Java Doc for learning.
The last point to note is the sharing mechanism, which is the new feature provided in the MIDP2.0. Allow the RecordStore in one suite to be accessed by another, which is, of course, in the authorized mode. If the MIDlet suite1 is authmode_any in the creation of RecordStore1, then other packages may have access to RecordStore1, such as the one above MIDlet Suite2. Typically, such access is done in two steps. Create a recordstore that can be shared
We can do this in the following way, and we have to set the AuthMode to authmode_any.
Static RecordStore Openrecordstore (String recordstorename, boolean createifnecessary, int authmode, Boolean writable)
Visit recordstore
If a midlet in another MIDlet suite wants to access it, it needs to know the VendorName and suitename of the MIDlet suite to be accessed, and we can usually get both of these data from the Jad file. I use the following method,
Static RecordStore Openrecordstore (String recordstorename, String VendorName, String suitename)
The main purpose of this article is to introduce the basic concepts of RMS and provide some guidance to readers on how to use the methods in RMS. To further study RMS, readers can refer to the source download area The author provides a mobile communication based on midp1.0 implementation.