Normal simple storage interface for Android, J2ME and PC

I read a lot of questions about Android, J2ME and RecordStore , but I still can not find an answer that could satisfy me.

I need to implement the low-level part of my Java application, which should work on different platforms, right now it is Android and J2ME, and in the future it should also work on PC. I need to store simple datasets, almost similar to RecordStore in J2ME:

The application must own several record stores with records, each record has:

  • id (but it should be " my ", and not automatically returned, as it is in RecordStore ),
  • data (byte array only).

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

But this task seems very common (at least for Android + J2ME), so maybe there is already a small implementation? I ask only because I do not like to reinvent the wheel.

And maybe some suggestions?

+4
source share
2 answers

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

Here's what the interface looks like:

 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 warehouses:

 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; } 

Documents created with Doxygen can be found here .

A mercury repository with an interface and all implementations can be found here .

How to use it:

As I said in my question, I have an Android app and a J2ME app, both of these apps do a similar thing. (if anyone is interested, they communicate via Bluetooth with a remote built-in device)

Both applications have a common low-level part, which performs the main task.

I have an interface IMainApp , something like this:

 public interface IMainApp { public ISDataStoreFactory getDataStoreFactory(); /* * ... some other methods */ } 

Both applications (for Android and J2ME) have their own implementations of this interface, and they transmit a link to a low-level part. When the low-level part wants to open some data store, it uses the ISDataStoreFactory returned by IMainApp.getDataStoreFactory . It works the way I want it to work.

Hope this is helpful to everyone.

+1
source

That's what i did

create profiles .. which are the values ​​that I want to save in the database

Profile Example

  public class NewsProfile { public String uniqid = "", category = "", title = "" ; public NewsProfile(String uniqid) { this.uniqid = uniqid; } } 

Create a news store hosting a 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(); } 

This means that at any time I want to save data to a database ... what I want to save at any given time is NewsProfile .... You cannot implement for different repositories that you want. SQLite, RMS, even web services

Thanks :)

0
source

Source: https://habr.com/ru/post/1402879/


All Articles