📄 exrms.java
字号:
package exframework;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.*;
import javax.microedition.media.Control;
/**
* <p>Title: ExFramework</p>
*
* <p>Description: lizhenpeng</p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: LP&P</p>
*
* @author lipeng
* @version 1.0
*/
public class ExRMS
implements AllAction,RecordFilter,RecordListener
{
private RecordStore recordStore;
public boolean open()
{
boolean result=true;
try
{
recordStore = RecordStore.openRecordStore("test",true,RecordStore.AUTHMODE_PRIVATE,true);
}
catch(RecordStoreNotFoundException e)
{
result=false;
}
catch(RecordStoreFullException e)
{
result=false;
}
catch(RecordStoreException e)
{
result=false;
}
catch(IllegalArgumentException e)
{
result=false;
}
return result;
}
public boolean matches(byte[] candidate)
{
//if(readRecord(candidate)>30)
{
// return true;
}
return false;
}
public void recordAdded(RecordStore recordStore,int recordId)
{
}
public void recordChanged(RecordStore recordStore,int recordId)
{
}
public void recordDeleted(RecordStore recordStore,int recordId)
{
}
public boolean close()
{
boolean result = true;
try
{
recordStore.closeRecordStore();
RecordStore.deleteRecordStore("test");
}
catch(RecordStoreNotOpenException e)
{
result = false;
}
catch(RecordStoreException e)
{
result = false;
}
return result;
}
public ExRMS()
{
}
int readRecord(byte[] data,Test test)
{
int coord = -1;
ByteArrayInputStream bais= new ByteArrayInputStream(data);
DataInputStream inputStream=new DataInputStream(bais);
try
{
test.x = inputStream.readInt();
test.y = inputStream.readInt();
inputStream.close();
bais.close();
}
catch(IOException e)
{
}
return coord;
}
int readRecord(byte[] data)
{
int coord = -1;
ByteArrayInputStream bais= new ByteArrayInputStream(data);
DataInputStream inputStream=new DataInputStream(bais);
try
{
coord = inputStream.readInt();
inputStream.close();
bais.close();
}
catch(IOException e)
{
}
return coord;
}
void addRecord(Test test)
{
ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream outputStream=new DataOutputStream(baos);
try
{
test.writeData(outputStream);
//outputStream.wr
}
catch(IOException ioe)
{
System.out.println(ioe);
ioe.printStackTrace();
}
byte[] b=baos.toByteArray();
try
{
recordStore.addRecord(b,0,b.length);
}
catch(RecordStoreException rse)
{
System.out.println(rse);
rse.printStackTrace();
}
try
{
outputStream.close();
baos.close();
}
catch(IOException ios)
{
System.out.println(ios);
ios.printStackTrace();
}
}
void addRecord(int data)
{
ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream outputStream=new DataOutputStream(baos);
try
{
outputStream.writeInt(data);
}
catch(IOException ioe)
{
System.out.println(ioe);
ioe.printStackTrace();
}
byte[] b=baos.toByteArray();
try
{
recordStore.addRecord(b,0,b.length);
}
catch(RecordStoreException rse)
{
System.out.println(rse);
rse.printStackTrace();
}
try
{
outputStream.close();
baos.close();
}
catch(IOException ios)
{
System.out.println(ios);
ios.printStackTrace();
}
}
public void allAction(MainForm form)
{
try
{
open();
//form.addline("RecordStore open");
//form.addline("RecordStore name "+recordStore.getName());
addRecord(9);
form.addline("record 1 is "+readRecord(recordStore.getRecord(1)));
byte[] data = recordStore.getRecord(1);
data[3] = 10;
data[2] = 2;
recordStore.setRecord(1,data,0,data.length);
form.addline("after modify:record 1 is "+readRecord(recordStore.getRecord(1)));
close();
/* recordStore.addRecordListener(new RecordListenerDemo());
addRecord(9);
byte[] data = recordStore.getRecord(1);
data[3] = 0x0a;
recordStore.setRecord(1,data,0,data.length);
recordStore.deleteRecord(1);*/
/*
this.addRecord(7);
this.addRecord(5);
this.addRecord(6);
this.addRecord(8);
this.addRecord(4);
this.addRecord(1);
this.addRecord(2);
this.addRecord(3);
RecordEnumeration iter=recordStore.enumerateRecords(null,null,true);
while(iter.hasNextElement())
{
form.append(readRecord(iter.nextRecord())+",");
}
form.addline("");
iter = recordStore.enumerateRecords(null,new SortAscend(),true);
form.append("ascend sort:");
while(iter.hasNextElement())
{
form.append(readRecord(iter.nextRecord())+",");
}
form.addline("");
iter = recordStore.enumerateRecords(null,new SortDescend(),true);
form.append("descend sort:");
while(iter.hasNextElement())
{
form.append(readRecord(iter.nextRecord())+",");
}
form.addline("");
form.addline("next recId" + recordStore.getNextRecordID());
form.addline("number of record " + recordStore.getNumRecords());
byte [] data=recordStore.getRecord(1);
form.addline("read record " + readRecord(data));
recordStore.getRecordSize(1);
recordStore.getSize();
recordStore.getSizeAvailable();
recordStore.getVersion();
recordStore.setRecord(1,data,0,data.length);
data[3] = 4;
form.addline("read record after change" + readRecord(data));
*/
}
catch(Exception e)
{
}
close();
try
{
Test[] test = new Test[10];
ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(baos);
form.addline("存入的数据");
for(int i = 0;i<test.length;i++)
{
test[i] = new Test();
test[i].x = i;
test[i].y = test.length-i;
form.append("("+test[i].x+","+test[i].y+")");
test[i].writeData(dos);
}
form.addline("");
LPRecordStore rs = new LPRecordStore("lipeng");
long beginTime = System.currentTimeMillis();
rs.setRecord(baos.toByteArray());
long endTime = System.currentTimeMillis();
System.out.println(endTime - beginTime);
rs.close();
rs = new LPRecordStore("lipeng");
byte[] data = rs.getRecord();
ByteArrayInputStream bais=new ByteArrayInputStream(data);
DataInputStream dis=new DataInputStream(bais);
Test []test2 = new Test[10];
form.addline("读取的数据");
for(int i = 0;i<test2.length;i++)
{
test2[i] = new Test();
test2[i].readData(dis);
form.append("("+test2[i].x+","+test2[i].y+")");
}
form.addline("");
rs.close();
}
catch(Exception e)
{
}
/*对比两种存贮方式的速度和占据的体积
try
{
long beginTime;
long endTime;
open();
Test[] test=new Test[10];
for(int i=0;i<test.length;i++)
{
test[i]=new Test();
test[i].x=i;
test[i].y=test.length-i;
}
beginTime=System.currentTimeMillis();
for(int i=0;i<test.length;i++)
{
addRecord(test[i]);
}
endTime=System.currentTimeMillis();
System.out.println("1:"+(endTime-beginTime));
LPRecordStore rs = new LPRecordStore("lipeng");
beginTime=System.currentTimeMillis();
ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(baos);
for(int i=0;i<test.length;i++)
{
test[i].writeData(dos);
}
rs.setRecord(baos.toByteArray());
endTime=System.currentTimeMillis();
System.out.println("2:"+(endTime-beginTime));
beginTime=System.currentTimeMillis();
for(int i=0;i<test.length;i++)
{
this.readRecord(recordStore.getRecord(i+1),test[i]);
}
endTime=System.currentTimeMillis();
System.out.println("3:"+(endTime-beginTime));
beginTime=System.currentTimeMillis();
byte[] data=rs.getRecord();
ByteArrayInputStream bais=new ByteArrayInputStream(data);
DataInputStream dis=new DataInputStream(bais);
for(int i=0;i<test.length;i++)
{
test[i].readData(dis);
}
endTime=System.currentTimeMillis();
System.out.println("4:"+(endTime-beginTime));
System.out.println("System:"+recordStore.getSize());
System.out.println("Game:"+rs.rs.getSize());
close();
rs.close();
rs.deleteRecord();
}
catch(Exception e)
{
System.out.print(e.toString());
System.out.println(e);
}*/
//this.recordStore.ad
}
}
class SortAscend implements RecordComparator
{
public int compare(byte[]rec1,byte[]rec2)
{
int num1 = readRecord(rec1);
int num2 = readRecord(rec2);
if(num1>num2)
{
return this.FOLLOWS;
}
else if(num1<num2)
{
return this.PRECEDES;
}
else
{
return this.EQUIVALENT;
}
}
int readRecord(byte[] data)
{
int coord=-1;
ByteArrayInputStream bais=new ByteArrayInputStream(data);
DataInputStream inputStream=new DataInputStream(bais);
try
{
coord=inputStream.readInt();
inputStream.close();
bais.close();
}
catch(IOException e)
{
}
return coord;
}
}
class SortDescend
implements RecordComparator
{
public int compare(byte[] rec1,byte[] rec2)
{
int num1=readRecord(rec1);
int num2=readRecord(rec2);
if(num1>num2)
{
return this.PRECEDES;
}
else if(num1<num2)
{
return this.FOLLOWS;
}
else
{
return this.EQUIVALENT;
}
}
int readRecord(byte[] data)
{
int coord=-1;
ByteArrayInputStream bais=new ByteArrayInputStream(data);
DataInputStream inputStream=new DataInputStream(bais);
try
{
coord=inputStream.readInt();
inputStream.close();
bais.close();
}
catch(IOException e)
{
}
return coord;
}
}
class MoreFilter implements RecordFilter
{
private int criterion;
public MoreFilter(int num)
{
criterion = num;
}
public boolean matches(byte[] candidate)
{
if(readRecord(candidate)>criterion)
{
return true;
}
else
{
return false;
}
}
int readRecord(byte[] data)
{
int coord=-1;
ByteArrayInputStream bais=new ByteArrayInputStream(data);
DataInputStream inputStream=new DataInputStream(bais);
try
{
coord=inputStream.readInt();
inputStream.close();
bais.close();
}
catch(IOException e)
{
}
return coord;
}
}
class RecordListenerDemo
implements RecordListener
{
public void recordAdded(RecordStore recordStore,int recordId)
{
try
{
System.out.print("Added Id is: "+recordId);
System.out.println(" Data is: "+readRecord(recordStore.getRecord(recordId)));
}
catch(Exception e)
{
}
}
public void recordChanged(RecordStore recordStore,int recordId)
{
try
{
System.out.print("Changed Id is: "+recordId);
System.out.println(" Data is: "+readRecord(recordStore.getRecord(recordId)));
}
catch(Exception e)
{
}
}
public void recordDeleted(RecordStore recordStore,int recordId)
{
System.out.println("delete Id is: "+recordId);
}
int readRecord(byte[] data)
{
int coord=-1;
ByteArrayInputStream bais=new ByteArrayInputStream(data);
DataInputStream inputStream=new DataInputStream(bais);
try
{
coord=inputStream.readInt();
inputStream.close();
bais.close();
}
catch(IOException e)
{
}
return coord;
}
}
class Test
{
public int x;
public int y;
public void writeData(DataOutputStream dos) throws IOException
{
dos.writeInt(x);
dos.writeInt(y);
}
public void readData(DataInputStream dis) throws IOException
{
x = dis.readInt();
y = dis.readInt();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -