Skip to content

Instantly share code, notes, and snippets.

@codenameone
Created March 2, 2016 13:04
Show Gist options
  • Save codenameone/ebfffaffd405ed3b7723 to your computer and use it in GitHub Desktop.
Save codenameone/ebfffaffd405ed3b7723 to your computer and use it in GitHub Desktop.
Full Externalizable class sample for Codenmae One
public class MyClass implements Externalizable {
private static final int VERSION = 2;
private String name;
private String value;
private String domain;
private Date date;
private long expires;
public MyClass() {}
public int getVersion() {
return VERSION;
}
public String getObjectId() {
return "MyClass";
}
public void externalize(DataOutputStream out) throws IOException {
Util.writeUTF(name, out);
Util.writeUTF(value, out);
Util.writeUTF(domain, out);
if(date != null) {
out.writeBoolean(true);
out.writeLong(date.getTime());
} else {
out.writeBoolean(false);
}
out.writeLong(expires);
}
public void internalize(int version, DataInputStream in) throws IOException {
name = Util.readUTF(in);
value = Util.readUTF(in);
domain = Util.readUTF(in);
if(version > 1) {
boolean hasDate = in.readBoolean();
if(hasDate) {
date = new Date(in.readLong());
}
}
expires = in.readLong();
}
}
@codenameone
Copy link
Author

Sample code for Externalizable.

From the Codename One project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment