Core Technology: Analysis of RMS usage in j2s

Source: Internet
Author: User
Core Technology: Analysis of RMS usage in j2-general Linux technology-Linux programming and kernel information. The following is a detailed description. In j2s, RMS is the only permanent storage tool, and its importance is self-evident. However, many new people who have just started to learn j2s always complain that there is little or no specific information in this area. Therefore, I want to share some of my learning experiences in this area with you.

RMS is the Record Manager System, which is often used as a tool for scoring records and Game Information Storage in mobile apps.
The use of RMS can be divided into two parts: 1. Single record construction; 2. Use and operation of RecordStore. The following two aspects are described in detail.

1. Construction of a single record. We may need to record many similar entries when storing records. Here we can regard this structure as a database. In this step, we need to construct a row in the database, that is, the construction of a single record. The source code of the program is as follows:
Package com. cuilichen. usual;

Import java. io. ByteArrayInputStream; // various input/output streams to be used
Import java. io. ByteArrayOutputStream;
Import java. io. DataInputStream;
Import java. io. DataOutputStream;

Public class Appointment {// class name of a single record
Private int int1 ;//
Private int int2 ;//
Private long long1;
Private String str1; // str1 is used as a reserved field to record the search keywords.
Private String str2 ;//
Private String str3 ;//
Private boolean WroteFlag ;//

Public Appointment (){
}

Public Appointment (int _ int1, int _ int2, long _ long1, String _ str1,
String _ str2, String _ str3, boolean _ WroteFlag ){
This. int1 = _ int1; // write the RMS Constructor
This. int2 = _ int2;
This. long1 = _ long1;
This. str1 = _ str1;
This. str2 = _ str2;
This. str3 = _ str3;
This. WroteFlag = _ WroteFlag;
}

Public Appointment (byte [] rec ){
InitAppointmnet (rec); // constructor for reading RMS content
}

Public byte [] toBytes () {// write it as byte

Byte [] data = null;

Try {
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
DataOutputStream dos = new DataOutputStream (baos );
Dos. writeInt (int1 );
Dos. writeInt (int2 );
Dos. writeLong (long1 );
Dos. writeUTF (str1 );
Dos. writeUTF (str2 );
Dos. writeUTF (str3 );
Dos. writeBoolean (WroteFlag );
Data = baos. toByteArray ();
Baos. close ();
Dos. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
Return data;
}

Public void initAppointmnet (byte [] rec) {// read content from byte

ByteArrayInputStream bais = new ByteArrayInputStream (rec );
DataInputStream dis = new DataInputStream (bais );

Try {
Int1 = dis. readInt ();
Int2 = dis. readInt ();
Long1 = dis. readLong ();
Str1 = dis. readUTF ();
Str2 = dis. readUTF ();
Str3 = dis. readUTF ();
WroteFlag = dis. readBoolean ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
Public int getInt1 () {// int
Return int1;
}
Public int getInt2 (){
Return int2;
}
Public long getLong1 (){
Return long1;
}

Public String getStr1 () {// String
Return str1;
}

Public String getStr2 () {// String
Return str2;
}

Public String getStr3 (){
Return str3;
}

Public boolean getWroteFlag () {// return the write flag
Return WroteFlag;
}
}
This class ensures the writing and output of content when we use a stream. Of course, just like the database table design, we can add or remove fields to or from each record at will. In the above class, I only use int1, int2, long1, str1, str2, str3 and WroteFlag have a total of seven fields.

2. RecordStore operations. Class RMS is as follows:
Package com. cuilichen. usual;

Import javax. microedition. rms. RecordEnumeration;
Import javax. microedition. rms. RecordStore;

Public class RMS {
Public static final int Int1 = 0; // default value of each field
Public static final int Int2 = 0;
Public static final long Long1 = 0;
Public static final String Str1 = "";
Public static final String Str2 = "";
Public static final String Str3 = "";

Public static boolean addRecord (String name, int int1, int int2, // Add record
Long long1, String str1, String str2, String str3, boolean B ){
Boolean success = false;

Try {
RecordStore rs = RecordStore. openRecordStore (name, true );
Appointment app = new Appointment (int1, int2, long1, str1, str2, str3, B );
// Since str1 is a reserved field, we need to perform this operation here: for example, if int1 is the keyword we set, str1 = Integer. toString (int1 );
Byte [] data = app. toBytes ();
Rs. addRecord (data, 0, data. length );
Rs. closeRecordStore ();
Success = true;
} Catch (Exception e ){
E. printStackTrace ();
}
Return success;
}
Public static int getNumOfRecords (String name) {// obtain the number of records in RMS
Try {
RecordStore rs = RecordStore. openRecordStore (name, true );

Return rs. getNumRecords ();
} Catch (Exception e ){
Return 0;
}
}

Public static Appointment [] getRecords (String name) {// retrieve all records in RMS
Appointment [] result = {};

Try {
RecordStore rs = RecordStore. openRecordStore (name, false );
RecordEnumeration re = rs. enumerateRecords (null, null, false );
Result = new Appointment [rs. getNumRecords ()];

For (int I = 0; I <result. length; I ++ ){
Int j = re. previusrecordid ();
Appointment app = new Appointment (rs. getRecord (j ));
Result = app;

// System. out. println ("app [" + I + "]" + app. getStr2 ());
}

Rs. closeRecordStore ();
} Catch (Exception e ){
}

Return result;
}
Public static Appointment getRecord (String name, int j) {// retrieve a record based on the Record Number (int j parameter)
Appointment result = new Appointment ();

Try {
RecordStore rs = RecordStore. openRecordStore (name, false );
RecordEnumeration re = rs. enumerateRecords (null, null, false );
Result = new Appointment (rs. getRecord (j ));
Rs. closeRecordStore ();
} Catch (Exception e ){
}

Return result;
}

Public static int getIndex (String name, String content) {// get the record number int j. The reserved field str1 must be used here.
RecordStore rs = null;
RecordEnumeration re = null;

Try {
Rs = RecordStore. openRecordStore (name, false); // open
Re = rs. enumerateRecords (null, null, false); // enumeration

For (int I = 0; I <RMS. getNumOfRecords (name); I ++ ){
Int j = re. nextRecordId ();
Appointment app = new Appointment (rs. getRecord (j ));

If (app. getStr1 (). equals (content )){
Return j;
}
}
} Catch (Exception e ){
}
Return 1;
}

Public static boolean setRecord (String name, int id, int int1, int int2, // records whose record number is id
Long long1, String str1, String str2, String str3, boolean B ){
Boolean success = false;
RecordStore rs = null;
RecordEnumeration re = null;

Try {
Rs = RecordStore. openRecordStore (name, false); // open
Re = rs. enumerateRecords (null, null, false); // enumeration

Appointment app = new Appointment (int1, int2, long1, str1, str2, str3, B );
// Str1 is used as a reserved field. In this case, if int1 is the keyword we set, str1 = Integer. toString (int1 );

Byte [] data = app. toBytes ();
Rs. setRecord (id, data, 0, data. length );
Success = true;
Rs. closeRecordStore ();
} Catch (Exception e ){
}

Return success;
}
}
In this class, I didn't throw each Exception out. In general, this is not suitable. It violates Java's Exception handling mechanism. However, among the various j2s programs that I use this class, it is competent, so no further modifications are made.
With the above two classes and your understanding of RMS, you can use RMS smoothly in the program.
For example, at the beginning of the MIDlet, perform the following operations (add record ):
Protected void startApp () throws MIDletStateChangeException {
If (RMS. getNumOfRecords (rsName) = 0) {// rsName has been declared before. String rsName = "MyRMS ";
For (int I = 0; I <6; I ++ ){
RMS. addRecord (rsName, RMS. Int1, I, RMS. Long1, Integer. toString (I), RMS. Str2, "1234567890123456789", false );
}
} It adds 6 records to RMS, where int1, long1, str2, and WroteFlag are not used. We only use int2, str1 (as reserved fields), and str3.
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.