package midp.demo; import javax.microedition.rms.*; import javax.microedition.lcdui.*; import javax.microedition.midlet.*; /************************************************************************* Demonstrates how to use record stores. This example midlet demonstrates how to create a record store and add and delete records. It demonstrates how to filter the records using the RecordFilter interface. The output is sent to the standard output rather than to the canvas on the phone. File: RecordStoreDemo.java Created by: Metrowerks COPYRIGHT All rights reserved Sony Ericsson Mobile Communications AB 2003. The software is the copyrighted work of Sony Ericsson Mobile Communications AB. The use of the software is subject to the terms of the end-user license agreement which accompanies or is included with the software. The software is provided "as is" and Sony Ericsson specifically disclaim any warranty or condition whatsoever regarding merchantability or fitness for a specific purpose, title or non-infringement. No warranty of any kind is made in relation to the condition, suitability, availability, accuracy, reliability, merchantability and/or non-infringement of the software provided herein. *************************************************************************/ public class RecordStoreDemo extends MIDlet { /** * Name of the record store to create */ static final String RECORD_STORE_NAME = "constants"; /** * Signals the MIDlet to start providing service and enter the * Active state. */ protected void startApp() throws MIDletStateChangeException { runExample(); } /** * Signals the MIDlet to stop providing service and enter the * Paused state. In the Paused state the MIDlet must stop * providing service, and might release all resources and become * quiescent */ protected void pauseApp() { } /** * Signals the MIDlet to terminate and enter the Destroyed state * Midlets should perform any operations required before being * terminated, such as releasing resources or saving preferences * or state. */ protected void destroyApp(boolean unconditional) { } public void runExample() { RecordStore recordStore = null; int recordID = 0; /* * Create (or open) the test recordStore */ try { System.out.println("creating data base"); recordStore = RecordStore.openRecordStore(RECORD_STORE_NAME, true); } catch (RecordStoreException rse) { System.err.println(rse.toString()); } /* * Insert 10 new integer records * Allocate a buffer of 64 bytes to store data in */ byte[] dataBuf = new byte[64]; for(int i = 0; i < 10; ++i) { System.out.println( "writing I " + String.valueOf( i )); /* * Set the first element of the record to 'I' to * identify this record as an integer. */ dataBuf[0] = (byte)'I'; /* * Convert the integer into an array of bytes */ asBytes(i, dataBuf, 1); /* * Insert the integer into the database. */ try { /* Add a record with the data contained in dataBuf, * an offset of 0, and a length of dataBuf.length */ recordID = recordStore.addRecord(dataBuf, 0, dataBuf.length); } catch (RecordStoreException rse) { System.err.println(rse.toString()); try { recordStore.deleteRecord(recordID); } catch(Exception ex) { System.err.println(ex.toString()); } } } /* * Insert some string records */ recordID = insertString("foo", recordStore); recordID = insertString("bar", recordStore); /* * delete one of the string records */ try { System.out.println("deleting ID #" + String.valueOf(recordID)); recordStore.deleteRecord(recordID); } catch (RecordStoreException rse) { System.err.println(rse.toString()); } recordID = insertString("baz", recordStore); /* * Build an enumeration to index through all records. Notice * the hole where the one string record was deleted. */ RecordEnumeration re = null; try { re = recordStore.enumerateRecords((RecordFilter)null, (RecordComparator)null, false); } catch (RecordStoreNotOpenException rsnoe) { System.err.println(rsnoe.toString()); } while(re.hasNextElement()) { try { System.out.println("Next Record ID = " + re.nextRecordId()); } catch(InvalidRecordIDException iride) { System.err.println(iride.toString()); } } /* * Clean up the RecordEnumeration */ re.destroy(); /* * Build a new RecordEnumeration with a filter to include only * integer records. Notice how it will only report the integer * records. */ IntegerFilter iFilt = new IntegerFilter(); try { re = recordStore.enumerateRecords((RecordFilter)iFilt, null, false); } catch (RecordStoreNotOpenException rsnoe) { System.err.println(rsnoe.toString()); } /* * And walk through it backwards for kicks... */ while(re.hasPreviousElement()) { try { System.out.println("Previous Record ID = " + re.previousRecordId()); } catch(InvalidRecordIDException iride) { System.err.println(iride.toString()); } } /* * Clean up the RecordEnumeration */ re.destroy(); try { recordStore.closeRecordStore(); //recordStore.deleteRecordStore(RECORD_STORE_NAME); } catch (RecordStoreException rse) { System.err.println(rse.toString()); } } /** * A helper function which inserts a record containing a String * into the given recordStore. * @param str String to insert * @param rStore which RecordStore to insert into * @return record id */ private int insertString(String str, RecordStore rStore){ byte[] buff = new byte[64]; int rID = 0; /* * Set the first element of the record to 'S' to identify * this record as a String. */ buff[0] = (byte)'S'; int length = asBytes(str, buff, 1); /* * Insert the string into the database. */ try { System.out.println( "writing S " + str); rID = rStore.addRecord(buff, 0, buff.length); } catch (RecordStoreException rse) { System.err.println(rse.toString()); try { rStore.deleteRecord(rID); } catch(Exception ex) { System.err.println(ex.toString()); } } return rID; } /** * A convenience method for converting an integer into * a byte array. * * @param i the integer to turn into a byte array. * @param data the array to put it into * @param offset the offset into the data array * @return The number of bytes written to the array. */ static int asBytes(int i, byte[] data, int offset) { data[offset++] = (byte)((i >> 24) & 0xff); data[offset++] = (byte)((i >> 16) & 0xff); data[offset++] = (byte)((i >> 8) & 0xff); data[offset] = (byte)(i & 0xff); return 4; } /** * A convenience method for converting a String into * a byte array. * * @param s the String to turn into a byte array. * @param data the array to put it into * @param offset the offset into the data array * @return The number of bytes written to the array. */ static int asBytes(String s, byte[] data, int offset) { byte[] temp = s.getBytes(); for (int i = 0; i < s.length(); i++) { data[offset++] = temp[i]; } return s.length(); } } /** * IntegerFilter class */ class IntegerFilter implements RecordFilter{ /** * Used toward the end of this example, when creating a * RecordEnumerator to only index through the integer records. */ public boolean matches(byte[] candidate) throws IllegalArgumentException { /* * Only show integers */ return candidate[0] == 'I'; } }