×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    20

    Read back from RMS

    Hi there,

    Graham wrote a tutorial in my last post. I tried it and it works perfectly! Thanks for that Graham.
    But i have a little problem when i want to read it back from the RMS.

    I have a persistent interface:
    Code:
    public interface Persistent {
    
        //Called to serialize an object
        public void serialize(DataOutput out) throws IOException;
        
        //Called to reconstruct a serialized object
        public void reconstruct(byte[] data) throws IOException;
    }
    then i have 2 classes who implements it (below one of them):
    Code:
    public class TypeOnefield implements Persistent {
       public TypeOnefield(String name, String type, String desc, String textfieldname) {
            this.name = name;
            this.description = desc;
            this.textfieldname = textfieldname;
            this.type = type;
        public void serialize(DataOutput out) throws IOException {
            out.writeUTF(getName());
            out.writeUTF(getDescription());
            out.writeUTF(getTextfieldname());
            out.writeUTF(getType());
            out.writeUTF(getValue());
        }
        //recustruct the serialized object from the recordstore to the original object
        public void reconstruct(byte[] data) throws IOException {
            ByteArrayInputStream bin = new ByteArrayInputStream(data);
            DataInputStream din = new DataInputStream(bin);
            this.name = din.readUTF();
            this.description = din.readUTF();
            this.textfieldname = din.readUTF();
            this.type = din.readUTF();
            this.value = din.readUTF();
        }
    }
    It is not the whole code, lot of overhead like setter and getter...

    Then i have a Vector which contains a lot of objects from TypeOneField and TypeTwoField.
    So far so good.

    Now i want to store the Vector in the RMS:
    Code:
    public void writeXML(Vector exercises) throws IOException {
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            DataOutputStream dout = new DataOutputStream(bout);
            try {
                Enumeration en = exercises.elements();
                while (en.hasMoreElements()) {
                    Persistent p = (Persistent) (en.nextElement());
                    p.serialize(dout);
                }
            } catch (IOException e) {
            } finally {
                dout.close();
            }
            byte[] data = bout.toByteArray();  // here's the array to store in RMS
            try {
                if (rsXML.getNumRecords() != 0) {
                    rsXML.setRecord(1, data, 0, data.length);
                } else {
                    rsXML.addRecord(data, 0, data.length);
                }
    
            } catch (RecordStoreException ex) {
                ex.printStackTrace();
            }
        }
    After saving i want to read the stuff back from the RMS to the vector and that's the problem:

    Code:
    Vector exercises;
            try {
                rsXML = RecordStore.openRecordStore(XML_RECORD, false);
                //I have no idea how i can call the "reconstruct"-method in TypeOneField or TypeTwoField, because i don't know which object i get
    Thats the problem, i hope you can understand and help me...i'm so close to solve the storage thing. The tutorial works perfectly with my preferences, ican read and write...but this is another complicated thing for me.

    thanks for your help!

    -matze

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Read back from RMS

    Code:
    RecordStore rs = RecordStore.openRecordStore(XML_RECORD, false);
    try {
        byte[] data = rs.getRecord(1);
        ByteArrayInputStream bin = new ByteArrayInputStream(data);
        DataInputStream din = new DataInputStream(bin);
        try {
            // read back the data
        } finally {
            din.close();
        }
    } finally {
        rs.closeRecordStore();
    }
    I notice in your code you appear to be keeping the record store open. You will have problems if you do this. Open the record store, use it, then close it as quickly as possible. Record stores that are left open can result in lost data, or even corrupted record stores.

    When writing a Vector of objects that are of different classes, you might want to start by writing (and reading) an integer count of the size of the Vector. This will make it easy to read back the correct number of objects in a for() loop. Also, before writing each object, write a "class ID" (say, 1 for "TypeOneField" and 2 for "TypeTwoField"), so you know which class to instantiate.

    Code:
    // read back the data: for example...
    int count = din.readInt();
    for (int i = 0; i < count; i++) {
        int id = din.readInt();
        Persistent p;
        switch (id) {
        case 1:
            p = new TypeOneField();
            break;
        case 2:
            p = new TypeTwoField();
            break;
        default:
            throw new Exception("unknown id: " + id);
        }
        p.reconstruct(din);
        yourVector.addElement(p);
    }
    Note that I'm passing the DataInput object to the reconstruct() method, not a byte[], so that it mirrors the serialize(DataOutput) method.

    Does that help?

    Graham.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    20

    Re: Read back from RMS

    thanks a lot graham! i will try it and post my success or failure ! but i think it will help me to go further and finnish it, i got your idea now.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    20

    Re: Read back from RMS

    hi graham (and all others ),
    you wrote that i have to write and read the size of the vector first. i'm not sure about that. i have my two methods (serialize and reconstruct) in TypeOnefield and TypeTwofield, as you know.

    Code:
        
        public void serialize(DataOutput out) throws IOException {
            out.writeInt(); // I don't know the size of the vector here, and why do you need the vector size for the for-loop? i dont get it :(
            out.writeInt(CLASS_ID);
            out.writeUTF(getName());
            out.writeUTF(getDescription());
            out.writeUTF(getTextfieldname());
            out.writeUTF(getType());
            out.writeUTF(getValue());
        }
        
        public void reconstruct(DataInput din) throws IOException {
            this.name = din.readUTF();
            this.description = din.readUTF();
            this.textfieldname = din.readUTF();
            this.type = din.readUTF();
            this.value = din.readUTF();
        }
    i took your code below with the for-loop, so i think it is the last part writing the size in my stream..and hopefully i'm done after that
    thanks again!
    -matze

  5. #5
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Read back from RMS

    You're writing the objects in the Vector in a loop, so...

    Code:
    int count = yourVector.size();
    out.writeInt(count);
    for (int i = 0; i < count; i++) {
        ((Persistent) (yourVector.elementAt(i))).serialize(out);
    }
    You need to know the number of elements in the vector when you're reading them back, so you know how many to read (it's less trouble that relying on "end of file" mechanisms).

    Graham.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    20

    Re: Read back from RMS

    yeah it works, perfectly ! thanks one more time!

    after a little research i found out that the minimal capacity of RMS is 8kb. That's really bad, because maybe i have more then only 8 kb...Do you think i should provide a second write-method with the file connection api? otherwise some phones are not compatible with the application.

    and i wanna know, how do you debug your applications? i'm using 3 different emulators and a few mobilephones. but i have only nokia phones ! is there a proper way to debug it on a mobilephone or how do you handle it?

    thanks again

  7. #7
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Read back from RMS

    Quote Originally Posted by bumble-bee View Post
    after a little research i found out that the minimal capacity of RMS is 8kb. That's really bad, because maybe i have more then only 8 kb...
    Sure... but this is the minimum required by the MIDP-1 specification. Even the oldest, lowest-spec Nokias from six years ago provide 20k RMS per application. Newer Series 40s provide around 100k, and Series 60s have no limit. I am not aware of any phone that provides the minimum 8k - and most have no limit at all (except the amount of free flash memory, or whatever their maximum file size is (probably 2Gb)). I would never suggest trying to store gigabytes of data in RMS, but if you have something like 10 or 20k, you will be fine.

    Quote Originally Posted by bumble-bee View Post
    Do you think i should provide a second write-method with the file connection api? otherwise some phones are not compatible with the application.
    The FileConnection API is painful to use, especially if you want to write data, since it is subject to many security restrictions. You are much better to use RMS.

    Quote Originally Posted by bumble-bee View Post
    and i wanna know, how do you debug your applications? i'm using 3 different emulators and a few mobilephones. but i have only nokia phones ! is there a proper way to debug it on a mobilephone or how do you handle it?
    You mean testing or debugging?

    As you'll start to discover, different devices can behave very differently, so testing on as many phones as you can is a good idea. Test on older phones from different manufacturers, with the lowest spec you want to support. An old Series 40, and old Series 60, a Motorola (say, a V3 RAZR), an older Sony Ericsson (maybe a K700). Phones from Nokia, Sony Ericsson and Motorola tend to have good compatibility, so as long as you test on older, lower-spec devices, you have a good chance your app will work on newer ones.

    Why older devices? Usually, older devices have bugs, and newer devices have fixes (not always true, but often). Also, newer devices are usually faster, have more memory, more APIs, etc. Again, not always true, so check the specifications.

    For debugging (once you find a problem), first choice is any emulator you can reproduce the problem in. Debugging with emulators is always easiest, especially if you can hook them into your IDE and debug properly, and single step your code in the IDE.

    If you can't reproduce the issue on an emulator, only on a real handset, then life is more difficult. Some devices (newer Nokias, all Sony Ericssons, and some Motos if you can enable the feature) support on-device debug. This allows you to connect the phone to your computer, and debug in the IDE while running on the device. If the handset doesn't support that, then you may be limited to some trial-and-error.

    Does that help?

    Graham.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    20

    Re: Read back from RMS

    hi graham,
    yes that helps a lot! i'm a, lets say "newbie", in mobile programming and know i know from an experienced programmer how it goes. i thought that it would be like you said, so everything is perfect !

    thanks another time for your help!

    -matze

Similar Threads

  1. Problem in Going Back from Form to List
    By lopesrohan1988 in forum Symbian User Interface
    Replies: 2
    Last Post: 2009-07-07, 08:11
  2. About RMS database secutity
    By browndrf in forum Mobile Java Networking & Messaging & Security
    Replies: 9
    Last Post: 2007-07-27, 18:45
  3. Errors building project in VS.2003
    By jensesaat in forum Symbian
    Replies: 11
    Last Post: 2006-11-13, 16:54
  4. Replies: 5
    Last Post: 2006-08-02, 12:35
  5. socket read hangs until socket is closed
    By camroe in forum Mobile Java Networking & Messaging & Security
    Replies: 1
    Last Post: 2005-07-21, 09:44

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •