记录管理系统(Record Management System,RMS)是移动信息设备描述(Mobile Information Device Profile,MIDP)的一个主要子系统,是一种应用程序编程接口(API),为 MIDP 应用程序提供本地的、基于设备的数据持久存储功能,
了解MIDP记录管理系统
。在各种 MIDP 设备大行其道的今天,RMS 是唯一可以实现本地数据存储的工具——极少设备支持传统的文件系统。可以设想一下,彻底地了解 RMS,对于编写任何依靠持久性本地数据的程序来说,是多么至关重要。
本文是一系列文章的第一篇。这些文章将探究 RMS,以及解答有关它在 MIDP 应用程序中使用情况的较重要的问题,例如,与外部数据源(如关系数据库)相互作用的问题。开始我们要讨论的是 RMS 提供一些什么内容,并编写几个简单的 RMS 调试诊断程序。
关键概念
首先,要理解记录管理系统的一些关键概念。
记录
顾名思义,RMS 是用来管理记录的系统。一条记录是一个单一的数据项。RMS 没有对放入记录的内容加以限制:记录可以是数字、字符串、数组和图像——任何一段连续字节可以描述的事物。如果您能将数据编译成二进制代码,并且具备相应的解码手段,那么您就能将其存入一条记录中,当然受系统分配的容量大小的限制。
许多 RMS 的初学者都对记录这个词感到困惑。“字段在什么地方?”他们奇怪系统是如何将单一记录细分为分散的数据序列。答案很简单:在RMS中,记录不包含任何字段。或者说得更准确些,一条记录只包含一个大小可变的二进制字段。这样,解释记录内容的任务就完全依靠应用程序来完成。RMS 只提供存储和惟一标识符。虽然这样分工导致应用程序趋于复杂化,但是它使得 RMS 小巧灵活——这是作为 MIDP 子系统的一项重要属性。
在 API 级别中,记录是简单的字节数组。
记录存储
记录存储是记录的有序集合。记录不是独立的实体:每一条记录必须从属于一个记录存储,而且所有的记录存取都通过记录存储来实现。事实上,记录存储保证记录读写的自动运行,不会发生数据损坏。
在创建一条记录时,记录存储为其分配一个惟一的标识符。该标识符是称作记录 ID 的整数。加入记录存储的第一条记录的记录 ID 是 1,第二条的记录 ID 是 2,依此类推。记录 ID不是索引:记录删除后不会对现存的记录进行重新编号,也不会影响后面记录的记录 ID 值。
在 MIDlet 套件中使用名称来标记记录存储。一个记录存储名可以包括 1 到 32 位 Unicode 字符,而且在创建该记录存储的 MIDlet 套件中该名称必须惟一。在 MIDP1.0 中,记录存储不能被不同的 MIDlet 套件共享。在MIDP2.0 里面,则允许 MIDlet 套件与其他套件共享同一个记录存储,其中此记录存储由 MIDlet 套件的名称,销售商的名称以及记录存储自己的名称共同加以识别。
记录存储也具有时间戳记和版本信息,所以应用程序能够知道记录存储最后被修改的时间。为了进一步紧密跟踪,应用程序可以注册监听程序以便及时获知记录存储何时被修改。
在 API level 中,用javax.microedition.rms.RecordStore
类的实例来代表记录存储。所有的RMS类和接口在javax.microedition.rms
包中都又定义。
RMS 外观
在我们看一些代码之前,先回顾一下几个有关 RMS 的关键信息。
存储容量限制
基于记录的数据存储的有效存储空间总量,因设备的不同而不同。MIDP 规范要求设备为持久数据存储,保留至少 8k 非易失性存储空间。该规范没有对个别记录的大小加以限制,可是空间限制会随着设备的不同而发生改变。RMS 提供多种方式决定单条记录的大小,记录存储的总容量,以及保留多少空间用于数据存储。记住持久性存储器是共享的,是珍贵的资源,因此请节约使用。
任何使用 RMS 的 MIDlet 套件,都应该通过在 JAR 的清单(manifest)文件和应用程序描述符中设置 MIDlet 数据大小(
MIDlet-Data-Size
)属性,指定所需的数据存储空间的最小字节数。不要使设置的值大于必须的容量,因为设备有可能拒绝安装那些数据存储要求超过有效空间的 MIDlet 套件。如果该属性缺失,那么设备将会假定此MIDlet 套件不需要数据存储空间。实际上,大多数设备允许应用程序超过它们规定的空间要求,但是请不要依赖这种方式。
需要指出,某些 MIDP 的实现需要定义其他的一些有关存储需求的属性——请查阅设备文档了解细节。
速度
对持久性存储器操作所需的时间,通常都要比对易失性(非持久性)存储器进行相同操作所需的时间更长。特别是写数据,在某些平台上写数据会持续很长时间。为了提高性能,不是从 MIDlet 事件线程中来完成 RMS 操作,而是利用缓存频繁访问易失性存储器中的数据以保证用户接口反应迅速。
线程安全性
RMS 操作具有很高的线程安全性,但对任何共享资源而言,各个线程仍必须协调对同一记录存储的数据读写操作。这种协调要求需要采用在不同 MIDlets 中运行的线程,因为在同一个 MIDlet 套件中记录存储是共享的。
异常
一般说来,RMS API 中的方法除了会抛出诸如java.lang.IllegalArgumentException
之类的标准运行时异常之外,还会抛出一个或多个经检查的异常。RMS 异常都包括在javax.microedition.rms
包内:
InvalidRecordIDException
在由于记录 ID 无效而无法执行某个操作时抛出。
RecordStoreFullException
在记录存储中没有足够的可用空间时抛出。
RecordStoreNotFoundException
在应用程序试图打开一个不存在的记录存储时抛出。
RecordStoreNotOpenException
应用程序试图访问已经关闭的记录存储时抛出。
RecordStoreException
是其他 4 个异常的总集,并且在出现它们未包括的一般错误时抛出。
注意,为了精简,这一系列文章中的代码里面的异常处理都被简化或者省略了。
使用 RMS
剩下的文章内容讲述的是使用 RMS API 时对记录的一些基本操作。某些操作贯穿于一个工具类 RMSAnalyzer 的开发中,用于记录存储分析。可以将 RMSAnalyzer
用作自己项目的调试诊断程序。
记录存储探究
可以通过调用RecordStore.listRecordStores()
获得 MIDlet 套件中记录存储的列表。这个静态方法返回一个字符串数组,其中每一个字符串代表该 MIDlet 套件中的一个记录存储的名称。如果没有记录存储,则返回为null。
方法 RMSAnalyzer.analyzeAll()
使用 listRecordStores()
为套件中的每个记录存储调用analyze()
:
<PRE class=java5><span>public</SPAN><span>void</SPAN>analyzeAll<span>(</SPAN><span>)</SPAN><span>{</SPAN><span>String</SPAN><span>[</SPAN><span>]</SPAN>names = RecordStore.<span>listRecordStores</SPAN><span>(</SPAN><span>)</SPAN>;<span>for</SPAN><span>(</SPAN><span>int</SPAN>i =<span>0</SPAN>; names !=<span>null</SPAN>&& i < names.<span>length</SPAN>; ++i<span>)</SPAN><span>{</SPAN>analyze<span>(</SPAN>names<span>[</SPAN>i<span>]</SPAN><span>)</SPAN>;<span>}</SPAN><span>}</SPAN></PRE>
注意,此数套件标识的只是自身 MIDlet 套件的记录存储。也就是说创建它们的那一个套件。MIDP 规范无论如何不包括列出其他 MIDP 套件记录存储的功能。在 MIDP 1.0 中,自身套件以外的记录存储是根本看不到的。在 MIDP 2.0,自己的套件可以将记录存储指派为可共享的,但其他 MIDlet 套件只有在知道其名称后才能使用它。
打开和关闭记录存储
RecordStore.openRecordStore()
用于打开一个记录存储,也可以用于有选择地创建一个记录存储。该静态方法返回一个 RecordStore 对象的实例,如下面的 RMSAnalyzer.analyze()
版本所示:
<PRE class=java5><span>public</SPAN><span>void</SPAN>analyze<span>(</SPAN><span>String</SPAN>rsName<span>)</SPAN><span>{</SPAN>RecordStore rs =<span>null</SPAN>;<span>try</SPAN><span>{</SPAN>rs = RecordStore.<span>openRecordStore</SPAN><span>(</SPAN>rsName,<span>false</SPAN><span>)</SPAN>; analyze<span>(</SPAN>rs<span>)</SPAN>;<span>// call overloaded method</SPAN><span>}</SPAN><span>catch</SPAN><span>(</SPAN>RecordStoreException e<span>)</SPAN><span>{</SPAN>logger.<span>exception</SPAN><span>(</SPAN>rsName, e<span>)</SPAN>;<span>}</SPAN><span>finally</SPAN><span>{</SPAN><span>try</SPAN><span>{</SPAN>rs.<span>closeRecordStore</SPAN><span>(</SPAN><span>)</SPAN>;<span>}</SPAN><span>catch</SPAN><span>(</SPAN>RecordStoreException e<span>)</SPAN><span>{</SPAN><span>// Ignore this exception</SPAN><span>}</SPAN><span>}</SPAN><span>}</SPAN></PRE>
openRecordStore()
的第二个参数表示若该记录存储不存在,是否应该创建它。假如使用的是 MIDP 2.0,并且希望 MIDlet 打开由其他套件建立的记录存储,就可以用以下形式的openRecordStore()
:
<PRE class=java5>...<span>String</SPAN>name =<span>"mySharedRS"</SPAN>;<span>String</SPAN>vendor =<span>"EricGiguere.com"</SPAN>;<span>String</SPAN>suite =<span>"TestSuite"</SPAN>;RecordStore rs = RecordStore.<span>openRecordStore</SPAN><span>(</SPAN>name, vendor, suite<span>)</SPAN>;...</PRE>
销售商和套件的名称,必须与 MIDlet 套件的清单(manifest)文件和应用程序描述符所定义的名称相匹配。
完成对记录存储的操作之后,调用RecordStore.closeRecordStore()
将其关闭,与analyze()
方法一样。
RecordStore
实例在一个MIDlet 套件中是惟一的:它一旦被打开,以后用同一名称调用openRecordStore()
将会返回相同的对象引用。这个实例被该 MIDlet 套件的所有 MIDlet 共享。
每一个RecordStore
实例都会跟踪它被打开的次数。除非调用相同次数的closeRecordStore()
,否则记录存储不会被真正关闭。在记录存储关闭之后使用它会抛出RecordStoreNotOpenException。
创建记录存储
调用 openRecordStore()
并将第二个参数设为真来创建一个专用的记录存储:
<PRE class=java5>...<span>// Create a record store</SPAN>RecordStore rs =<span>null</SPAN>;<span>try</SPAN><span>{</SPAN>rs = RecordStore.<span>openRecordStore</SPAN><span>(</SPAN><span>"myrs"</SPAN>,<span>true</SPAN><span>)</SPAN>;<span>}</SPAN><span>catch</SPAN><span>(</SPAN>RecordStoreException e<span>)</SPAN><span>{</SPAN><span>// couldn't open it or create it</SPAN><span>}</SPAN>...</PRE>
要执行记录存储的一次性初始化,请在打开记录存储后,立即检查getNextRecordID()
是否等于1:
<PRE class=java5><span>if</SPAN><span>(</SPAN>rs.<span>getNextRecordID</SPAN><span>(</SPAN><span>)</SPAN>==<span>1</SPAN><span>)</SPAN><span>{</SPAN><span>// perform. one-time initialization</SPAN><span>}</SPAN></PRE>
或者,要在某个记录存储一旦为空就重新初始化该记录存储,请检查getNumRecords()
返回的值:
<PRE class=java5><span>if</SPAN><span>(</SPAN>rs.<span>getNumRecords</SPAN><span>(</SPAN><span>)</SPAN>==<span>0</SPAN><span>)</SPAN><span>{</SPAN><span>// record store is empty, re-initialize</SPAN><span>}</SPAN></PRE>
要创建共享的记录存储(只在 MIDP 2.0 中),请使用openRecordStore()
的四个参数变量:
<PRE class=java5><span>int</SPAN>authMode = RecordStore.<span>AUTHMODE_ANY</SPAN>;<span>boolean</SPAN>writable =<span>true</SPAN>;rs = RecordStore.<span>openRecordStore</SPAN><span>(</SPAN><span>"myrs"</SPAN>,<span>true</SPAN>, authMode, writable<span>)</SPAN>;</PRE>
当第二个参数为真且该记录存储已经不存在,则最后两个参数控制它的授权模式以及可写性。授权模式决定其他 MIDlet 套件能否访问该记录存储。有两种可能的模式分别为RecordStore.AUTHMODE_PRIVATE
(只有自身 MIDlet 套件有权访问)和RecordStore.AUTHMODE_ANY
(任何 MIDlet 套件都有权访问)。可写性标记决定其他 MIDlet 套件可否修改此记录存储——如果为假,它们只能从该记录存储中读取数据。
注意自身 MIDlet 套件能够使用RecordStore.setMode
随时更改记录存储的权限模式和可写性:
<PRE class=java5>rs.<span>setMode</SPAN><span>(</SPAN>RecordStore.<span>AUTHMODE_ANY</SPAN>,<span>false</SPAN><span>)</SPAN>;</PRE>
实际上,最好用AUTHMODE_PRIVATE
建立一个共享记录存储,并且在初始化之后将其发布。
添加与更新记录
还记得记录是字节数组吧。使用RecordStore.addRecord()
添加新的记录到打开的记录存储:
<PRE class=java5>...<span>byte</SPAN><span>[</SPAN><span>]</SPAN>data =<span>new</SPAN><span>byte</SPAN><span>[</SPAN><span>]</SPAN><span>{</SPAN><span>0</SPAN>,<span>1</SPAN>,<span>2</SPAN>,<span>3</SPAN><span>}</SPAN>;<span>int</SPAN>recordID;recordID = rs.<span>addRecord</SPAN><span>(</SPAN>data,<span>0</SPAN>, data.<span>length</SPAN><span>)</SPAN>;...</PRE>
通过将第一个参数设为null 来添加一个空记录。
第二个和第三个参数指定了数组的开始偏移量及从该偏移量开始要存储的总字节数。添加成功后返回新纪录的 ID,否则抛出类似于RecordStoreFullException
的异常。
可以用RecordStore.setRecord() 随时更新记录:
<PRE class=java5>...<span>int</SPAN>recordID = ...;<span>// some record ID</SPAN><span>byte</SPAN><span>[</SPAN><span>]</SPAN>data =<span>new</SPAN><span>byte</SPAN><span>[</SPAN><span>]</SPAN><span>{</SPAN><span>0</SPAN>,<span>10</SPAN>,<span>20</SPAN>,<span>30</SPAN><span>}</SPAN>;rs.<span>setRecord</SPAN><span>(</SPAN>recordID, data,<span>1</SPAN>,<span>2</SPAN><span>)</SPAN>;<span>// replaces all data in record with 10, 20</SPAN>...</PRE>
不能批次添加或者更新记录:您必须在内存中作为字节数组的形式构建整个记录,而且使用一次调用来添加或更新记录。
您能通过调用RecordStore.getNextRecordID(),找出下一个记录标识符调用 addRecordStore() 时会返回什么值。
所有当前的记录标识符都会小于这个值。
在第二篇文章中我们将着眼于将对象和其他数据转化为字节数组的策略。
读取记录
可以利用 RecordStore.getRecord() 的两种形式中的一种读取记录。
第一种形式分配合适大小的字节数组并将记录数据复制到里面:
<PRE class=java5>...<span>int</SPAN>recordID = ....<span>// some record ID</SPAN><span>byte</SPAN><span>[</SPAN><span>]</SPAN>data = rs.<span>getRecord</SPAN><span>(</SPAN>recordID<span>)</SPAN>;...</PRE>
第二种形式是从指定的偏移量开始,将数据复制到预分配的数组中并返回所复制的字节数量:
<PRE class=java5>...<span>int</SPAN>recordID = ...;<span>// some record ID</SPAN><span>byte</SPAN><span>[</SPAN><span>]</SPAN>data = ...;<span>// an array</SPAN><span>int</SPAN>offset = ...;<span>// the starting offset</SPAN><span>int</SPAN>numCopied = rs.<span>getRecord</SPAN><span>(</SPAN>recordID, data, offset<span>)</SPAN>;...</PRE>
数组必须有足够的容量来容纳数据,否则会抛出 java.lang.ArrayIndexOutOfBoundsException
。使用RecordStore.getRecordSize()
返回的值指定一个足够大的数组。实际上,etRecord()
的第一种形式相当于:
<PRE class=java5>...<span>byte</SPAN><span>[</SPAN><span>]</SPAN>data =<span>new</SPAN><span>byte</SPAN><span>[</SPAN>rs.<span>getRecordSize</SPAN><span>(</SPAN>recordID<span>)</SPAN><span>]</SPAN>;rs.<span>getRecord</SPAN><span>(</SPAN>recordID, data,<span>0</SPAN><span>)</SPAN>;...</PRE>
第二种形式在对于一系列记录的迭代操作时,进行最小化存储器分配工作很有帮助。例如,可以使用它和getNextRecordID()及
getRecordSize()
一起来执行在记录存储中对所有记录的强制搜索:
<PRE class=java5>...<span>int</SPAN>nextID = rs.<span>getNextRecordID</SPAN><span>(</SPAN><span>)</SPAN>;<span>byte</SPAN><span>[</SPAN><span>]</SPAN>data =<span>null</SPAN>;<span>for</SPAN><span>(</SPAN><span>int</SPAN>id =<span>0</SPAN>; id < nextID; ++id<span>)</SPAN><span>{</SPAN><span>try</SPAN><span>{</SPAN><span>int</SPAN>size = rs.<span>getRecordSize</SPAN><span>(</SPAN>id<span>)</SPAN>;<span>if</SPAN><span>(</SPAN>data ==<span>null</SPAN>|| data.<span>length</SPAN>< size<span>)</SPAN><span>{</SPAN>data =<span>new</SPAN><span>byte</SPAN><span>[</SPAN>size<span>]</SPAN>;<span>}</SPAN>rs.<span>getRecord</SPAN><span>(</SPAN>id, data,<span>0</SPAN><span>)</SPAN>;processRecord<span>(</SPAN>rs, id, data, size<span>)</SPAN>;<span>// process it</SPAN><span>}</SPAN><span>catch</SPAN><span>(</SPAN>InvalidRecordIDException e<span>)</SPAN><span>{</SPAN><span>// ignore, move to next record</SPAN><span>}</SPAN><span>catch</SPAN><span>(</SPAN>RecordStoreException e<span>)</SPAN><span>{</SPAN>handleError<span>(</SPAN>rs, id, e<span>)</SPAN>;<span>// call an error routine</SPAN><span>}</SPAN><span>}</SPAN>...</PRE>
但是,更好的方法是使用RecordStore.enumerateRecords()
对记录进行迭代操作。我的第三篇文章将讨论 enumerateRecords()
的使用。
删除记录和记录存储
用RecordStore.deleteRecord()
删除记录:
<PRE class=java5>...<span>int</SPAN>recordID = ...;<span>// some record ID</SPAN>rs.<span>deleteRecord</SPAN><span>(</SPAN>recordID<span>)</SPAN>;...</PRE>
一旦记录被删除,任何使用它的企图都会抛出InvalidRecordIDException
。
您用 RecordStore.deleteRecordStore()
删除记录存储:
<PRE class=java5>...<span>try</SPAN><span>{</SPAN>RecordStore.<span>deleteRecordStore</SPAN><span>(</SPAN><span>"myrs"</SPAN><span>)</SPAN>;<span>}</SPAN><span>catch</SPAN><span>(</SPAN>RecordStoreNotFoundException e<span>)</SPAN><span>{</SPAN><span>// no such record store</SPAN><span>}</SPAN><span>catch</SPAN><span>(</SPAN>RecordStoreException e<span>)</SPAN><span>{</SPAN><span>// somebody has it open</SPAN><span>}</SPAN>...</PRE>
记录存储只有当其未打开时才能被删除,并且只能由自身 MIDlet 套件中的 MIDlet 来执行。
其他操作
剩下还有几个 RMS 操作,它们都是属于RecordStore
类的方法:
getLastModified()
返回上次修改记录存储的时间,与System.currentTimeMillis()
返回的格式相同。
getName()
返回记录存储的名称。
getNumRecords()
返回记录存储中的记录数。
getSize()
以字节为单位返回记录存储的总大小。总量包括所有记录的总大小以及系统执行记录存储所需的额外开销。
getSizeAvailable()
返回记录存储能增加的可用字节数。注意实际的可用大小可能比存储个别记录花费的额外开销要小。
getVersion()
返回记录存储的版本号。版本号是大于零的正整数,每次记录存储更改后都会增加。
MIDlet 也能使用addRecordListener()
注册监听程序来跟踪记录存储的改变,然后可以用removeRecordListener() 撤销注册。
我将会在第三篇文章中讨论这些监听程序。
RMSAnalyzer
类
第一篇以RMSAnalyzer
类——记录存储的分析器——的源代码作为结束。下面是分析记录存储的代码:
<PRE class=java5>...<span>RecordStore</SPAN>rs = ...;<span>// open the record store</SPAN>RMSAnalyzer analyzer =<span>new</SPAN>RMSAnalyzer<span>(</SPAN><span>)</SPAN>;analyzer.<span>analyze</SPAN><span>(</SPAN>rs<span>)</SPAN>;...</PRE>
默认情况下,分析程序转至System.out
stream 流,请看下面的代码:
<PRE class=java5>=========================================Record store: recordstore2<span>Number</SPAN>of records =<span>4</SPAN>Total size =<span>304</SPAN>Version =<span>4</SPAN>Last modified =<span>1070745507485</SPAN>Size available =<span>975950</SPAN>Record #<span>1</SPAN>of length<span>56</SPAN>bytes5f<span>62</SPAN><span>06</SPAN><span>75</SPAN>2e 6b 1c<span>42</SPAN><span>58</SPAN>3f _b.<span>u</SPAN>.<span>k</SPAN>.<span>BX</SPAN>?1e 2e 6a<span>24</SPAN><span>74</SPAN><span>29</SPAN>7c<span>56</SPAN><span>30</SPAN><span>32</SPAN>..<span>j</SPAN>$t<span>)</SPAN>|V025f<span>67</SPAN>5a<span>13</SPAN><span>47</SPAN>7a<span>77</SPAN><span>68</SPAN>7d<span>49</SPAN>_gZ.<span>Gzwh</SPAN><span>}</SPAN>I<span>50</SPAN><span>74</SPAN><span>50</SPAN><span>20</SPAN>6b<span>14</SPAN><span>78</SPAN><span>60</SPAN><span>58</SPAN>4b PtP k.<span>x</SPAN>`XK1a<span>61</SPAN><span>67</SPAN><span>20</SPAN><span>53</SPAN><span>65</SPAN>0a 2f<span>23</SPAN>2b .<span>ag</SPAN>Se./#+<span>16</SPAN><span>42</SPAN><span>10</SPAN>4e<span>37</SPAN>6f .<span>B</SPAN>.<span>N7o</SPAN>Record #<span>2</SPAN>of length<span>35</SPAN>bytes<span>22</SPAN>4b<span>19</SPAN><span>22</SPAN><span>15</SPAN>7d<span>74</SPAN>1f<span>65</SPAN><span>26</SPAN><span>"K."</SPAN>.<span>}</SPAN>t.<span>e</SPAN>&4e 1e<span>50</SPAN><span>62</SPAN><span>50</SPAN>6e 4f<span>47</SPAN>6a<span>26</SPAN>N.<span>PbPnOGj</SPAN>&<span>31</SPAN><span>11</SPAN><span>74</SPAN><span>36</SPAN>7a 0a<span>33</SPAN><span>51</SPAN><span>61</SPAN>0e<span>1</SPAN>.<span>t6z</SPAN>.3Qa.<span>04</SPAN><span>75</SPAN>6a 2a 2a .<span>uj**</SPAN>Record #<span>3</SPAN>of length<span>5</SPAN>bytes<span>47</SPAN><span>04</SPAN><span>43</SPAN><span>22</SPAN>1f G.<span>C</SPAN><span>". Record #4 of length 57 bytes6b 6f 42 1d 5b 65 2f 72 0f 7a koB.[e/r.z2a 6e 07 57 51 71 5f 68 4c 5c *n.WQq_hL<span>\</SPAN>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(<span>\J</SPAN>N!WMu80 68 06 26 3b 77 33 ?h.&;w3Actual size of records = 153-----------------------------------------</SPAN></PRE>
这种格式在使用 J2ME 无线工具包测试时很方便,
电脑资料
《了解MIDP记录管理系统》()。为了在实际设备上测试,可以将分析结果发送到串口或者直接通过网络传输至 servlet。您可以通过定义一个自己的类来达到这个目的。这个类实现
RMSAnalyzer.Logger
接口并将该类的一个实例传输到
RMSAnalyzer
构造程序。
附在本文之后的是一个称为RMSAnalyzerTest
用于示范分析程序使用的 J2ME 无线工具包项目:
<PRE class=java5><span>package</SPAN>com.<span>ericgiguere</SPAN>;<span>import</SPAN>java.<span>io</SPAN>.<span>*</SPAN>;<span>import</SPAN>javax.<span>microedition</SPAN>.<span>rms</SPAN>.<span>*</SPAN>;<span>// Analyzes the contents of a record store.</SPAN><span>// By default prints the analysis to System.out,</SPAN><span>// but you can change this by implementing your</SPAN><span>// own Logger.</SPAN><span>public</SPAN><span>class</SPAN>RMSAnalyzer<span>{</SPAN><span>// The logging interface.</SPAN><span>public</SPAN><span>interface</SPAN><span>Logger</SPAN><span>{</SPAN><span>void</SPAN>logEnd<span>(</SPAN>RecordStore rs<span>)</SPAN>;<span>void</SPAN>logException<span>(</SPAN><span>String</SPAN>name,<span>Throwable</SPAN>e<span>)</SPAN>;<span>void</SPAN>
;logException<span>(</SPAN>RecordStore rs,<span>Throwable</SPAN>e<span>)</SPAN>;<span>void</SPAN>logRecord<span>(</SPAN>RecordStore rs,<span>int</SPAN>id,<span>byte</SPAN><span>[</SPAN><span>]</SPAN>data,<span>int</SPAN>size<span>)</SPAN>;<span>void</SPAN>logStart<span>(</SPAN>RecordStore rs<span>)</SPAN>;<span>}</SPAN><span>private</SPAN><span>Logger</SPAN>logger;<span>// Constructs an analyzer that logs to System.out.</SPAN><span>public</SPAN>RMSAnalyzer<span>(</SPAN><span>)</SPAN><span>{</SPAN><span>this</SPAN><span>(</SPAN><span>null</SPAN><span>)</SPAN>;<span>}</SPAN><span>// Constructs an analyzer that logs to the given logger.</SPAN><span>public</SPAN>RMSAnalyzer<span>(</SPAN><span>Logger</SPAN>logger<span>)</SPAN><span>{</SPAN><span>this</SPAN>.<span>logger</SPAN>=<span>(</SPAN>logger !=<span>null</SPAN><span>)</SPAN>? logger :<span>new</SPAN>SystemLogger<span>(</SPAN><span>)</SPAN>;<span>}</SPAN><span>// Open the record stores owned by this MIDlet suite</SPAN><span>// and analyze their contents.</SPAN><span>public</SPAN><span>void</SPAN>analyzeAll<span>(</SPAN><span>)</SPAN><span>{</SPAN><span>String</SPAN><span>[</SPAN><span>]</SPAN>names = RecordStore.<span>listRecordStores</SPAN><span>(</SPAN><span>)</SPAN>;<span>for</SPAN><span>(</SPAN><span>int</SPAN>i =<span>0</SPAN>; names !=<span>null</SPAN>&& i < names.<span>length</SPAN>; ++i<span>)</SPAN><span>{</SPAN>analyze<span>(</SPAN>names<span>[</SPAN>i<span>]</SPAN><span>)</SPAN>;<span>}</SPAN><span>}</SPAN><span>// Open a record store by name and analyze its contents.</SPAN><span>public</SPAN><span>void</SPAN>analyze<span>(</SPAN><span>String</SPAN>rsName<span>)</SPAN><span>{</SPAN>RecordStore rs =<span>null</SPAN>;<span>try</SPAN><span>{</SPAN>rs = RecordStore.<span>openRecordStore</SPAN><span>(</SPAN>rsName,<span>false</SPAN><span>)</SPAN>; analyze<span>(</SPAN>rs<span>)</SPAN>;<span>}</SPAN><span>catch</SPAN><span>(</SPAN>RecordStoreException e<span>)</SPAN><span>{</SPAN>logger.<span>logException</SPAN><span>(</SPAN>rsName, e<span>)</SPAN>;<span>}</SPAN><span>finally</SPAN><span>{</SPAN><span>try</SPAN><span>{</SPAN>rs.<span>closeRecordStore</SPAN><span>(</SPAN><span>)</SPAN>;<span>}</SPAN><span>catch</SPAN><span>(</SPAN>RecordStoreException e<span>)</SPAN><span>{</SPAN><span>// Ignore this exception</SPAN><span>}</SPAN><span>}</SPAN><span>}</SPAN><span>// Analyze the contents of an open record store using</SPAN><span>// a simple brute force search through the record store.</SPAN><span>public</SPAN><span>synchronized</SPAN><span>void</SPAN>analyze<span>(</SPAN>RecordStore rs<span>)</SPAN><span>{</SPAN><span>try</SPAN><span>{</SPAN>logger.<span>logStart</SPAN><span>(</SPAN>rs<span>)</SPAN>;<span>int</SPAN>lastID = rs.<span>getNextRecordID</SPAN><span>(</SPAN><span>)</SPAN>;<span>int</SPAN>numRecords = rs.<span>getNumRecords</SPAN><span>(</SPAN><span>)</SPAN>;<span>int</SPAN>count =<span>0</SPAN>;<span>byte</SPAN><span>[</SPAN><span>]</SPAN>data =<span>null</SPAN>;<span>for</SPAN><span>(</SPAN><span>int</SPAN>id =<span>0</SPAN>; id < lastID && count < numRecords; ++id<span>)</SPAN><span>{</SPAN><span>try</SPAN><span>{</SPAN><span>int</SPAN>size = rs.<span>getRecordSize</SPAN><span>(</SPAN>id<span>)</SPAN>;<span>// Make sure data array is big enough,</SPAN><span>// plus add some for growth</SPAN><span>if</SPAN><span>(</SPAN>data ==<span>null</SPAN>|| data.<span>length</SPAN>< size<span>)</SPAN><span>{</SPAN>data =<span>new</SPAN><span>byte</SPAN><span>[</SPAN>size +<span>20</SPAN><span>]</SPAN>;<span>}</SPAN>rs.<span>getRecord</SPAN><span>(</SPAN>id, data,<span>0</SPAN><span>)</SPAN>; logger.<span>logRecord</SPAN><span>(</SPAN>rs, id, data, size<span>)</SPAN>;++count;<span>// only increase if record exists</SPAN><span>}</SPAN><span>catch</SPAN><span>(</SPAN>InvalidRecordIDException e<span>)</SPAN><span>{</SPAN><span>// just ignore and move to the next one</SPAN><span>}</SPAN><span>catch</SPAN><span>(</SPAN>RecordStoreException e<span>)</SPAN><span>{</SPAN>logger.<span>logException</SPAN><span>(</SPAN>rs, e<span>)</SPAN>;<span>}</SPAN><span>}</SPAN><span>}</SPAN><span>catch</SPAN><span>(</SPAN>RecordStoreException e<span>)</SPAN><span>{</SPAN>logger.<span>logException</SPAN><span>(</SPAN>rs, e<span>)</SPAN>;<span>}</SPAN><span>finally</SPAN><span>{</SPAN>logger.<span>logEnd</SPAN><span>(</SPAN>rs<span>)</SPAN>;<span>}</SPAN><span>}</SPAN><span>// A logger that outputs to a PrintStream.</SPAN><span>public</SPAN><span>static</SPAN><span>class</SPAN>PrintStreamLogger<span>implements</SPAN><span>Logger</SPAN><span>{</SPAN><span>public</SPAN><span>static</SPAN><span>final</SPAN><span>int</SPAN>COLS_MIN =<span>10</SPAN>;<span>public</SPAN><span>static</SPAN><span>final</SPAN><span>int</SPAN>COLS_DEFAULT =<span>20</SPAN>;<span>private</SPAN><span>int</SPAN>cols;<span>private</SPAN><span>int</SPAN>numBytes;<span>private</SPAN><span>StringBuffer</SPAN>hBuf;<span>private</SPAN><span>StringBuffer</SPAN>cBuf;<span>private</SPAN><span>StringBuffer</SPAN>pBuf;<span>private</SPAN><span>PrintStream</SPAN>out;<span>public</SPAN>PrintStreamLogger<span>(</SPAN><span>PrintStream</SPAN>out<span>)</SPAN><span>{</SPAN><span>this</SPAN><span>(</SPAN>out, COLS_DEFAULT<span>)</SPAN>;<span>}</SPAN><span>public</SPAN>PrintStreamLogger<span>(</SPAN><span>PrintStream</SPAN>out,<span>int</SPAN>cols<span>)</SPAN><span>{</SPAN><span>this</SPAN>.<span>out</SPAN>= out;<span>this</SPAN>.<span>cols</SPAN>=<span>(</SPAN>cols > COLS_MIN ? cols : COLS_MIN<span>)</SPAN>;<span>}</SPAN><span>private</SPAN><span>char</SPAN>convertChar<span>(</SPAN><span>char</SPAN>ch<span>)</SPAN><span>{</SPAN><span>if</SPAN><span>(</SPAN>ch < 0x20<span>)</SPAN><span>return</SPAN><span>'.'</SPAN>;<span>return</SPAN>ch;<span>}</SPAN><span>public</SPAN><span>void</SPAN>logEnd<span>(</SPAN>RecordStore rs<span>)</SPAN><span>{</SPAN>out.<span>println</SPAN><span>(</SPAN><span>"<span>\n</SPAN>Actual size of records = "</SPAN>+ numBytes<span>)</SPAN>; printChar<span>(</SPAN><span>'-'</SPAN>, cols *<span>4</SPAN>+<span>1</SPAN><span>)</SPAN>;hBuf =<span>null</SPAN>; cBuf =<span>null</SPAN>; pBuf =<span>null</SPAN>;<span>}</SPAN><span>public</SPAN><span>void</SPAN>logException<span>(</SPAN><span>String</SPAN>name,<span>Throwable</SPAN>e<span>)</SPAN><span>{</SPAN>out.<span>println</SPAN><span>(</SPAN><span>"Exception while analyzing "</SPAN>+ name +<span>": "</SPAN>+ e<span>)</SPAN>;<span>}</SPAN><span>public</SPAN><span>void</SPAN>logException<span>(</SPAN>RecordStore rs,<span>Throwable</SPAN>e<span>)</SPAN><span>{</SPAN><span>String</SPAN>name;<span>try</SPAN><span>{</SPAN>name = rs.<span>getName</SPAN><span>(</SPAN><span>)</SPAN>;<span>}</SPAN><span>catch</SPAN><span>(</SPAN>RecordStoreException rse<span>)</SPAN><span>{</SPAN>name =<span>""</SPAN>;<span>}</SPAN>logException<span>(</SPAN>name, e<span>)</SPAN>;<span>}</SPAN><span>public</SPAN><span>void</SPAN>logRecord<span>(</SPAN>RecordStore rs,<span>int</SPAN>id,<span>byte</SPAN><span>[</SPAN><span>]</SPAN>data,<span>int</SPAN>len<span>)</SPAN><span>{</SPAN><span>if</SPAN><span>(</SPAN>len <<span>0</SPAN>&& data !=<span>null</SPAN><span>)</SPAN><span>{</SPAN>len = data.<span>length</SPAN>;<span>}</SPAN>hBuf.<span>setLength</SPAN><span>(</SPAN><span>0</SPAN><span>)</SPAN>; cBuf.<span>setLength</SPAN><span>(</SPAN><span>0</SPAN><span>)</SPAN>;numBytes += len;out.<span>println</SPAN><span>(</SPAN><span>"Record #"</SPAN>+ id +<span>" of length "</SPAN>+ len +<span>" bytes"</SPAN><span>)</SPAN>;<span>for</SPAN><span>(</SPAN><span>int</SPAN>i =<span>0</SPAN>; i < len; ++i<span>)</SPAN><span>{</SPAN><span>int</SPAN>b =<span>Math</SPAN>.<span>abs</SPAN><span>(</SPAN>data<span>[</SPAN>i<span>]</SPAN><span>)</SPAN>;<span>String</SPAN>hStr =<span>Integer</SPAN>.<span>toHexString</SPAN><span>(</SPAN>b<span>)</SPAN>;<span>if</SPAN><span>(</SPAN>b < 0x10<span>)</SPAN><span>{</SPAN>hBuf.<span>append</SPAN><span>(</SPAN><span>'0'</SPAN><span>)</SPAN>;<span>}</SPAN>hBuf.<span>append</SPAN><span>(</SPAN>hStr<span>)</SPAN>; hBuf.<span>append</SPAN><span>(</SPAN><span>' '</SPAN><span>)</SPAN>;cBuf.<span>append</SPAN><span>(</SPAN>convertChar<span>(</SPAN><span>(</SPAN><span>char</SPAN><span>)</SPAN>b<span>)</SPAN><span>)</SPAN>;<span>if</SPAN><span>(</SPAN>cBuf.<span>length</SPAN><span>(</SPAN><span>)</SPAN>== cols<span>)</SPAN><span>{</SPAN>out.<span>println</SPAN><span>(</SPAN>hBuf +<span>" "</SPAN>+ cBuf<span>)</SPAN>;hBuf.<span>setLength</SPAN><span>(</SPAN><span>0</SPAN><span>)</SPAN>; cBuf.<span>setLength</SPAN><span>(</SPAN><span>0</SPAN><span>)</SPAN>;<span>}</SPAN><span>}</SPAN>len = cBuf.<span>length</SPAN><span>(</SPAN><span>)</SPAN>;<span>if</SPAN><span>(</SPAN>len ><span>0</SPAN><span>)</SPAN><span>{</SPAN><span>while</SPAN><span>(</SPAN>len++ < cols<span>)</SPAN><span>{</SPAN>hBuf.<span>append</SPAN><span>(</SPAN><span>" "</SPAN><span>)</SPAN>; cBuf.<span>append</SPAN><span>(</SPAN><span>' '</SPAN><span>)</SPAN>;<span>}</SPAN>out.<span>println</SPAN><span>(</SPAN>hBuf +<span>" "</SPAN>+ cBuf<span>)</SPAN>;<span>}</SPAN><span>}</SPAN><span>public</SPAN><span>void</SPAN>logStart<span>(</SPAN>RecordStore rs<span>)</SPAN><span>{</SPAN>hBuf =<span>new</SPAN><span>StringBuffer</SPAN><span>(</SPAN>cols *<span>3</SPAN><span>)</SPAN>; cBuf =<span>new</SPAN><span>StringBuffer</SPAN><span>(</SPAN>cols<span>)</SPAN>; pBuf =<span>new</SPAN><span>StringBuffer</SPAN><span>(</SPAN><span>)</SPAN>;printChar<span>(</SPAN><span>'='</SPAN>, cols *<span>4</SPAN>+<span>1</SPAN><span>)</SPAN>;numBytes =<span>0</SPAN>;<span>try</SPAN><span>{</SPAN>out.<span>println</SPAN><span>(</SPAN><span>"Record store: "</SPAN>+ rs.<span>getName</SPAN><span>(</SPAN><span>)</SPAN><span>)</SPAN>; out.<span>println</SPAN><span>(</SPAN><span>" Number of records = "</SPAN>+ rs.<span>getNumRecords</SPAN><span>(</SPAN><span>)</SPAN><span>)</SPAN>; out.<span>println</SPAN><span>(</SPAN><span>" Total size = "</SPAN>+ rs.<span>getSize</SPAN><span>(</SPAN><span>)</SPAN><span>)</SPAN>; out.<span>println</SPAN><span>(</SPAN><span>" Version = "</SPAN>+ rs.<span>getVersion</SPAN><span>(</SPAN><span>)</SPAN><span>)</SPAN>; out.<span>println</SPAN><span>(</SPAN><span>" Last modified = "</SPAN>+ rs.<span>getLastModified</SPAN><span>(</SPAN><span>)</SPAN><span>)</SPAN>; out.<span>println</SPAN><span>(</SPAN><span>" Size available = "</SPAN>+ rs.<span>getSizeAvailable</SPAN><span>(</SPAN><span>)</SPAN><span>)</SPAN>; out.<span>println</SPAN><span>(</SPAN><span>""</SPAN><span>)</SPAN>;<span>}</SPAN><span>catch</SPAN><span>(</SPAN>RecordStoreException e<span>)</SPAN><span>{</SPAN>logException<span>(</SPAN>rs, e<span>)</SPAN>;<span>}</SPAN><span>}</SPAN><span>private</SPAN><span>void</SPAN>printChar<span>(</SPAN><span>char</SPAN>ch,<span>int</SPAN>num<span>)</SPAN><span>{</SPAN>pBuf.<span>setLength</SPAN><span>(</SPAN><span>0</SPAN><span>)</SPAN>;<span>while</SPAN><span>(</SPAN>num-- ><span>0</SPAN><span>)</SPAN><span>{</SPAN>pBuf.<span>append</SPAN><span>(</SPAN>ch<span>)</SPAN>;<span>}</SPAN>out.<span>println</SPAN><span>(</SPAN>pBuf.<span>toString</SPAN><span>(</SPAN><span>)</SPAN><span>)</SPAN>;<span>}</SPAN><span>}</SPAN><span>// A logger that outputs to System.out.</SPAN><span>public</SPAN><span>static</SPAN><span>class</SPAN>SystemLogger<span>extends</SPAN>PrintStreamLogger<span>{</SPAN><span>public</SPAN>SystemLogger<span>(</SPAN><span>)</SPAN><span>{</SPAN><span>super</SPAN><span>(</SPAN><span>System</SPAN>.<span>out</SPAN><span>)</SPAN>;<span>}</SPAN><span>public</SPAN>SystemLogger<span>(</SPAN><span>int</SPAN>cols<span>)</SPAN><span>{</SPAN><span>super</SPAN><span>(</SPAN><span>System</SPAN>.<span>out</SPAN>, cols<span>)</SPAN>;<span>}</SPAN><span>}</SPAN></PRE>