Java tutorial
//package com.java2s; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class Main { public static byte[] objectToStream(Object obj) { try { ByteArrayOutputStream buff = new ByteArrayOutputStream(); ObjectOutputStream stream = new ObjectOutputStream(buff); stream.writeObject(obj); stream.close(); return buff.toByteArray(); } catch (IOException e) { e.printStackTrace(); return null; } } }