Do not do single byte IO.  Most of our ObjectStream implementations perform very poorly.  Allocate a new byte[4096] or new byte[8192] and do IO in larger units.

Also the size is well known (outputStream.getSize()).  It is good to be paranoid, so double check that the total number of bytes output matches the size of the stream.  If they differ, complain, loudly with a die.