Question

I'm trying to implement the Externalizable interface to store the data using the LWUIT-IO's storage. This worked great for simple objects that are composed of Strings, booleans and ints.

However, I have an object that is composed of these types, but also of a Vector of the above mentioned Externalizable object. This seem to mess up the process and I get nothing when I try to retrieve the object from storage.

I assumed it was like the Serializable interface and that the Externalizable objects inside the main object are automatically handled. I'm not sure if this is true, or why it's failing.

The object inside the object is:

public class Song implements Externalizable{

    String name = "examplesongname";

    public void externalize(DataOutputStream out) throws IOException {
        out.writeUTF(name);
    }

    public void internalize(int version, DataInputStream in) throws IOException {
        name = in.readUTF();
    }

    public String getObjectId() {
        return "pat.objects.Song";
    }

    public int getVersion() {
        return 1;
    }
}

The containing object is as follows:

public class Playlist implements Externalizable{
    String name = "exampleplaylistname";
    Vector songs = new Vector();

    public void externalize(DataOutputStream out) throws IOException {        
        out.writeUTF(name);
        out.write(songs.size());
        Enumeration allItems = songs.elements();

        while(allItems.hasMoreElements()){
               Externalizable nextItem = (Externalizable) allItems.nextElement();
               nextItem.externalize(out);
        }
    }

    public void internalize(int version, DataInputStream in) throws IOException {
        name = in.readUTF();

        int size = in.readInt();
        songs= new Vector();
        for(int currentIndex = 0; currentIndex < size; currentIndex++){
             Object nextItem = new Object();
             ((Externalizable)nextItem).internalize(version, in);
              songs.addElement(nextItem);
        }
    }
    }

    public String getObjectId() {
        return "pat.objects.Playlist";
    }

    public int getVersion() {
        return 1;
    }
}

What am I doing wrong or missing that is making the Playlist (containing object) fail to be stored while if I try to store the first one by itself it works?

Please note that the overriding methods are different that normal Java since this is the LWUIT version of Externalizable interface.

Was it helpful?

Solution

You need to use Util.register(...) to register these classes as externalizable when your app starts up.

Also the call directly to externalize isn't correct. You should use Util.writeObject/readObject to write another externalizable object (with its own version number). You can then avoid the loop over the vector which would be redundant and just write the whole vector.

I would also suggest using Util.readUTF/writeUTF which support null strings as well.

As a sidenote, I'd suggest migrating to Codename one since LWUIT is no longer maintained by anyone. Also Steve Hannah has a nice writeup on externalization is Codename One which is pretty similar to LWUIT (although it now supports Lists and Maps): http://www.shannah.ca/blog/?p=234

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top