您的位置:首页 > 其它

Record Management System从入门到精通系列之三

2008-04-25 06:01 316 查看
前面两篇文章详细的介绍了Record Management System的基本概念以及对象序列化的问题,现在我们主要介绍关于RecordStore类的使用,在SUN的网站提供了一个RMSAnalyzer类,你可以把他用在你的项目中来调试你的程序。

Record Store Discovery

你可以通过调用RecordStore.listRecordStores()来得到MIDlet suites中的Record Store,这个静态方法返回一个String类型的数组,每个代表Record Store的名字,如果没有Record Store那么会返回null,方法RMSAnalyzer.annlyzeAll()通过调用listRecordStores()得到Record Store然后通过方法analyze()分析每个Record Store. public void analyzeAll(){ String[] names = RecordStore.listRecordStores(); for( int i = 0; names != null && i < names.length; ++i ){ analyze( names[i] ); } } 注意到列出的数组名字是所属MIDlet suite的Record Store。MIDP中没有提供列举出任何其他MIDlet suites的Record Store的方法,在MIDP 1.0中Record Store在所属MIDlet suites外是不可见的,在MIDP 2.0中,MIDlet suite可以指定一个Record Store作为可共享的,但是其他的suite要知道他的名字才可以访问它。

Opening and closing Record Store

RecordStore.openRecordStore()是用来打开一个Record Store的,它也可以用来创建一个Record Store,这个静态方法返回一个Record Store的对象,下面是RMSAnalyzer.analyze()。 public void analyze( String rsName ){ RecordStore rs = null; try { rs = RecordStore.openRecordStore( rsName, false ); analyze( rs ); // call overloaded method } catch( RecordStoreException e ){ logger.exception( rsName, e ); } finally { try { rs.closeRecordStore(); } catch( RecordStoreException e ){ // Ignore this exception } } } openRecordStore()的第二个参数表示如果Record store不存在是不是创建新的,在MIDP2.0中,如果你想打开一个在其他的MIDlet suite里面创建的Record Store的话应该用下面的方法。 ... String name = "mySharedRS"; String vendor = "EricGiguere.com"; String suite = "TestSuite"; RecordStore rs = RecordStore.openRecordStore( name, vendor, suite ); ... vendor和suite的名字应该和MIDlet suite的manifest和jad的内容一致。 当你完成了对Record store的操作以后应该调用RecordStore.closeRecordStore()来关闭它,一个RecordStore的实例在一个MIDlet suite里面是唯一的,如果以同样的名字再次调用openRecordStore()的话会返回同样的实例,这样多个MIDlet在共享一个Record store,每个Record store会跟踪它被打开的次数,这个Record store直到被调用相同次数的closeRecordStore()后才会彻底的关闭,对一个已经关闭的Record Store进行操作会导致抛出 RecordStoreNotOpenException。

Creating Record Store

创建一个私有的Record store,把第二个参数设置为true调用openRecordStore(),... // Create a Record store RecordStore rs = null; try { rs = RecordStore.openRecordStore( "myrs", true ); } catch( RecordStoreException e ){ // couldn't open it or create it } 如果要创建一个可共享的Record store,那么使用四个参数变量的openRecordStore() int authMode = RecordStore.AUTHMODE_ANY; boolean writable = true; rs = RecordStore.openRecordStore( "myrs", true, authMode, writable ); 当第二个参数是true并且Record store不存在的时候,后面两个参数控制他的授权模式和可写性,授权模式决定是否其他的MIDlet suite具有访问Record store的权限,两种可能的模式是RecordStore.AUTHMODE_PRIVATE(只有拥有的SUITE才可以访问)和RecordStore.AUTHMODE_ANY(任何suite都可以访问),可写性控制着是否其他的suite能够修改Record store,如果false的话,那么只有所属suite才可以修改,其他的只能读取。注意所属suite可以在任何时候调用RecordStore.setMode()来修改它的授权和读写模式,例如: rs.setMode( RecordStore.AUTHMODE_ANY, false );事实上最好是创建一个Record store,授权模式为RecordStore.AUTHMODE_PRIVATE。

Adding and updating Records

记录就是字节数组,你可以通过调用RecordStore.addRecord()来添加一个新的记录到一个打开的Record Store
byte[] data = new byte[]{ 0, 1, 2, 3 }; int RecordID; RecordID = rs.addRecord( data, 0, data.length ); 如果第一个参数是null的话,那么你就能添加一个空记录。第二个和第三个参数说明了字节数组的起点和从起点开始的总的字节数。如果添加成功会返回新记录的Record ID,如果失败会抛出异常,例如RecordStoreFullException。 通过调用RecordStore.setRecord()可以在任何时候更新记录的内容。 int RecordID = ...; // some Record ID byte[] data = new byte[]{ 0, 10, 20, 30 }; rs.setRecord( RecordID, data, 1, 2 ); // replaces all data in Record with 10, 20 你不能大块的添加记录,必须首先把记录转换成数组,然后调用添加的函数,通过调用RecordStore.getNextRecordID()你可以得到下次调用addRecord()将要得到的Record ID,这个值比现在使用的任何值都大。

Reading Records

想要读取记录,有两种方法。第一是分配合适大小的数组然后把记录的内容复制过去。 int RecordID = .... // some Record ID byte[] data = rs.getRecord( RecordID ); 第二种方法是把数组复制到预先分配的字节数组中,指定复制的起点并返回复制的字节数目。 int RecordID = ...; // some Record ID byte[] data = ...; // an array int offset = ...; // the starting offset int numCopied = rs.getRecord( RecordID, data, offset ); 数组的大小必须能足够容纳数据,否则会抛出java.lang.ArrayIndexOutOfBoundsException.使用RecordStore.getRecordSize()来分配数组空间是合适的方法。事实上第一个方法等价与 byte[] data = new byte[rs.getRecordSize(RecordID)]; rs.getRecord(RecordID,data,0); 第二种方法有利于减小内存的分配,当你要遍历一组记录的时候,你可以结合getNextRecordID()和getRecordSize()来完成运算量很大的搜索。 int nextID = rs.getNextRecordID(); byte[] data = null; for( int id = 0; id < nextID; ++id ){ try { int size = rs.getRecordSize( id ); if( data == null || data.length < size ){ data = new byte[ size ]; } rs.getRecord( id, data, 0 ); processRecord( rs, id, data, size ); // process it } catch( InvalidRecordIDException e ){ // ignore, move to next Record } catch( RecordStoreException e ){ handleError( rs, id, e ); // call an error routine } } 更好的办法是使用RecordStore.enumerateRecords()来遍历记录。

Deleting Records and Record Stores

你可以通过调用RecordStore.deleteRecord()来删除记录 int RecordID = ...; // some Record ID rs.deleteRecord( RecordID ); 一旦记录被删除,任何对记录的操作将会导致抛出InvalidRecordIDException,通过调用RecordStore.deleteRecordStore()来删除Record Store. try { RecordStore.deleteRecordStore( "myrs" ); } catch( RecordStoreNotFoundException e ){ // no such Record store } catch( RecordStoreException e ){ // somebody has it open } Record Store只能在没有打开的时候被所属的suite的MIDlet删除。

Other operations

getLastModified()返回最后修改Record store的时间,格式和System.currentTimeMillis()一样。 getName()得到Record store的名字。 getNumRecords()返回Record store中记录的数量。 getSize()返回Record store的整个大小,包括记录的大小和系统来实现Record store的空间。 getSizeAvailable()返回Record store中还能用的空间, getVersion()返回Record store的版本数,这个数大于0一流信息监控拦截系统(IMB System)

中客科技信息有限公司信息监控系统提醒您:很抱歉,由于您提交的内容中或访问的内容中含有系统不允许的关键词或者您的IP受到了访问限制,本次操作无效,系统已记录您的IP及您提交的所有数据。请注意,不要提交任何违反国家规定的内容!本次拦截的相关信息为:监听器an lang="EN">deleteRecordListener()。

The RMSAnalyzer class

最后我们提供一个分析Record store的类,你可以这样使用它。 RecordStore rs = ...; // open the Record store RMSAnalyzer analyzer = new RMSAnalyzer(); analyzer.analyze( rs ); 通常分析输出到System.out,样式如下所示: ========================================= Record store: Recordstore2 Number of Records = 4 Total size = 304 Version = 4 Last modified = 1070745507485 Size available = 975950 Record #1 of length 56 bytes 5f 62 06 75 2e 6b 1c 42 58 3f _b.u.k.BX? 1e 2e 6a 24 74 29 7c 56 30 32 ..j$t)|V02 5f 67 5a 13 47 7a 77 68 7d 49 _gZ.Gzwh}I 50 74 50 20 6b 14 78 60 58 4b PtP k.x`XK 1a 61 67 20 53 65 0a 2f 23 2b .ag Se./#+ 16 42 10 4e 37 6f .B.N7o Record #2 of length 35 bytes 22 4b 19 22 15 7d 74 1f 65 26 "K.".}t.e& 4e 1e 50 62 50 6e 4f 47 6a 26 N.PbPnOGj& 31 11 74 36 7a 0a 33 51 61 0e 1.t6z.3Qa. 04 75 6a 2a 2a .uj** Record #3 of length 5 bytes 47 04 43 22 1f G.C". Record #4 of length 57 bytes 6b 6f 42 1d 5b 65 2f 72 0f 7a koB.[e/r.z 2a 6e 07 57 51 71 5f 68 4c 5c *n.WQq_hL 1a 2a 44 7b 02 7d 19 73 4f 0b .*D{.}.sO. 75 03 34 58 17 19 5e 6a 5e 80 u.4X..^j^? 2a 39 28 5c 4a 4e 21 57 4d 75 *9(JN!WMu 80 68 06 26 3b 77 33 ?h.&;w3 Actual size of Records = 153 ----------------------------------------- 这种样式方便在wtk中显示,在实际的设备中进行测试的时候,你可能希望把分析输出到串口或者通过网络发到servlet,你可以通过定义自己的类实现实现Logger接口,然后把这个类作为RMSAnalyzer构造器的参数。下面是源代码。
package com.ericgiguere; import java.io.*; import javax.microedition.rms.*; // Analyzes the contents of a Record store. // By default prints the analysis to System.out, // but you can change this by implementing your // own Logger. public class RMSAnalyzer { // The logging interface. public interface Logger { void logEnd( RecordStore rs ); void logException( String name, Throwable e ); void logException( RecordStore rs, Throwable e ); void logRecord( RecordStore rs, int id, byte[] data, int size ); void logStart( RecordStore rs ); } private Logger logger; // Constructs an analyzer that logs to System.out. public RMSAnalyzer(){ this( null ); } // Constructs an analyzer that logs to the given logger. public RMSAnalyzer( Logger logger ){ this.logger = ( logger != null ) ? logger : new SystemLogger(); } // Open the Record stores owned by this MIDlet suite // and analyze their contents. public void analyzeAll(){ String[] names = RecordStore.listRecordStores(); for( int i = 0; names != null && i < names.length; ++i ){ analyze( names[i] ); } } // Open a Record store by name and analyze its contents. public void analyze( String rsName ){ RecordStore rs = null; try { rs = RecordStore.openRecordStore( rsName, false ); analyze( rs ); } catch( RecordStoreException e ){ logger.logException( rsName, e ); } finally { try { rs.closeRecordStore(); } catch( RecordStoreException e ){ // Ignore this exception } } } // Analyze the contents of an open Record store using // a simple brute force search through the Record store. public synchronized void analyze( RecordStore rs ){ try { logger.logStart( rs ); int lastID = rs.getNextRecordID(); int numRecords = rs.getNumRecords(); int count = 0; byte[] data = null; for( int id = 0; id < lastID && count < numRecords; ++id ){ try { int size = rs.getRecordSize( id ); // Make sure data array is big enough, // plus add some for growth if( data == null || data.length < size ){ data = new byte[ size + 20 ]; } rs.getRecord( id, data, 0 ); logger.logRecord( rs, id, data, size ); ++count; // only increase if Record exists } catch( InvalidRecordIDException e ){ // just ignore and move to the next one } catch( RecordStoreException e ){ logger.logException( rs, e ); } } } catch( RecordStoreException e ){ logger.logException( rs, e ); } finally { logger.logEnd( rs ); } } // A logger that outputs to a PrintStream. public static class PrintStreamLogger implements Logger { public static final int COLS_MIN = 10; public static final int COLS_DEFAULT = 20; private int cols; private int numBytes; private StringBuffer hBuf;  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: