RMS高效编程指南

来源:互联网 发布:python 驼峰 下划线 编辑:程序博客网 时间:2024/04/29 13:51

RMS高效编程指南


Record Management System是MIDP的子系统,提供了数据的持久性存储功能,本文并非讲述Record Management System的基础知识,而是从编程的角度提出高效使用Record Management System的建议。如果您对RMS还不够了解请参考本专题其他的文章。

    在RecordStore中存储的数据是以字节的形势存在的,MIDP规范中并没有规定什么数据可以存储在RMS中,只要他可以转换成字节数组。那么读取和写入这些字节数据的时候我们应该注意些什么问题呢?由于非挥发性内存的存取速度都比较慢,因此我们应该尽量的少对RMS进行写操作,当然这也和设备有关系,有些设备的写操作是非常好费资源的。在读取数据的时候我们应该尽量复用对象,避免大量的创建对象然后丢弃对象,这样会给Heap和GC造成不小的负担。

    看下面读取数据的两个不同的代码片断
//片断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
}

//片断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
}

    上面的代码存在的问题是系统每次读取记录都要创建新的字节数组对象,这显然不够高效。其实我们可以对字节数组进行复用,并可以适当的调整它的大小。
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,比如ByteArrayInputStream和DataInputStream类,那么在使用他们的时候,我们也应该尽量复用对象。比如当我们从RMS中读取纪录的时候,假设纪录包括一个布尔型和两个整型数据。
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
}


    下面提供一个封装好的Record类,你可以在使用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();
    }
}

使用起来非常简单
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
}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 车电瓶没电了打不着火怎么办 面包车没电了打不着火怎么办 汽车电瓶没电了打不着火怎么办 自动挡的车没有电打不起来火怎么办 自动挡汽车没电了打不着火怎么办 自动挡小车没电了打不着火怎么办 被4s店销售骗了怎么办 宇通客车尿素泵不工作怎么办 汽车打蜡不小心打在车帮怎么办 海信电视怎么安装第三方软件怎么办 网上买票的时候不想用学生票怎么办 南方公园完整破碎讲话嘴不动怎么办 同一个安全员被锁在两个项目怎么办 家长拖欠家教老师的课时费怎么办 合同没到期房东不退押金怎么办 租房合同没到期房东要违约怎么办 档案存放费交了一年的延期怎么办 天津房子卖了户口没地方迁怎么办 中国到美国读计算机硕士签证怎么办 在江苏大学去德国读研怎么办? 宿舍上下铺的床一动就响怎么办 自助取款机存款忘打印了凭条怎么办 高一新生跨省转学籍怎么办 网上买票不小心买成了学生票怎么办 做横幅标语字打出来老是歪的怎么办 创业板股票暂停上市后钱怎么办 新股东入股公司之前的亏损怎么办 目前公司账面亏损有人要入股怎么办 土地确权后没有土地的人怎么办 老板不发工资跑路了怎么办 别人登录了我的美团账号怎么办 月嫂家政公司快坚持不下去了怎么办 华硕飞马4a手机发热怎么办 超级必发指数手机页面没曲线怎么办 唐小僧理财暴雷了投资者该怎么办 教师资格证面试准考证号忘了怎么办 初中学校说处分不给毕业证怎么办? 自动档一键打火的车没电了怎么办 物流代收货款一直拿不到钱怎么办 丰巢快递柜收不到验证码怎么办 拼多多三级惩罚下架3天怎么办