views:

82

answers:

1

Hi,

I was wondering if it is possible in j2me to have 2 or more recordStores open at the same time. I basically want to be able to add/remove records from 2 different recordStores in the same execution of the code. Is this possible?

If so, how would you do it? At the top of the class, you do something like 'private RecordStore rs;' would you need to have two instances of this to make it work or could you do it with the one declaration?

Thanks in advance.

+2  A: 

From the RecordStore javadoc: "MIDlets within a MIDlet suite are allowed to create multiple record stores, as long as they are each given different names. When a MIDlet suite is removed from a platform all the record stores associated with its MIDlets will also be removed. MIDlets within a MIDlet suite can access each other's record stores directly."

So you can operate multiple record Stores within a MIDlet.

Here is an example I use sometimes with my students (WARNING: there is no UI in this MIDlet: for demonstration purposes only). You can see that using one or two variables does not matter.

package test;

import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import javax.microedition.midlet.*; import javax.microedition.rms.RecordEnumeration; import javax.microedition.rms.RecordStore; import javax.microedition.rms.RecordStoreException;

public class ExampleTwoRS extends MIDlet {

private final static String RS_BOYS_NAME = "boys";
private final static String RS_GIRLS_NAME = "girls";

public ExampleTwoRS() {
    Person[] people = new Person[4];
    people[0] = new Person("Angelina", false);
    people[1] = new Person("Brad", true);
    people[2] = new Person("Mirka", false);
    people[3] = new Person("Roger", true);

    try {
        initData(people);
        readData(RS_BOYS_NAME);
        readData(RS_GIRLS_NAME);
    } catch (RecordStoreException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

public void startApp() {
}

private void initData(Person[] people) throws RecordStoreException, IOException {
    RecordStore rsBoys = null;
    RecordStore rsGirls = null;

    try {
        rsBoys = RecordStore.openRecordStore(RS_BOYS_NAME, true);
        rsGirls = RecordStore.openRecordStore(RS_GIRLS_NAME, true);

        for (int i = 0; i < people.length; i++) {
            byte[] data = people[i].toByteArray();
            if (people[i].isMale()) {
                rsBoys.addRecord(data, 0, data.length);
            } else {
                rsGirls.addRecord(data, 0, data.length);
            }
        }
    } finally {
        rsBoys.closeRecordStore();
        rsGirls.closeRecordStore();
    }
}

private void readData(String rsName) throws RecordStoreException, IOException {
    RecordStore rs = null;

    try {
        rs = RecordStore.openRecordStore(rsName, true);

        int i = 0;
        RecordEnumeration re = rs.enumerateRecords(null, null, true);
        Person[] people = new Person[re.numRecords()];
        while (re.hasNextElement()) {
            people[i] = new Person();
            people[i].fromByteArray(re.nextRecord());
            System.out.println(rsName + ": " + people[i].toString());
            i++;
        }
    } finally {
        rs.closeRecordStore();
    }
}

private void initNumbers() {
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

}

class Person { private String name; boolean male;

public Person() {
}

public Person(String name, boolean male) {
    this.name = name;
    this.male = male;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public boolean isMale() {
    return male;
}

public void setMale(boolean male) {
    this.male = male;
}

public void fromDataStream(DataInputStream dis) throws IOException {
    this.name = dis.readUTF();
    this.male = dis.readBoolean();
}

public void toDataStream(DataOutputStream dos) throws IOException {
    dos.writeUTF(getName());
    dos.writeBoolean(isMale());
}

public void fromByteArray(byte[] data) throws IOException {
    ByteArrayInputStream bin = new ByteArrayInputStream(data);
    DataInputStream din = new DataInputStream(bin);

    fromDataStream(din);
    din.close();
}

public byte[] toByteArray() throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    DataOutputStream dout = new DataOutputStream(bout);

    toDataStream(dout);
    dout.close();

    return bout.toByteArray();
}

public String toString() {
    return name + (male ? " (b)" : " (g)");
}

}

Jala
I would add that, while the spec certainly allows for a single MIDlet to use multiple RecordStores, it doesn't madate that they can all be open at the same time. That is a fragmentation point open to interpretation and would need to be tested on the actual target handset.
QuickRecipesOnSymbianOS
+1 Good point to stand out, thank you.
Jala