ExRMS.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:13k
源码类别:

J2ME

开发平台:

Java

  1. package exframework;
  2. import javax.microedition.lcdui.*;
  3. import javax.microedition.rms.*;
  4. import java.io.*;
  5. import javax.microedition.media.Control;
  6. /**
  7.  * <p>Title: ExFramework</p>
  8.  *
  9.  * <p>Description: lizhenpeng</p>
  10.  *
  11.  * <p>Copyright: Copyright (c) 2005</p>
  12.  *
  13.  * <p>Company: LP&P</p>
  14.  *
  15.  * @author lipeng
  16.  * @version 1.0
  17.  */
  18. public class ExRMS
  19.   implements AllAction,RecordFilter,RecordListener
  20. {
  21.   private RecordStore recordStore;
  22.   public boolean open()
  23.   {
  24.     boolean result=true;
  25.     try
  26.     {
  27.       recordStore = RecordStore.openRecordStore("test",true,RecordStore.AUTHMODE_PRIVATE,true);
  28.     }
  29.     catch(RecordStoreNotFoundException e)
  30.     {
  31.       result=false;
  32.     }
  33.     catch(RecordStoreFullException e)
  34.     {
  35.       result=false;
  36.     }
  37.     catch(RecordStoreException e)
  38.     {
  39.       result=false;
  40.     }
  41.     catch(IllegalArgumentException e)
  42.     {
  43.       result=false;
  44.     }
  45.     return result;
  46.   }
  47.   public boolean matches(byte[] candidate)
  48.   {
  49.     //if(readRecord(candidate)>30)
  50.     {
  51.      // return true;
  52.     }
  53.     return false;
  54.   }
  55.   public void recordAdded(RecordStore recordStore,int recordId)
  56.   {
  57.   }
  58.   public void recordChanged(RecordStore recordStore,int recordId)
  59.   {
  60.   }
  61.   public void recordDeleted(RecordStore recordStore,int recordId)
  62.   {
  63.   }
  64.   public boolean close()
  65.   {
  66.     boolean result = true;
  67.     try
  68.     {
  69.       recordStore.closeRecordStore();
  70.       RecordStore.deleteRecordStore("test");
  71.     }
  72.     catch(RecordStoreNotOpenException e)
  73.     {
  74.       result = false;
  75.     }
  76.     catch(RecordStoreException e)
  77.     {
  78.       result = false;
  79.     }
  80.     return result;
  81.   }
  82.   public ExRMS()
  83.   {
  84.   }
  85.   int readRecord(byte[] data,Test test)
  86.   {
  87.     int coord = -1;
  88.     ByteArrayInputStream bais= new ByteArrayInputStream(data);
  89.     DataInputStream inputStream=new DataInputStream(bais);
  90.     try
  91.     {
  92.       test.x = inputStream.readInt();
  93.       test.y = inputStream.readInt();
  94.       inputStream.close();
  95.       bais.close();
  96.     }
  97.     catch(IOException e)
  98.     {
  99.     }
  100.     return coord;
  101.   }
  102.   int readRecord(byte[] data)
  103. {
  104.   int coord = -1;
  105.   ByteArrayInputStream bais= new ByteArrayInputStream(data);
  106.   DataInputStream inputStream=new DataInputStream(bais);
  107.   try
  108.   {
  109.     coord = inputStream.readInt();
  110.     inputStream.close();
  111.     bais.close();
  112.   }
  113.   catch(IOException e)
  114.   {
  115.   }
  116.   return coord;
  117. }
  118.   void addRecord(Test test)
  119.   {
  120.     ByteArrayOutputStream baos=new ByteArrayOutputStream();
  121.     DataOutputStream outputStream=new DataOutputStream(baos);
  122.     try
  123.     {
  124.       test.writeData(outputStream);
  125.       //outputStream.wr
  126.     }
  127.     catch(IOException ioe)
  128.     {
  129.       System.out.println(ioe);
  130.       ioe.printStackTrace();
  131.     }
  132.     byte[] b=baos.toByteArray();
  133.     try
  134.     {
  135.       recordStore.addRecord(b,0,b.length);
  136.     }
  137.     catch(RecordStoreException rse)
  138.     {
  139.       System.out.println(rse);
  140.       rse.printStackTrace();
  141.     }
  142.     try
  143.     {
  144.       outputStream.close();
  145.       baos.close();
  146.     }
  147.     catch(IOException ios)
  148.     {
  149.       System.out.println(ios);
  150.       ios.printStackTrace();
  151.     }
  152.   }
  153.   void addRecord(int data)
  154.   {
  155.     ByteArrayOutputStream baos=new ByteArrayOutputStream();
  156.     DataOutputStream outputStream=new DataOutputStream(baos);
  157.     try
  158.     {
  159.       outputStream.writeInt(data);
  160.     }
  161.     catch(IOException ioe)
  162.     {
  163.       System.out.println(ioe);
  164.       ioe.printStackTrace();
  165.     }
  166.     byte[] b=baos.toByteArray();
  167.     try
  168.     {
  169.       recordStore.addRecord(b,0,b.length);
  170.     }
  171.     catch(RecordStoreException rse)
  172.     {
  173.       System.out.println(rse);
  174.       rse.printStackTrace();
  175.     }
  176.     try
  177.     {
  178.       outputStream.close();
  179.       baos.close();
  180.     }
  181.     catch(IOException ios)
  182.     {
  183.       System.out.println(ios);
  184.       ios.printStackTrace();
  185.     }
  186.   }
  187.   public void allAction(MainForm form)
  188.   {
  189.     try
  190.     {
  191.       open();
  192.      //form.addline("RecordStore open");
  193.       //form.addline("RecordStore name "+recordStore.getName());
  194.       addRecord(9);
  195.       form.addline("record 1 is "+readRecord(recordStore.getRecord(1)));
  196.       byte[] data = recordStore.getRecord(1);
  197.       data[3] = 10;
  198.       data[2] = 2;
  199.       recordStore.setRecord(1,data,0,data.length);
  200.       form.addline("after modify:record 1 is "+readRecord(recordStore.getRecord(1)));
  201.       close();
  202.   /*    recordStore.addRecordListener(new RecordListenerDemo());
  203.       addRecord(9);
  204.       byte[] data = recordStore.getRecord(1);
  205.       data[3] = 0x0a;
  206.       recordStore.setRecord(1,data,0,data.length);
  207.       recordStore.deleteRecord(1);*/
  208. /*
  209.       this.addRecord(7);
  210.       this.addRecord(5);
  211.       this.addRecord(6);
  212.       this.addRecord(8);
  213.       this.addRecord(4);
  214.       this.addRecord(1);
  215.       this.addRecord(2);
  216.       this.addRecord(3);
  217.       RecordEnumeration iter=recordStore.enumerateRecords(null,null,true);
  218.       while(iter.hasNextElement())
  219.       {
  220.         form.append(readRecord(iter.nextRecord())+",");
  221.       }
  222.       form.addline("");
  223.       iter = recordStore.enumerateRecords(null,new SortAscend(),true);
  224.       form.append("ascend sort:");
  225.       while(iter.hasNextElement())
  226.       {
  227.         form.append(readRecord(iter.nextRecord())+",");
  228.       }
  229.       form.addline("");
  230.       iter = recordStore.enumerateRecords(null,new SortDescend(),true);
  231.       form.append("descend sort:");
  232.       while(iter.hasNextElement())
  233.       {
  234.         form.append(readRecord(iter.nextRecord())+",");
  235.       }
  236.       form.addline("");
  237.       form.addline("next recId" + recordStore.getNextRecordID());
  238.       form.addline("number of record " + recordStore.getNumRecords());
  239.       byte []  data=recordStore.getRecord(1);
  240.       form.addline("read record " + readRecord(data));
  241.       recordStore.getRecordSize(1);
  242.       recordStore.getSize();
  243.       recordStore.getSizeAvailable();
  244.       recordStore.getVersion();
  245.       recordStore.setRecord(1,data,0,data.length);
  246.       data[3] = 4;
  247.       form.addline("read record after change" + readRecord(data));
  248. */
  249.     }
  250.     catch(Exception e)
  251.     {
  252.     }
  253.     close();
  254.     try
  255.     {
  256.       Test[] test = new Test[10];
  257.       ByteArrayOutputStream baos=new ByteArrayOutputStream();
  258.       DataOutputStream dos=new DataOutputStream(baos);
  259.       form.addline("存入的数据");
  260.       for(int i = 0;i<test.length;i++)
  261.       {
  262.         test[i] = new Test();
  263.         test[i].x = i;
  264.         test[i].y = test.length-i;
  265.         form.append("("+test[i].x+","+test[i].y+")");
  266.         test[i].writeData(dos);
  267.       }
  268.       form.addline("");
  269.       LPRecordStore rs = new LPRecordStore("lipeng");
  270.       long beginTime = System.currentTimeMillis();
  271.       rs.setRecord(baos.toByteArray());
  272.       long endTime = System.currentTimeMillis();
  273.       System.out.println(endTime - beginTime);
  274.       rs.close();
  275.       rs = new LPRecordStore("lipeng");
  276.       byte[] data = rs.getRecord();
  277.       ByteArrayInputStream bais=new ByteArrayInputStream(data);
  278.       DataInputStream dis=new DataInputStream(bais);
  279.       Test []test2 = new Test[10];
  280.       form.addline("读取的数据");
  281.       for(int i = 0;i<test2.length;i++)
  282.       {
  283.         test2[i] = new Test();
  284.         test2[i].readData(dis);
  285.         form.append("("+test2[i].x+","+test2[i].y+")");
  286.       }
  287.       form.addline("");
  288.       rs.close();
  289.     }
  290.     catch(Exception e)
  291.     {
  292.     }
  293.   /*对比两种存贮方式的速度和占据的体积
  294.     try
  295.     {
  296.       long beginTime;
  297.       long endTime;
  298.       open();
  299.       Test[] test=new Test[10];
  300.       for(int i=0;i<test.length;i++)
  301.       {
  302.         test[i]=new Test();
  303.         test[i].x=i;
  304.         test[i].y=test.length-i;
  305.       }
  306.       beginTime=System.currentTimeMillis();
  307.       for(int i=0;i<test.length;i++)
  308.       {
  309.         addRecord(test[i]);
  310.       }
  311.       endTime=System.currentTimeMillis();
  312.       System.out.println("1:"+(endTime-beginTime));
  313.       LPRecordStore rs = new LPRecordStore("lipeng");
  314.       beginTime=System.currentTimeMillis();
  315.       ByteArrayOutputStream baos=new ByteArrayOutputStream();
  316.       DataOutputStream dos=new DataOutputStream(baos);
  317.       for(int i=0;i<test.length;i++)
  318.       {
  319.         test[i].writeData(dos);
  320.       }
  321.       rs.setRecord(baos.toByteArray());
  322.       endTime=System.currentTimeMillis();
  323.       System.out.println("2:"+(endTime-beginTime));
  324.       beginTime=System.currentTimeMillis();
  325.       for(int i=0;i<test.length;i++)
  326.       {
  327.         this.readRecord(recordStore.getRecord(i+1),test[i]);
  328.       }
  329.       endTime=System.currentTimeMillis();
  330.       System.out.println("3:"+(endTime-beginTime));
  331.       beginTime=System.currentTimeMillis();
  332.       byte[] data=rs.getRecord();
  333.       ByteArrayInputStream bais=new ByteArrayInputStream(data);
  334.       DataInputStream dis=new DataInputStream(bais);
  335.       for(int i=0;i<test.length;i++)
  336.       {
  337.         test[i].readData(dis);
  338.       }
  339.       endTime=System.currentTimeMillis();
  340.       System.out.println("4:"+(endTime-beginTime));
  341.       System.out.println("System:"+recordStore.getSize());
  342.       System.out.println("Game:"+rs.rs.getSize());
  343.       close();
  344.       rs.close();
  345.       rs.deleteRecord();
  346.     }
  347.     catch(Exception e)
  348.     {
  349.       System.out.print(e.toString());
  350.                System.out.println(e);
  351.     }*/
  352.     //this.recordStore.ad
  353.   }
  354. }
  355. class SortAscend implements RecordComparator
  356. {
  357.   public int compare(byte[]rec1,byte[]rec2)
  358.   {
  359.     int num1  = readRecord(rec1);
  360.     int num2 = readRecord(rec2);
  361.     if(num1>num2)
  362.     {
  363.       return this.FOLLOWS;
  364.     }
  365.     else if(num1<num2)
  366.     {
  367.       return this.PRECEDES;
  368.     }
  369.     else
  370.     {
  371.       return this.EQUIVALENT;
  372.     }
  373.   }
  374.   int readRecord(byte[] data)
  375.   {
  376.     int coord=-1;
  377.     ByteArrayInputStream bais=new ByteArrayInputStream(data);
  378.     DataInputStream inputStream=new DataInputStream(bais);
  379.     try
  380.     {
  381.       coord=inputStream.readInt();
  382.       inputStream.close();
  383.       bais.close();
  384.     }
  385.     catch(IOException e)
  386.     {
  387.     }
  388.     return coord;
  389.   }
  390. }
  391. class SortDescend
  392.   implements RecordComparator
  393. {
  394.   public int compare(byte[] rec1,byte[] rec2)
  395.   {
  396.     int num1=readRecord(rec1);
  397.     int num2=readRecord(rec2);
  398.     if(num1>num2)
  399.     {
  400.       return this.PRECEDES;
  401.     }
  402.     else if(num1<num2)
  403.     {
  404.       return this.FOLLOWS;
  405.     }
  406.     else
  407.     {
  408.       return this.EQUIVALENT;
  409.     }
  410.   }
  411.   int readRecord(byte[] data)
  412.   {
  413.     int coord=-1;
  414.     ByteArrayInputStream bais=new ByteArrayInputStream(data);
  415.     DataInputStream inputStream=new DataInputStream(bais);
  416.     try
  417.     {
  418.       coord=inputStream.readInt();
  419.       inputStream.close();
  420.       bais.close();
  421.     }
  422.     catch(IOException e)
  423.     {
  424.     }
  425.     return coord;
  426.   }
  427. }
  428. class MoreFilter implements RecordFilter
  429. {
  430.   private int criterion;
  431.   public MoreFilter(int num)
  432.   {
  433.     criterion = num;
  434.   }
  435.   public boolean matches(byte[] candidate)
  436.   {
  437.     if(readRecord(candidate)>criterion)
  438.     {
  439.       return true;
  440.     }
  441.     else
  442.     {
  443.       return false;
  444.     }
  445.   }
  446.   int readRecord(byte[] data)
  447.   {
  448.     int coord=-1;
  449.     ByteArrayInputStream bais=new ByteArrayInputStream(data);
  450.     DataInputStream inputStream=new DataInputStream(bais);
  451.     try
  452.     {
  453.       coord=inputStream.readInt();
  454.       inputStream.close();
  455.       bais.close();
  456.     }
  457.     catch(IOException e)
  458.     {
  459.     }
  460.     return coord;
  461.   }
  462. }
  463. class RecordListenerDemo
  464.   implements RecordListener
  465. {
  466.   public void recordAdded(RecordStore recordStore,int recordId)
  467.   {
  468.     try
  469.     {
  470.       System.out.print("Added Id is: "+recordId);
  471.       System.out.println(" Data is: "+readRecord(recordStore.getRecord(recordId)));
  472.     }
  473.     catch(Exception e)
  474.     {
  475.     }
  476.   }
  477.   public void recordChanged(RecordStore recordStore,int recordId)
  478.   {
  479.     try
  480.     {
  481.       System.out.print("Changed Id is: "+recordId);
  482.       System.out.println(" Data is: "+readRecord(recordStore.getRecord(recordId)));
  483.     }
  484.     catch(Exception e)
  485.     {
  486.     }
  487.   }
  488.   public void recordDeleted(RecordStore recordStore,int recordId)
  489.   {
  490.       System.out.println("delete Id is: "+recordId);
  491.   }
  492.   int readRecord(byte[] data)
  493.   {
  494.     int coord=-1;
  495.     ByteArrayInputStream bais=new ByteArrayInputStream(data);
  496.     DataInputStream inputStream=new DataInputStream(bais);
  497.     try
  498.     {
  499.       coord=inputStream.readInt();
  500.       inputStream.close();
  501.       bais.close();
  502.     }
  503.     catch(IOException e)
  504.     {
  505.     }
  506.     return coord;
  507.   }
  508. }
  509. class Test
  510. {
  511.   public int x;
  512.   public int y;
  513.   public void writeData(DataOutputStream dos) throws IOException
  514.   {
  515.     dos.writeInt(x);
  516.     dos.writeInt(y);
  517.   }
  518.   public void readData(DataInputStream dis) throws IOException
  519.   {
  520.     x = dis.readInt();
  521.     y = dis.readInt();
  522.   }
  523. }