Posted By

ff1959 on 10/26/09


Tagged

java array convert


Versions (?)

Convert object to byte array


 / Published in: Java
 

From time to time, coders must convert an object to a stream of bytes. The following method accomplishes this task.

  1. public static byte[] convertToByteArray(Object obj) throws IOException
  2. {
  3. ObjectOutputStream os = null;
  4.  
  5. os = new ObjectOutputStream(new BufferedOutputStream(byteStream));
  6. os.flush();
  7. os.writeObject(obj);
  8. os.flush();
  9. byte[] sendBuf = byteStream.toByteArray();
  10. os.close();
  11. return sendBuf;
  12. }

Report this snippet  

You need to login to post a comment.