0

How to check the Progress of Java read/write serilization and display it in a JProgressBar? The tasks that need to be done are these:

  1. Check how many bytes have been read/written, and get a percentage done by dividing this by the total bytes that need to be read/written.
  2. Putting this value in a progress bar and then repaint the JFrame to update the background and display the JProgressBar

Another problem could be how to do this for either reading or writing, as they are both very different. If it cannot be done for writing that is fine, but monitoring the reading is a priority.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Evared
  • 93
  • 1
  • 1
  • 7
  • *"Cheers, evared"* Don't include sigs. in questions. They are noise. – Andrew Thompson Jul 24 '13 at 15:13
  • Use a `SwingWorker`. See [Worker Threads and SwingWorker](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) for details. – Andrew Thompson Jul 24 '13 at 15:15
  • At what point do you know the size of the stream? – trashgod Jul 24 '13 at 16:58
  • I'm using length() to find the size of the file in bytes before the JProgressBar is loaded so i can pass the size into the bar and set it as its max value. Now all i need to do is see how much of the file has been loaded to the JVM and set the JProgressBar accordingly. – Evared Aug 03 '13 at 21:29

1 Answers1

0

To check the progress of a Reading operation is very different to writing. This forum post covers this issue of getting the size of an Object fairly well saying: "...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."

If the size of an Object after serialization cannot be noted before the serialization there is no way that I'm aware of to track the progress.

As far as reading goes, it is much simpler to track the progress using a ProgressMonitorInputStream, and there is a tutorial here on how to use it.

Evared
  • 93
  • 1
  • 1
  • 7