I am trying to implement an Externalizable interface for storing data using LWUIT-IO storage. This worked great for simple objects consisting of strings, booleans and ints.
However, I have an object consisting of these types, but also a vector of the aforementioned Externalizable object. This seems to have messed up the process, and I get nothing when I try to retrieve an object from storage.
I suggested that this is similar to the Serializable interface and that Externalizable objects inside the main object are automatically processed. I am not sure if this is true, or why it does not succeed.
The object inside the object:
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 contained 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 does not allow me to save the playlist (containing the object), and if I try to save it first, does it work?
Note that overriding methods are different from regular Java, as this is a version of the Externalizable LWUIT interface.
source share