<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    騎豬闖天下

    J2ME隨筆,記錄成長的腳步

    統(tǒng)計(jì)

    留言簿(3)

    閱讀排行榜

    評(píng)論排行榜

    [J2ME] 以對(duì)象的方式操作RMS

    原則上來說,手機(jī)上盡量不允許大批量的操作RMS,但目前這個(gè)項(xiàng)目是一個(gè)閱讀軟件,不得不把下載到的書存到手機(jī)中,這就要反復(fù)操作RMS,項(xiàng)目期間出現(xiàn)過很多問題,現(xiàn)把幾個(gè)主要的思想記錄下來,以供在以后的項(xiàng)目中做為參考。

    1.通過對(duì)已下載數(shù)據(jù)的RMS存儲(chǔ),可以實(shí)現(xiàn)文件的斷點(diǎn)續(xù)傳;
    2.通過對(duì)下載列表的記錄,可以實(shí)現(xiàn)已下載、正在下載、暫停,等待下載等的區(qū)分;
    3.因?yàn)槭欠磸?fù)操作RMS,所以對(duì)內(nèi)存的回收一定要注意,操作結(jié)束立刻要回收;
    4.因?yàn)槭欠磸?fù)操作RMS,所以每次有設(shè)計(jì)到RMS有關(guān)的操作,必然遍歷已下載或比較書名等,都要先讀取下RMS;
    5.Symbian S40系統(tǒng)的RMS非常有限,大致為1-2百k,不適合大數(shù)據(jù)存儲(chǔ),S60理論上沒有限制,但存到20M時(shí),有的機(jī)子會(huì)出問題;
    6.本代碼范例為以對(duì)象的方式反復(fù)操作RMS,如果小范圍內(nèi)使用RMS,如只記錄下用戶名密碼等,是不需要這么負(fù)責(zé)的代碼的;


    代碼范例:
    package com.xuudoo.booking;

    import
     java.io.ByteArrayInputStream;
    import
     java.io.ByteArrayOutputStream;
    import
     java.io.DataInputStream;
    import
     java.io.DataOutputStream;
    import
     java.io.IOException;
    import
     java.io.InputStream;
    import
     javax.microedition.rms.RecordStore;

    import
     com.xuudoo.ewl.EWLScreen;

    public final class RMSRecorderIfno 
    {
        
    private
     ByteArrayOutputStream dis;
        
    private
     ByteArrayInputStream is;
        
    private
     DataOutputStream dis1;
        
    private
     DataInputStream is1;
        
    public
     RecordStore rs;
        
    private
     String rmsName;
        
    public boolean
     boon;
        
    private static long
     length;
        
    private static boolean istrue = false
    ;

        
    /**
         *     RMS命名規(guī)則:
         *     RecordStore.openRecordStore(storeName, false);
         * 
         *  
    @param storeName= "002_" + bookName + "_packet";//
         *    
    @param
     storeName= "002_" + bookName + "_mgz";    //存儲(chǔ)書
         *    
    @param
     storeName= "002_" + bookName + "_info";    //
         *    
    @param
     storeName= "002_" + bookName + "_file";    //
         *    
    @param
     storeName= "002_" + bookName + "_byte";    //存儲(chǔ)每塊數(shù)據(jù)中的廢字節(jié)數(shù)
         *    
    @param
     storeName= "002_" + bookName + "_url";    //存儲(chǔ)url和一本書的總數(shù)據(jù)塊數(shù)
         *    
    @param
     storeName= "002_" + "downloaded";        //存儲(chǔ)已經(jīng)下載結(jié)束書的ID
         *    
    @param
     storeName= "002_" + "downloadpause";        //存儲(chǔ)暫停下載書的ID
         
    */

        
    public RMSRecorderIfno() {
            
    if (!istrue) 
    {
                istrue 
    = true
    ;
                length 
    =
     getAvailable();
            }

        }


        
    /**
         * 打開一個(gè)倉儲(chǔ)
         * 
    @param name:倉儲(chǔ)名字
         * 
    @param
     flag:值為true,當(dāng)不存在時(shí)創(chuàng)建一個(gè)新的store
         * 
    @return

         
    */

        
    public final boolean open(String name, boolean flag) {
            
    try 
    {
                rmsName 
    =
     name;
                rs 
    =
     RecordStore.openRecordStore(rmsName, flag);
                rs.setMode(
    0true
    );
                boon 
    = true
    ;
            }
     catch (Exception e) {
                e.printStackTrace();
                
    return false
    ;
            }

            
    return true;
        }


        
    public final void openWriteStream() {
            dis 
    = new
     ByteArrayOutputStream();
            dis1 
    = new
     DataOutputStream(dis);
        }

        
        
    public final void closeWriteStream() {
            
    try 
    {
                
    if (dis1 != null)
    {
                    dis1.close();
                    dis1 
    = null
    ;
                }

                
    if (dis != null{
                    dis.close();
                    dis 
    = null
    ;
                }

            }
     catch (IOException e) {
                e.printStackTrace();
            }

        }


        
    public final void write(int k) {
            
    try 
    {
                dis1.writeInt(k);
                
    return
    ;
            }
     catch (Exception exception) {
                exception.printStackTrace();
            }

        }


        
    public final void write(byte data[]) {
            
    try 
    {
                dis1.write(data);
                
    return
    ;
            }
     catch (Exception exception) {
                exception.printStackTrace();
            }

        }


        
    public final void write(boolean flag) {
            
    try 
    {
                dis1.writeBoolean(flag);
                
    return
    ;
            }
     catch (Exception exception) {
                exception.printStackTrace();
            }

        }


        
    public final void write(String s) {
            
    try 
    {
                dis1.writeUTF(s);
                
    return
    ;
            }
     catch (Exception exception) {
                exception.printStackTrace();
            }

        }


        
    public final void write(short word0) {
            
    try 
    {
                dis1.writeShort(word0);
                
    return
    ;
            }
     catch (Exception exception) {
                exception.printStackTrace();
            }

        }


        
    public final byte[] saveStreamToByteArray() {
            
    try 
    {
                dis1.flush();
            }
     catch (Exception exception) 
    {
                exception.printStackTrace();
            }

            
    return dis.toByteArray();
        }


        
    /**
         * 讀取位置K的數(shù)據(jù)
         * 
    @param k
         * 
    @return

         
    */

        
    public final boolean openReadStream(int k) {
            
    try 
    {
                is 
    = new
     ByteArrayInputStream(getRecord(k));
                is1 
    = new
     DataInputStream(is);
            }
     catch (Exception _ex) {
                
    if
    (BookingMidlet.DEBUG)
                    _ex.printStackTrace();
                
    return false
    ;
            }

            
    return true;
        }


        
    /**
         * 讀取整數(shù)值
         * 
    @return
         
    */

        
    public final int readInt() {
            
    try 
    {
                
    return
     is1.readInt();
            }
     catch (Exception exception) {
                
    if
    (BookingMidlet.DEBUG)
                    exception.printStackTrace();
                
    return 0
    ;
            }

        }


        
    public final boolean readBoolean() {
            
    try 
    {
                
    return
     is1.readBoolean();
            }
     catch (Exception exception) {
                exception.printStackTrace();
                
    return false
    ;
            }

        }


        
    public final String readString() {
            
    try 
    {
                
    return
     is1.readUTF();
            }
     catch (Exception exception) {
                
    return null
    ;
            }

        }


        
    /**
         * 返回?cái)?shù)據(jù)庫中的記錄條數(shù)
         * 
    @return
         
    */

        
    public final int size() {
            
    try 
    {
                
    return
     rs.getNumRecords();
            }
     catch (Exception e) {
                
    if(BookingMidlet.DEBUG)
    {
                    e.printStackTrace();
                }

                
    return -1;
            }

        }


        
    public final void setRecord(int k, byte abyte[]) {
            
    try 
    {
                rs.setRecord(k, abyte, 
    0
    , abyte.length);
                
    return
    ;
            }
     catch (Exception exception) {
                exception.printStackTrace();
            }

        }


        
    public final int addRecord(byte abyte0[]) {
            
    try 
    {
                
    return rs.addRecord(abyte0, 0
    , abyte0.length);
            }
     catch (Exception e) {
                
    if(rs != null)
    {
                    UIManager.showAlert(
    "下載失敗,儲(chǔ)存器已滿,請(qǐng)及時(shí)清理"
    );
                }

                
    if(UIManager._instance.downloadtool != null && UIManager._instance.downloadtool.conn != null){
                    UIManager._instance.downloadtool.CloseMidlet();
                    UIManager._instance.removeDownloadingItem(UIManager._instance.downloadtool.item);
                    UIManager._instance.removeDownloadedItem(UIManager._instance.downloadtool.item);
                    delete(
    "002_" + UIManager._instance.downloadtool.imageName + "_byte"
    );
                    delete(
    "002_" + UIManager._instance.downloadtool.imageName + "_url"
    );
                    
    if(UIManager._instance.downloadtool.rms != null)
    {
                        UIManager._instance.downloadtool.rms.close();
                        UIManager._instance.downloadtool.rms 
    = null
    ;
                    }

                    delete(
    "002_" + UIManager._instance.downloadtool.imageName + "_mgz");
                    
    if
    (BookingMidlet.DEBUG)
                        EWLScreen.prompt2 
    = "出錯(cuò)了,請(qǐng)注意?。?/span>";
                }

                
    return -1;
            }

        }


        
    /**
         * 讀取位置K上的字節(jié)數(shù)組數(shù)據(jù)
         * 
    @param k
         * 
    @return

         
    */

        
    public final byte[] getRecord(int k) {
            
    try 
    {
                
    return
     rs.getRecord(k);
            }
     catch (Exception exception) {
                exception.printStackTrace();
                exception.printStackTrace();
                
    return null
    ;
            }

        }


        
    public final void close() {
            
    try 
    {
                
    if (dis1 != null
    {
                    dis1.close();
                    dis1 
    = null
    ;
                }

                
    if (dis != null{
                    dis.close();
                    dis 
    = null
    ;
                }

                
    if (is1 != null{
                    is1.close();
                    is1 
    = null
    ;
                }

                
    if (is != null{
                    is.close();
                    is 
    = null
    ;
                }

                
    if (rs != null{
                    boon 
    = false
    ;
                    rs.closeRecordStore();
                    rs 
    = null
    ;
                    
    return
    ;
                }

            }
     catch (Exception _ex) {
                _ex.printStackTrace();
            }

        }


        
    public static final void delete(String s) {
            
    try 
    {
                RecordStore.deleteRecordStore(s);
                
    return
    ;
            }
     catch (Exception exception) {
                exception.printStackTrace();
            }

        }


        
    public static final boolean a(long l) {
            
    if (l > 0L && l < length) 
    {
                length 
    -=
     l;
                
    return true
    ;
            }
     else {
                
    return false
    ;
            }

        }


        
    public static final boolean b(long l) {
            
    if (l > 0L && length + l <= getAvailable()) 
    {
                length 
    +=
     l;
                
    return true
    ;
            }
     else {
                
    return false
    ;
            }

        }


        
    public static final long h() {
            
    return
     length;
        }


        
    public static final long getAvailable() {
            
    long
     l;
            RecordStore recordstore;
            
    try 
    {
                l 
    = (recordstore = RecordStore.openRecordStore("freeMemory"true
    )).getSizeAvailable();
                recordstore.closeRecordStore();
                
    return
     l;
            }
     catch (Exception exception) {
                exception.printStackTrace();
                
    return 0L
    ;
            }

        }


        
    public static final String[] getList() {
            
    try 
    {
                
    return
     RecordStore.listRecordStores();
            }
     catch (Exception exception) {
                exception.printStackTrace();
                
    return null
    ;
            }

        }


        
    public static final boolean exists(String name) {
            String rmslistname[] 
    =
     getList();
            
    try 
    {
    //            rs = RecordStore.openRecordStore(name, false);

            }
     catch (Exception e) {
                e.printStackTrace();
            }

            
    if (rmslistname == null)
                
    return false
    ;
            
    for (int k = 0; k < rmslistname.length; k++
    )
                
    if
     (rmslistname[k].equals(name))
                    
    return true
    ;
            
    return false
    ;
        }


        
    public static final synchronized void a(String s, int k) {
            
    //   Object obj1 = null;

            try {
                ByteArrayOutputStream bytearrayoutputstream 
    = new
     ByteArrayOutputStream();
                DataOutputStream obj 
    = new
     DataOutputStream(bytearrayoutputstream);
                obj.writeInt(k);
                a(s, bytearrayoutputstream.toByteArray());
                
    if (obj != null
    )
                    ((DataOutputStream) (obj)).close();
            }
     catch (Exception exception) {
                exception.printStackTrace();
            }

        }


        
    public static final synchronized void a(String s, byte abyte0[]) {
            Object obj 
    = null
    ;
            
    try 
    {
                
    if (((RecordStore) (obj = RecordStore.openRecordStore(s, true))).getNumRecords() == 0
    )
                    ((RecordStore) (obj)).addRecord(abyte0, 
    0
    , abyte0.length);
                ((RecordStore) (obj)).setRecord(
    1, abyte0, 0
    , abyte0.length);

                
    if (obj != null
    )
                    ((RecordStore) (obj)).closeRecordStore();

                
    return
    ;
            }
     catch (Exception e) {
                e.printStackTrace();
            }

        }


        
    public static final synchronized boolean d(String s) {
            String as[];
            
    if ((as = RecordStore.listRecordStores()) == null
    )
                
    return false
    ;
            
    for (int k = 0; k < as.length; k++
    )
                
    if
     (as[k].equals(s))
                    
    return true
    ;

            
    return false
    ;
        }


        
    public static final synchronized int e(String s) {
            System.out.println(
    "bn.e(string) s=" +
     s);
            Object obj;
            obj 
    = null
    ;
            
    //     Object obj1 = null;

            int k;
            
    try 
    {
                obj 
    = new
     ByteArrayInputStream(f(s));
                DataInputStream datainputstream;
                datainputstream 
    = new
     DataInputStream((InputStream) (obj));
                k 
    =
     datainputstream.readInt();
                ((ByteArrayInputStream) (obj)).close();

                
    return
     k;
            }
     catch (Exception exception) {
                
    try 
    {
                    
    if (obj != null
    )
                        ((ByteArrayInputStream) (obj)).close();
                }
     catch (Exception ex) {
                    ex.printStackTrace();
                }

                exception.printStackTrace();
                
    return 0;
            }

        }


        
    public static final synchronized byte[] f(String s) {
            System.out.println(
    "bn.f(string) s=" +
     s);
            
    byte abyte0[] = (byte[]) null
    ;
            
    try 
    {
                RecordStore obj 
    = RecordStore.openRecordStore(s, true
    );
                
    if (obj.getNumRecords() == 1
    )
                    abyte0 
    = ((RecordStore) (obj)).getRecord(1
    );
                
    if (obj != null
    )
                    obj.closeRecordStore();

                
    return
     abyte0;
            }
     catch (Exception exception) {
                exception.printStackTrace();
                
    return null
    ;
            }

        }


    }

    posted on 2010-02-25 16:41 騎豬闖天下 閱讀(299) 評(píng)論(0)  編輯  收藏


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲国产成人AV网站| 亚洲综合成人网在线观看| 免费看国产精品3a黄的视频| 色片在线免费观看| 免费看国产一级片| 亚洲熟妇丰满多毛XXXX| 久久精品国产亚洲AV蜜臀色欲 | 久久久久se色偷偷亚洲精品av| 久久青草国产免费观看| 精品国产麻豆免费网站| 亚洲成AV人片天堂网无码| 亚洲久热无码av中文字幕| 伊人免费在线观看| 午夜老司机免费视频| 亚洲男人天堂2017| 一级做α爱过程免费视频| 三年片在线观看免费大全| 亚洲成AV人片在| 免费A级毛片av无码| 亚洲午夜未满十八勿入网站2| 国产精品玖玖美女张开腿让男人桶爽免费看| av大片在线无码免费| 日韩亚洲国产综合高清| 中文字幕免费视频| 国外亚洲成AV人片在线观看| 亚洲av成本人无码网站| 曰批视频免费30分钟成人| 亚洲日韩区在线电影| 免费人成网站在线观看10分钟| 亚洲成AV人片在WWW| 亚洲三级高清免费| 精品国产亚洲第一区二区三区| 亚洲国产精品丝袜在线观看| 亚洲日本一线产区和二线产区对比| 四虎影在线永久免费四虎地址8848aa| 免费无码婬片aaa直播表情| 精品久久洲久久久久护士免费| 人人公开免费超级碰碰碰视频| 亚洲国产精品一区二区九九 | 99视频精品全部免费观看| 亚洲AV成人影视在线观看|