RMS从入门到精通之三

来源:J2ME开发网 作者:mingjava 2007-11-23 出处:pcdog.com

java  qq  
上一页 1 2 3 4 下一页 

  nbsp;   

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;

        private StringBuffer cBuf;

        private StringBuffer pBuf;

        private PrintStream  out;

         public PrintStreamLogger( PrintStream out ){

            this( out, COLS_DEFAULT );

        }

         public PrintStreamLogger( PrintStream out, int cols


更多内容请看PCdog.com--电脑入门教程专题
上一页 1 2 3 4 下一页 
上一篇:RMS从入门到精通之四
下一篇:NokiaS40 和 S60 开发平台 1.0 已知问题