选择显示字体大小

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 )&#123;
try &#123;
data = rs.getrecord( i );
.... // do something with the data
&#125;
catch( invalidrecordidexception e )&#123;
continue;
&#125;
&#125;
&#125;
catch( exception e )&#123;
// error
&#125;

//片断2
recordstore rs = ....; // an open record store

try &#123;
recordenumeration enum = rs.enumeraterecords(
null, null, false );
while( enum.hasnextelement() )&#123;
byte[] data = enum.nextrecord();
.... // do something with the data
&#125;
&#125;
catch( exception e )&#123;
// error
&#125;

上面的代码存在的问题是系统每次读取记录都要创建新的字节数组对象,这显然不够高效。其实我们可以对字节数组进行复用,并可以适当的调整它的大小。
recordstore rs = ....; // an open record store

try &#123;
recordenumeration enum = rs.enumeraterecords(
null, null, false );
byte[] data = new byte[100];
int len = 0;

while( enum.hasnextelement() )&#123;
int id = enum.nextrecordid();

len = rs.getrecordsize( id );
if( len > data.length )&#123;
// add a growth factor
data = new byte[ len + 40 ];
&#125;
rs.getrecord( id, data, 0 );

// do something with the data
&#125;
&#125;
catch( exception e )&#123;
// error
&#125;

在我们读取数据的时候,通常还会用到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() )&#123;
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
&#125;

[1] [2] [3]  下一页


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons