Skip to content

Instantly share code, notes, and snippets.

@dagvadorj
Created November 1, 2012 05:45
Show Gist options
  • Save dagvadorj/3992076 to your computer and use it in GitHub Desktop.
Save dagvadorj/3992076 to your computer and use it in GitHub Desktop.
JSF converter for serializable objects; can be used for h:selectOneMenu etc
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import biz.source_code.base64Coder.Base64Coder;
public class ObjectSerializer {
public static Object fromString(String s) throws IOException,
ClassNotFoundException {
byte[] data = Base64Coder.decode(s);
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(
data));
Object o = ois.readObject();
ois.close();
return o;
}
public static String toString(Serializable o) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(o);
oos.close();
return new String(Base64Coder.encode(baos.toByteArray()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment