前面兩篇文章詳細(xì)的介紹了Record Management System的基本概念以及對象序列化的問題,現(xiàn)在我們主要介紹關(guān)于RecordStore類的使用,在SUN的網(wǎng)站提供了一個RMSAnalyzer類,你可以把他用在你的項目中來調(diào)試你的程序。
Record Store Discovery
       你可以通過調(diào)用RecordStore.listRecordStores()來得到MIDlet suites中的Record Store,這個靜態(tài)方法返回一個String類型的數(shù)組,每個代表Record Store的名字,如果沒有record Store那么會返回null,方法RMSAnalyzer.annlyzeAll()通過調(diào)用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] );
}
}
注意到列出的數(shù)組名字是所屬M(fèi)IDlet suite的Record Store。MIDP中沒有提供列舉出任何其他MIDlet suites的Record Store的方法,在MIDP 1.0中Record Store在所屬M(fèi)IDlet suites外是不可見的,在MIDP 2.0中,MIDlet suite可以指定一個Record Store作為可共享的,但是其他的suite要知道他的名字才可以訪問它。
Opening and closing Record Store
       RecordStore.openRecordStore()是用來打開一個Record Store的,它也可以用來創(chuàng)建一個Record Store,這個靜態(tài)方法返回一個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()的第二個參數(shù)表示如果record store不存在是不是創(chuàng)建新的,在MIDP2.0中,如果你想打開一個在其他的MIDlet suite里面創(chuàng)建的Record Store的話應(yīng)該用下面的方法。
...
String name = "mySharedRS";
String vendor = "EricGiguere.com";
String suite = "TestSuite";
RecordStore rs =
RecordStore.openRecordStore( name, vendor, suite );
...
vendor和suite的名字應(yīng)該和MIDlet suite的manifest和jad的內(nèi)容一致。
當(dāng)你完成了對record store的操作以后應(yīng)該調(diào)用RecordStore.closeRecordStore()來關(guān)閉它,一個RecordStore的實(shí)例在一個MIDlet suite里面是唯一的,如果以同樣的名字再次調(diào)用openRecordStore()的話會返回同樣的實(shí)例,這樣多個MIDlet在共享一個record store,每個record store會跟蹤它被打開的次數(shù),這個record store直到被調(diào)用相同次數(shù)的closeRecordStore()后才會徹底的關(guān)閉,對一個已經(jīng)關(guān)閉的Record Store進(jìn)行操作會導(dǎo)致拋出
RecordStoreNotOpenException。
Creating Record Store
                   創(chuàng)建一個私有的record store,把第二個參數(shù)設(shè)置為true調(diào)用openRecordStore(),...
// Create a record store
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore( "myrs", true );
} catch( RecordStoreException e ){
// couldn't open it or create it
}
如果要創(chuàng)建一個可共享的record store,那么使用四個參數(shù)變量的openRecordStore()
int authMode = RecordStore.AUTHMODE_ANY;
boolean writable = true;
rs = RecordStore.openRecordStore( "myrs", true,
authMode, writable );
當(dāng)?shù)诙€參數(shù)是true并且record store不存在的時候,后面兩個參數(shù)控制他的授權(quán)模式和可寫性,授權(quán)模式?jīng)Q定是否其他的MIDlet suite具有訪問record store的權(quán)限,兩種可能的模式是RecordStore.AUTHMODE_PRIVATE(只有擁有的SUITE才可以訪問)和RecordStore.AUTHMODE_ANY(任何suite都可以訪問),可寫性控制著是否其他的suite能夠修改record store,如果false的話,那么只有所屬suite才可以修改,其他的只能讀取。注意所屬suite可以在任何時候調(diào)用RecordStore.setMode()來修改它的授權(quán)和讀寫模式,例如:
rs.setMode( RecordStore.AUTHMODE_ANY, false );事實(shí)上最好是創(chuàng)建一個record store,授權(quán)模式為RecordStore.AUTHMODE_PRIVATE。
Adding and updating records
       記錄就是字節(jié)數(shù)組,你可以通過調(diào)用RecordStore.addRecord()來添加一個新的記錄到一個打開的Record Store
byte[] data = new byte[]{ 0, 1, 2, 3 };
int    recordID;
recordID = rs.addRecord( data, 0, data.length );
如果第一個參數(shù)是null的話,那么你就能添加一個空記錄。第二個和第三個參數(shù)說明了字節(jié)數(shù)組的起點(diǎn)和從起點(diǎn)開始的總的字節(jié)數(shù)。如果添加成功會返回新記錄的record ID,如果失敗會拋出異常,例如RecordStoreFullException。
通過調(diào)用RecordStore.setRecord()可以在任何時候更新記錄的內(nèi)容。
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
你不能大塊的添加記錄,必須首先把記錄轉(zhuǎn)換成數(shù)組,然后調(diào)用添加的函數(shù),通過調(diào)用RecordStore.getNextRecordID()你可以得到下次調(diào)用addRecord()將要得到的record ID,這個值比現(xiàn)在使用的任何值都大。
Reading records
       想要讀取記錄,有兩種方法。第一是分配合適大小的數(shù)組然后把記錄的內(nèi)容復(fù)制過去。
int recordID = .... // some record ID
byte[] data = rs.getRecord( recordID );
第二種方法是把數(shù)組復(fù)制到預(yù)先分配的字節(jié)數(shù)組中,指定復(fù)制的起點(diǎn)并返回復(fù)制的字節(jié)數(shù)目。
int recordID = ...; // some record ID
byte[] data = ...; // an array
int offset = ...; // the starting offset
int numCopied = rs.getRecord( recordID, data, offset );
數(shù)組的大小必須能足夠容納數(shù)據(jù),否則會拋出java.lang.ArrayIndexOutOfBoundsException.使用RecordStore.getRecordSize()來分配數(shù)組空間是合適的方法。事實(shí)上第一個方法等價與
byte[] data = new byte[rs.getRecordSize(recordID)];
rs.getRecord(recordID,data,0);
第二種方法有利于減小內(nèi)存的分配,當(dāng)你要遍歷一組記錄的時候,你可以結(jié)合getNextRecordID()和getRecordSize()來完成運(yùn)算量很大的搜索。
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
                   你可以通過調(diào)用RecordStore.deleteRecord()來刪除記錄
int recordID = ...; // some record ID
rs.deleteRecord( recordID );
一旦記錄被刪除,任何對記錄的操作將會導(dǎo)致拋出InvalidRecordIDException,通過調(diào)用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刪除。
其他操作
       getLastModified()返回最后修改record store的時間,格式和System.currentTimeMillis()一樣。
getName()得到record store的名字。
getNumRecords()返回record store中記錄的數(shù)量。
getSize()返回record store的整個大小,包括記錄的大小和系統(tǒng)來實(shí)現(xiàn)record store的空間。
getSizeAvailable()返回record store中還能用的空間,
getVersion()返回record store的版本數(shù),這個數(shù)大于0,每次record store被修改,這個數(shù)都會自動增加。
一個MIDlet能夠通過注冊一個監(jiān)聽器來跟蹤record store,通過addRecordListener()和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中顯示,在實(shí)際的設(shè)備中進(jìn)行測試的時候,你可能希望把分析輸出到串口或者通過網(wǎng)絡(luò)發(fā)到servlet,你可以通過定義自己的類實(shí)現(xiàn)實(shí)現(xiàn)Logger接口,然后把這個類作為RMSAnalyzer構(gòu)造器的參數(shù)。下面是源代碼。
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;
private StringBuffer cBuf;
private StringBuffer pBuf;
private PrintStream out;
public PrintStreamLogger( PrintStream out ){
this( out, COLS_DEFAULT );
}
public PrintStreamLogger( PrintStream out, int cols
this.out = out;
this.cols = ( cols > COLS_MIN ? cols : COLS_MIN );
}
private char convertChar( char ch ){
if( ch < 0x20 ) return '.';
return ch;
}
public void logEnd( RecordStore rs ){
out.println( "/nActual size of records = "
+ numBytes );
printChar( '-', cols * 4 + 1 );
hBuf = null;
cBuf = null;
pBuf = null;
}
public void logException( String name, Throwable e ){
out.println( "Exception while analyzing " +
name + ": " + e );
}
public void logException( RecordStore rs, Throwable e ){
String name;
try {
name = rs.getName();
} catch( RecordStoreException rse ){
name = "";
}
logException( name, e );
}
public void logRecord( RecordStore rs, int id,
byte[] data, int len ){
if( len < 0 && data != null ){
len = data.length;
}
hBuf.setLength( 0 );
cBuf.setLength( 0 );
numBytes += len;
out.println( "Record #" + id + " of length "
+ len + " bytes" );
for( int i = 0; i < len; ++i ){
int b = Math.abs( data[i] );
String hStr = Integer.toHexString( b );
if( b < 0x10 ){
hBuf.append( '0');
}
hBuf.append( hStr );
hBuf.append( ' ' );
cBuf.append( convertChar( (char) b ) );
if( cBuf.length() == cols ){
out.println( hBuf + " " + cBuf );
hBuf.setLength( 0 );
cBuf.setLength( 0 );
}
}
len = cBuf.length();
if( len > 0 ){
while( len++ < cols ){
hBuf.append( " " );
cBuf.append( ' ' );
}
out.println( hBuf + " " + cBuf );
}
}
public void logStart( RecordStore rs ){
hBuf = new StringBuffer( cols * 3 );
cBuf = new StringBuffer( cols );
pBuf = new StringBuffer();
printChar( '=', cols * 4 + 1 );
numBytes = 0;
try {
out.println( "Record store: "
+ rs.getName() );
out.println( " Number of records = "
+ rs.getNumRecords() );
out.println( " Total size = "
+ rs.getSize() );
out.println( " Version = "
+ rs.getVersion() );
out.println( " Last modified = "
+ rs.getLastModified() );
out.println( " Size available = "
+ rs.getSizeAvailable() );
out.println( "" );
} catch( RecordStoreException e ){
logException(
}
}
private void printChar( char ch, int num ){
pBuf.setLength( 0 );
while( num-- > 0 ){
pBuf.append( ch );
}
out.println( pBuf.toString() );
}
}
// A logger that outputs to System.out.
    public static class SystemLogger extends PrintStreamLogger 
   {
public SystemLogger(){
super( System.out );
}
public SystemLogger( int cols ){
super( System.out, cols );
}
}
(出處:http://m.survivalescaperooms.com)
新聞熱點(diǎn)
疑難解答