Skip to content

Instantly share code, notes, and snippets.

@zhaohangbo
Created September 23, 2016 13:12
Show Gist options
  • Save zhaohangbo/f2f36b68a57b2279142ecaebaa4fdbb1 to your computer and use it in GitHub Desktop.
Save zhaohangbo/f2f36b68a57b2279142ecaebaa4fdbb1 to your computer and use it in GitHub Desktop.
Java Serializable Object to Byte Array
http://stackoverflow.com/questions/2836646/java-serializable-object-to-byte-array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(yourObject);
out.flush();
byte[] yourBytes = bos.toByteArray();
...
} finally {
try {
bos.close();
} catch (IOException ex) {
// ignore close exception
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment