Skip to content

Instantly share code, notes, and snippets.

@dsaiztc
Created June 7, 2014 10:31
Show Gist options
  • Save dsaiztc/1d7d16faf164e8bc4d12 to your computer and use it in GitHub Desktop.
Save dsaiztc/1d7d16faf164e8bc4d12 to your computer and use it in GitHub Desktop.
Convert object to byte[].
/**
* 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