Embedding an external interface for a complex object in Java. (LWUIT)

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.

+4
source share
1 answer

You need to use Util.register(...) to register these classes as external when your application starts.

Also, calling directly for externalization is incorrect. You should use Util.writeObject/readObject to write another external object (with its own version number). Then you can avoid the loop over the vector, which will be redundant and just write the whole vector.

I also suggest using Util.readUTF/writeUTF , which also support null strings.

As a side element, I suggest switching to Codename one , since LWUIT is no longer supported by anyone. Steve Hannah also has a good externalization record, this is Codename One, which is very similar to LWUIT (although it now supports lists and maps): http://www.shannah.ca/blog/?p=234

+2
source

Source: https://habr.com/ru/post/1493620/


All Articles