Thursday, June 9, 2011

RMS Management


import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotOpenException;


public class RMSStore {

    private RecordStore rs = null;

    public void openRecordStore(String str) {
        try {
            if (rs == null) {
                rs = RecordStore.openRecordStore(str, true);
                //Modified by Mihir
                //rs = RecordStore.openRecordStore(str, true,RecordStore.AUTHMODE_ANY,true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void closeRecordStore() {
        try {
            if (rs != null) {
                rs.closeRecordStore();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void deleteRecordStore(String storenName) {
        try {
            RecordStore.deleteRecordStore(storenName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String[] listAllRecordStore() {
        return RecordStore.listRecordStores();
    }

    public int writeRecord(String str) {
        int id = 0;
        try {
            id = rs.addRecord(str.getBytes(), 0, str.getBytes().length);
        } catch (RecordStoreFullException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return id;
    }

    public int writeByteRecord(byte[] data) {
        int id = -1;

        try {
            id = rs.addRecord(data, 0, data.length);
        } catch (RecordStoreFullException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return id;
    }

    public int getRecordCount() {
        try {
            return rs.getNumRecords();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public byte[] getRecordDataFromId(int id) {
        byte[] data = null;
        try {
            data = rs.getRecord(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

    public String getRecordFromId(int id) {
        return new String(getRecordDataFromId(id));
    }

    public byte[] getRecordByteFromId(int id) {
        return getRecordDataFromId(id);
    }

    public void deleteRecord(int id) {
        try {
            rs.deleteRecord(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean checkRecordExists(String compare) {
        for (int i = 0; i < getRecordCount(); i++) {
            if (compare.equals(getRecordFromId(i + 1))) {
                return true;
            }
        }
        return false;
    }

    public int getMaxRMSSize() {
        int size = 0;
        try {
            size = rs.getSizeAvailable() + rs.getSize();
        } catch (RecordStoreNotOpenException e) {
            e.printStackTrace();
        }

        return size;
    }

    public void setRecordById(String str, int id) {
        try {
            rs.setRecord(id, str.getBytes(), 0, str.getBytes().length);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int getNextRecordId() {
        int id = 0;
        try {
            id = rs.getNextRecordID();
        } catch (RecordStoreException re) {
            re.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return id;
    }

    public RecordEnumeration getRecordEnumData() {
        try {
            return rs.enumerateRecords(null, null, false);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public String[] getRecordData() {

        String[] str = null;
        int counter = 0;
        try {
            RecordEnumeration enumeration = rs.enumerateRecords(null, null, false);
            str = new String[rs.getNumRecords()];

            while (enumeration.hasNextElement()) {
                try {
                    str[counter] = (new String(enumeration.nextRecord()));
                    counter++;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

}

No comments:

Post a Comment