code: public static void writeObjectToStream(final Object o, OutputStream out) {
      out = new BufferedOutputStream(out);
      try {
        final ObjectOutputStream oos = new ObjectOutputStream(out);
        oos.writeObject(o);
        oos.flush();
      } catch (final IOException e) {
        e.printStackTrace();
      }
    }
docs: Serialize java object to an ObjectOutputStream. The stream remains open.
  
  @param o
  the java object
  @param out
  the output stream