Weird RecordStore behavior when RecordStoreFullException occurs

Posted by Michael P on Stack Overflow See other posts from Stack Overflow or by Michael P
Published on 2010-03-17T00:04:01Z Indexed on 2010/03/17 0:11 UTC
Read the original article Hit count: 671

Hello everyone!

I'm developing a small J2ME application that displays bus stops timetables - they are stored as records in MIDP RecordStores.

Sometimes records can not fit a single RecordStore, especially on record update - using setRecord method - a RecordStoreFullException occurs. I catch the exception, and try to write the record to a new RecordStore along with deleting the previous one in the old RecordStore. Everything works fine except of deleting record from RecordStore where the RecordStoreFullException occurs. If I make an attempt to delete record that could not be updated, another Exception of type InvalidRecordIDException is thrown. This is weird and undocumented in MIDP javadoc. I have tested it on Sun WTK 2.5.2, MicroEdition SDK 3.0 and Nokia Series 40 SDK. Furthermore I created a code that reproduces this strange behaviour:


RecordStore rms = null;
        int id = 0;
        try {
            rms  = RecordStore.openRecordStore("Test", true);
            byte[] raw = new byte[192*10024]; //Big enough to cause RecordStoreFullException
            id = rms.addRecord(raw, 0, 160);
            rms.setRecord(id, raw, 0, raw.length);
        } catch (Exception e) {
            try {
                int count = rms.getNumRecords();
                RecordEnumeration en = rms.enumerateRecords(null, null, true);
                count = en.numRecords();
                while(en.hasNextElement()){
                    System.out.println("NextID: "+en.nextRecordId());
                }
                rms.deleteRecord(id); //this won't work!
                rms.setRecord(id, new byte[5], 0, 5); //this won't work too!
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

I added extra enumeration code to produce other weird behavior - when RecordStoreFullException occurs, count variable will be set to 1 (if RMS was empty) by both methods - getNumRecords and numRecords. System.out.println will produce NextID: 0! It is not acceptable because record ID can not be 0! Could someone explain this strange behavior?

Sorry for my bad English.

© Stack Overflow or respective owner

Related posts about java

Related posts about j2me