Search code examples
javasocketsprogressobjectoutputstreamobjectinputstream

Getting Progress of ObjectOutputStream/ObjectInputStream


I recently figured out how to use ObjectOutputStream and ObjectInputStream to send objects over a simple Java socket connection between a server and a client. I was wondering if I wanted to transfer an object that might be large in size, for example an image, is it possible to put a thread that keeps track of the progress of how much data has been sent/received? If the answer to this question isn't very direct, could someone explain how I might go about doing something similar? Thanks in advance!


Solution

  • The Apache Commons IO library has a pair of classes CountingInputStream and CountingOutputStream that implement byte counting input/output stream filters.

    If you insert these into your stream chains you can track the number of bytes read or written. (These filters have to be inserted somewhere between the physical input/output stream and object stream.)

    You could implement the same thing yourself by subclassing the FilterInputStream and FilterOutputStream classes. And there is even a Swing class called ProgressMonitorInputStream that might implement exactly what you need.