tags:

views:

48

answers:

2

I am using an ObjectOutputStream to send a large object (possibly a Map) to the server using my Swing application, Is there anyway to monitor the percentage sent etc as in a file upload.

+1  A: 

You could serialize your object to a byte array, like this (incomplete, credit http://www.exampledepot.com/egs/java.io/SerializeObj.html):

// Serialize to a byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
out = new ObjectOutputStream(bos);
out.writeObject(object); 
out.close(); 

// Get the bytes of the serialized object
byte[] buf = bos.toByteArray(); 

Then send the bytes to the server, one segment at a time. This way you'll always know what percentage of the total has been sent. On the other side, you'll have to reconstruct your object.

danben
Thx danben, got it!
Azlam
+1  A: 

Yes there is.

You can create your own implementation of OutputStream and wrap it around your ObjectOutputStream. Output streams are decorators so your implementation can count how many bytes have been sent and using SwingWorker asynchronously update the UI.

Initialize your implementatio of the OutputStream with the size of the object that has to sent to know how many bytes need to be sent. Look at danben's answer.

Boris Pavlović