I'm trying to download a large file from URL with commons.io Apache library. This is my code:
InputStream stream = new URL(CLIENT_URL).openStream();
ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(null, "Downloading...", stream);
ProgressMonitor pm = pmis.getProgressMonitor();
pm.setMillisToDecideToPopup(0);
pm.setMillisToPopup(0);
FileUtils.copyInputStreamToFile(pmis, new File(LATEST_FILENAME));
pmis.close();
stream.close();
But it doesn't show the popup. Or, to be honest, the popup appear and disappear only for a millisec, while the download take about 10secs.
A generic
InputStreamdoes not provide information about the current position or the total length to the outside wold. See InputStreamavailiable()is not the total size of theInputStreamand there is no such thing like get current position or get total size. You also might read only chunks / parts of the stream, even the progress bar would be able to figure out the total length of the stream it will not know that you are only going to read for example 512 bytes.The
ProcessMonitorInputStreamdecorates the providedInputStreamand updates the progress bar of the dialog box during the read operation. Per default theProgressMonitorInputStreamuseavailableof the passedInputStreamto initialise the max value of theProgressMonitor. The value may be correct for someInputStreamsbut is not especially when you transfer data via network.This initial max value is also the reason why you sometimes see the dialog box. The dialog closes automatically after the max value of the progress bar is reached. In order to display anything useful you must give the
ProgressMonitorsome hints about the start position and the end position in form ofsetMinimumandsetMaximum.