Created
September 26, 2014 04:48
-
-
Save andrerocker/196747e5b01d687b8cb7 to your computer and use it in GitHub Desktop.
Serialize and unserialize any java object to a base46 string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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