WEB开发网
开发学院软件开发Java 简单封装MIDP RMS操作 阅读

简单封装MIDP RMS操作

 2007-12-23 12:37:35 来源:WEB开发网   
核心提示:许多MIDP应用程序需要简单的存储配置信息,对此,简单封装MIDP RMS操作,简单封装一个RMSHandler,实现一条记录的读写:package com.crackj2ee.midp.rms;import java.io.*;import javax.microedition.rms.*;public final

许多MIDP应用程序需要简单的存储配置信息,对此,简单封装一个RMSHandler,实现一条记录的读写:

package com.crackj2ee.midp.rms;
import java.io.*;
import javax.microedition.rms.*;
public final class RMSHandler {
   PRivate static final String RECORD_STORE_NAME = "RmsDaTa";
   public static boolean loadUniqueRecord(Persistentable p) {
     byte[] data = loadUniqueRecord();
     if(data==null)
       return false;
     DataInputStream input = null;
     try {
       input = new DataInputStream(new ByteArrayInputStream(data));
       p.load(input);
       return true;
     }
     catch(IOException ioe) {
       return false;
     }
     finally {
       try {
         input.close();
       }
       catch (Exception e) {}
     }
   }
   public static boolean saveUniqueRecord(Persistentable p) {
     DataOutputStream output = null;
     try {
       ByteArrayOutputStream bytes = new ByteArrayOutputStream();
       output = new DataOutputStream(bytes);
       p.save(output);
       return saveUniqueRecord(bytes.toByteArray());
     }
     catch(IOException ioe) {
       return false;
     }
     finally {
       if(output!=null)
         try {
           output.close();
         }
         catch (Exception e) {}
     }
   }
   private static byte[] loadUniqueRecord() {
     RecordStore rs = null;
     RecordEnumeration re = null;
     try {
       rs = RecordStore.openRecordStore(RECORD_STORE_NAME, true);
       if(rs.getNumRecords()==0)
         return null;
       re = rs.enumerateRecords(null, null, false);
       if(re.hasNextElement())
         return re.nextRecord();
       return null;
     }
     catch(RecordStoreException rse) {
       return null;
     }
     finally {
       if(re!=null) {
         re.destroy();
       }
       if(rs!=null) {
         try {
           rs.closeRecordStore();
         }
         catch (Exception e) {}
       }
     }
   }
   private static boolean saveUniqueRecord(byte[] data) {
     RecordStore rs = null;
     RecordEnumeration re = null;
     try {
       rs = RecordStore.openRecordStore(RECORD_STORE_NAME, true);
       re = rs.enumerateRecords(null, null, false);
       if(re.hasNextElement()) {
         rs.setRecord(re.nextRecordId(), data, 0, data.length);
       }
       else {
         rs.addRecord(data, 0, data.length);
       }
       return true;
     }
     catch(RecordStoreException rse) {
       return false;
     }
     finally {
       if(re!=null) {
         re.destroy();
       }
       if(rs!=null) {
         try {
           rs.closeRecordStore();
         }
         catch (Exception e) {}
       }
     }
   }
}


需要持久化的类实现一个Persistentable接口:

package com.crackj2ee.midp.rms;
import java.io.*;
public interface Persistentable {
   void save(DataOutputStream output) throws IOException;
   void load(DataInputStream input) throws IOException;
}

读写数据时,按照顺序依次读写即可,例如:

class MyForm extends Form implements Persistentable {

   private int score;
   private String username;


  public MyForm() {
     super("Test");
     load(this);
   }



   public void save(DataOutputStream output) throws IOException {
     output.writeInt(score);
     output.writeUTF8(username);
   }
   public void load(DataInputStream input) throws IOException {
    score = input.readInt();
    username = input.readUTF8();
   }

}

(出处:http://www.cncms.com)


Tags:简单 封装 MIDP

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接