Question

I have read many questions about Android, J2ME and RecordStore, but I still can't find the answer that could satisfy me.

I need to implement low-level part of my Java app that should work on different platforms, right now this is Android and J2ME, and in future it should work on PC too. I need to store simple data sets, that is almost similar to RecordStore in J2ME:

App should own several record stores with records, each record has:

  • the id (but it should be "my" id, not auto-returned one as it is in RecordStore),
  • the data (just a byte array).

I think I should write an Interface with needed methods, and each platform should have its own implementation of this Interface.

But this task seems to be very common (at least, for Android + J2ME), so, maybe there already is some lightweight implementation? I'm asking just because I don't like to re-invent the wheel.

And maybe some suggestions?

Was it helpful?

Solution

So, I wrote interface that does satisfy my requirements, with two implementations: for Android and J2ME.

Here is how does Interface look:

public interface ISDataStore {


   /**
    * Get number of records in data store.
    */
   public int getNumRecords() throws SDataStoreException;

   /**
    * Get size of one record with specified id in bytes.
    *
    * @param record_id id of the record
    */
   public int getRecordSize(int record_id) throws SDataStoreException;

   /**
    * Get record.
    *
    * @param record_id id of the record to read
    * @param data byte array where to put the data
    * @param offset offset in 'data' array from which should start to copy
    */
   public void getRecord(int record_id, byte[] data, int offset) throws SDataStoreException;

   /**
    * Get record.
    *
    * @param record_id id of the record to read
    */
   public byte[] getRecord(int record_id) throws SDataStoreException;

   /**
    * Resolves is record with specified id exists or not.
    *
    * @param record_id id of the record
    * @return true if record exists, otherwise false
    */
   public boolean isRecordExists(int record_id) throws SDataStoreException;

   /**
    * Put new record or update existing one.
    *
    * @param record_id id of the record
    * @param data byte array of data
    * @param offset offset in the data byte array
    * @param length number of bytes to store
    *
    * @return true if operation was successful, otherwise false
    *
    */
   public boolean setRecord(int record_id, byte[] data, int offset, int length) throws SDataStoreException;

   /**
    * Delete the record.
    *
    * @param record_id id of the record
    *
    * @return true if operation was successful, otherwise false
    */
   public boolean deleteRecord(int record_id) throws SDataStoreException;

   /**
    * Clear all the records.
    */
   public void deleteAll() throws SDataStoreException;

   /**
    * Close the data store.
    */
   public void close() throws SDataStoreException;

}

There is also a factory for data stores:

public interface ISDataStoreFactory {

   /**
    * @param dataStoreId id of the data store
    * @return ISDataStore with given id. Type of this id depends on target platform
    */
   public ISDataStore getDataStore(Object dataStoreId) throws SDataStoreException;

   /**
    * Destroys data store with given id.
    * @param dataStoreId id of the data store. Type of this id depends on target platform
    */
   public void destroyDataStore(Object dataStoreId) throws SDataStoreException;

}

Docs auto-generated by Doxygen can be found here.

Mercurial repository with Interface and all the implementations can be found here.

How do I use it:

As I already said in my question, I have app for Android and app for J2ME, both these apps does the similar thing. (if anyone interested, they does communication via bluetooth with remote embedded device)

Both apps have common low-level part that does the main job.

I have interface IMainApp, something like that:

public interface IMainApp {

   public ISDataStoreFactory getDataStoreFactory();

   /*
    * ... some other methods
    */
}

Both apps (for Android and for J2ME) have its own implementations of this interface, and they pass its reference to the low-level part. When low-level part wants to open some data store, it uses ISDataStoreFactory returned by IMainApp.getDataStoreFactory. It works just like I want it to work.

Hope it is useful for anyone.

OTHER TIPS

Am shou you what i did

create profiles .. that are values i want to store in the database

Sample profile

    public class NewsProfile {

        public String uniqid = "", category = "", title = "" ;

        public NewsProfile(String uniqid) {
            this.uniqid = uniqid;
        }
    }

Create a News store that accept New Profile

public void saveProfile(int id, NewsProfile profile) {
    try {
        if (rs != null) {
            profile.id = id;
            byte[] bytes = toByteArray(profile);
            setRecord(profile.id, bytes);
            System.err.println("Exists = " + profile.catKey + String.valueOf(profile.status));
        }
    } catch (Exception e) {
        System.err.println("ERROR: saveUpdateProfile" + e.getMessage());
    }
}

public NewsProfile getProfileint id) throws RecordStoreException, IOException {
    byte[] bytes = rs.getRecord(id);
    DataInputStream is = new DataInputStream(new ByteArrayInputStream(bytes));
    String uniqid = is.readUTF();
    OptionsProfile profile = new NewsProfile (uniqid);
    profile.id = id;
    profile.catKey = uniqid;
    profile.category = is.readUTF();
    profile.title = is.readUTF();
    return profile;
}

private byte[] toByteArray(NewsProfile profile) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream os = new DataOutputStream(baos);
    os.writeUTF(profile.uniqid);
    os.writeUTF(profile.category);
    os.writeUTF(profile.title);
    return baos.toByteArray();
}

What this means is that anytime i want to save data to a database .... what am saving at any point in time is NewsProfile .... You can not implement for different storage you want .. SQLite , RMS , even Web Service

Thanks :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top