Skip to content

Instantly share code, notes, and snippets.

@andrerocker
Created September 26, 2014 04:48
Show Gist options
  • Save andrerocker/196747e5b01d687b8cb7 to your computer and use it in GitHub Desktop.
Save andrerocker/196747e5b01d687b8cb7 to your computer and use it in GitHub Desktop.
Serialize and unserialize any java object to a base46 string
private String serialize(Object object) {
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutputStream output = new ObjectOutputStream(buffer);
output.writeObject(object);
output.flush();
return Base64.encodeBase64String(buffer.toByteArray());
} catch (Exception e) {
return null;
}
}
private Object unserialize(String base64) {
try {
ByteArrayInputStream buffer = new ByteArrayInputStream(Base64.decodeBase64(base64));
ObjectInputStream input = new ObjectInputStream(buffer);
return input.readObject();
} catch (Exception e) {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment