My app reads and writes large serialized objects to files and I'd like to monitor the progress of this. Reading an object in works fine, because I can get the total size of the file in bytes first, and then update the progress as the bytes are read in. However, when writing a file out, I'm unsure how to get the total size of an object in bytes when it gets serialized, which I believe I would need to find out what percent done the operation is. Is there a way to find out how much space an object is taking up in memory or how much it will take up when serialized? If not, is there another way to monitor the progress?
Write yourself a FilterOutputStream that does monitoring in the write() methods and put it between the OOS and whatever it is presently wrapped around, e.g. FileOuputStream or Socket.getOutputStream().
However there's no useful way to know the size of an impending serialization other than to do it twice, once to a ByteArrayOutputStream to measure it, and again to the real target, which is pretty wasteful.
Are these objects so large that you really need to do this?