Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User Godmachine's Avatar
    Join Date
    Mar 2003
    Posts
    5
    hi am trying to write some code for serailizing objects from an array to rms and deserialize them back out. the problem is that it only read the second item twice. heres my code:

    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;
    import javax.microedition.rms.*;
    import java.io.*;

    public class serialTest extends MIDlet
    {
    User users[] = new User[2];
    RecordStore rs = null;

    public serialTest() { }

    public class User {
    private String password;
    private String name;

    public User(){
    this(null,null);
    }

    public User(String name, String password){
    this.name = name;
    this.password = password;
    }

    public String getName() {return name;}
    public String getPassword() {return password;}

    // rms methods
    public synchronized byte[] serialize()
    {
    try
    {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    DataOutputStream dout = new DataOutputStream(bout);
    dout.writeUTF(name != null ? name : "");
    dout.writeUTF(password != null ? password : "");
    return bout.toByteArray();
    }
    catch (Exception e) {return null;}
    }

    public synchronized boolean deserialize(byte[] data)
    {
    try
    {
    ByteArrayInputStream bin = new ByteArrayInputStream(data);
    DataInputStream din = new DataInputStream(bin);
    name = din.readUTF();
    password = din.readUTF();
    return true;
    }
    catch (Exception e) {return false;}
    }
    }

    protected void startApp() throws MIDletStateChangeException
    {
    users[0] = new User("Bob", "qwerty");
    users[1] = new User("Steve", "asdfghjk");
    try
    {
    RecordStore.deleteRecordStore("Users");
    rs = RecordStore.openRecordStore("Users", true);
    try
    {
    System.out.println(rs.getNumRecords());
    byte[] data;
    for(int i=0;i<users.length;i++)
    {
    System.out.println("adding record");
    data = users[i].serialize();
    rs.addRecord(data, 0, data.length);
    }
    System.out.println(rs.getNumRecords());
    rs.closeRecordStore();
    } catch(RecordStoreFullException rsfe){ }
    } catch(RecordStoreException rse){ }
    try
    {
    System.out.println("opened recordstore");
    rs = RecordStore.openRecordStore("Users", false);
    System.out.println(rs.getNumRecords());
    try
    {
    byte[] data;
    for(int i=0;i<rs.getNumRecords();i++)
    {
    System.out.println("reading record");
    data = rs.getRecord(i+1);
    users[i].deserialize(data);
    }
    rs.closeRecordStore();
    } catch(RecordStoreException rse){ }
    } catch(RecordStoreException rse){ }
    for(int i=0;i<2;i++)
    {
    System.out.println("*****check return methods*******");
    System.out.println("name = " + users[i].getName());
    System.out.println("password = " + users[i].getPassword());
    }
    }

    protected void destroyApp(boolean unconditional) { }

    protected void pauseApp() { }
    }

    any help would be apprecited thanks.

  2. #2
    Registered User Godmachine's Avatar
    Join Date
    Mar 2003
    Posts
    5
    actually ik ok now i changed the order of code and it looks to work fine sorry to trouble you all.

  3. #3
    Registered User bracz2's Avatar
    Join Date
    Mar 2003
    Posts
    2
    While reading back the recordstore your code assumes that the record id of the two records is 1 and 2, respectively.
    Basically this could lead to problems.
    Consider using a RecordEnumeration to query the whole or part of the recordstore. It is not only safer and cleaner, but somewhat easier (!) also than the code you provided.

    i=0;
    RecordEnumeration enum=rs.enumerateRecords(null,null,false);
    while(e.hasNextElement()) {
    users[i++].deserialize(enum.nextRecord());
    }

    Also think about the differences of extending the existing code with support of sorted retrieval...

  4. #4
    Registered User Godmachine's Avatar
    Join Date
    Mar 2003
    Posts
    5
    yeah the enumerator is better cheers for the info. the only thing i can say about it. is that it seems to read the records in backwards which i cant remeber happening before but its not vital to have them in the order entered so its not a problem to me

    also i have to intialise the array to the size of the recordstore before adding records to it. code as follows:

    for(int j=0;j<rs.getNumRecords();j++) {users[j] = new User("default", "default");}

    but i dont have a problem with that i should but i dont.

    cheers for the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved