RMS efficient programming guide (zt)

Source: Internet
Author: User
RMS efficient programming guide record management system is a subsystem of MIDP that provides persistent storage of data. This article does not describe the basic knowledge of record management system, instead, we recommend that you use the record management system efficiently from the programming perspective. If you do not know enough about RMS, please refer to other articles on this topic.

The data stored in recordstore exists in bytes. The MIDP specification does not specify that any data can be stored in RMS, as long as it can be converted to a byte array. So what should we pay attention to when reading and writing these bytes of data? Since non-volatile memory access speed is relatively slow, we should try to perform less write operations on RMS. Of course, this is also related to devices. Write operations on some devices are very cost-effective. When reading data, we should try to reuse the object as much as possible to avoid creating a large number of objects and then discarding the object, which will cause a great burden to heap and GC.

Let's take a look at the following two different code snippets for reading data:
// Segment 1
Recordstore rs =...; // an open record store

Try {
Int lastid = Rs. getnextrecordid ();
Byte [] data;

For (INT I = 0; I <lastid; ++ I ){
Try {
Data = Rs. getrecord (I );
... // Do something with the data
}
Catch (invalidrecordidexception e ){
Continue;
}
}
}
Catch (exception e ){
// Error
}

// Segment 2
Recordstore 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 with the code above is that the system creates a new byte array object every time it reads records, which is obviously not efficient enough. In fact, we can reuse the byte array and adjust its size as appropriate.
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 growth factor
Data = new byte [Len + 40];
}
Rs. getrecord (ID, Data, 0 );

// Do something with the data
}
}
Catch (exception e ){
// Error
}

Javaio is usually used when we read data, such as bytearrayinputstream and datainputstream. When using them, we should try to reuse objects as much as possible. For example, when we read a record from RMS, we assume that the record contains a boolean type and two integer data types.
Recordstoreenumeration Enum =...; // get a record Enumeration
Byte [] DATA = new byte [9]; // record size
Bytearrayinputstream 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 something here
}

The following provides an encapsulated record class. You can use it when using RMS.
Import java. Io .*;
Import javax. microedition. RMS .*;

Public class record implements datainput {

Private recordstore _ rs;
Private byte [] _ 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 (
New bytearrayinputstream (_ DATA ));
_ Length =-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 ioexception {
_ Din. readfully (B );
}

Public void readfully (byte B [], int off, int Len)
Throws ioexception {
_ Din. readfully (B, off, Len );
}
Return _ din. skipbytes (N );
}

Public Boolean readboolean () throws ioexception {
Return _ din. readboolean ();
}
 
Public byte readbyte () throws ioexception {
Return _ din. readbyte ();
}

Public int readunsignedbyte ()
Throws ioexception {
Return _ din. readunsignedbyte ();
}

Public short readshort () throws ioexception {
Return _ din. readshort ();
}

Public int readunsignedshort ()
Throws ioexception {
Return _ din. readunsignedshort ();
}

Public char readchar () throws ioexception {
Return _ din. readchar ();
}
Public int readint () throws ioexception {
Return _ din. readint ();
}

Public long readlong () throws ioexception {
Return _ din. readlong ();
}

Public String readutf () throws ioexception {
Return _ din. readutf ();
}
}

Easy to use
Try {
Rs = recordstore. openrecordstore ("mydata", true );

// Write two records to the record store

Bytearrayoutputstream bout =
New bytearrayoutputstream ();
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. closerecordstore ();
}
Catch (exception e ){
// Handle error
}

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.