Created
June 7, 2014 10:31
-
-
Save dsaiztc/1d7d16faf164e8bc4d12 to your computer and use it in GitHub Desktop.
Convert object to byte[].
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
/** | |
* Converts an object to a byte array | |
* | |
* @param object Object to convert (must implement Serializable) | |
* @return The object in byte[] | |
*/ | |
public static byte[] objectToByte(Object object) | |
{ | |
byte[] result = null; | |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
ObjectOutput out = null; | |
try | |
{ | |
out = new ObjectOutputStream(bos); | |
out.writeObject(object); | |
result = bos.toByteArray(); | |
} | |
catch (IOException ioe) | |
{ | |
Log.e(TAG, ioe.toString()); | |
} | |
finally | |
{ | |
try | |
{ | |
if (out != null) | |
{ | |
out.close(); | |
} | |
} | |
catch (IOException ex) | |
{ | |
Log.e(TAG, ex.toString()); | |
} | |
try | |
{ | |
bos.close(); | |
} | |
catch (IOException ex) | |
{ | |
Log.e(TAG, ex.toString()); | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment