Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JProgressBar from InputStream

There are many posts on JProgressBar, but I can not find a solution for the following problem. How do I update JProgressBar measuring bytes received from InputStream?

For example, I have the following code:

int bytesRead;
byte buffer = new byte[1024];
ByteArrayOutputStream getBytes = new ByteArrayOutputStream();

while ((bytesRead = dis.read(buffer)) != -1) {
    getBytes.write(buffer, 0, bytesRead);
}

return(getBytes.toByteArray());
like image 973
jadrijan Avatar asked May 19 '26 11:05

jadrijan


1 Answers

If you know how many bytes you will be reading, you can set that as the max and increment the progress bar by the number of bytes read for each iteration of the loop.

If you don't know how many bytes you will read, set the progress bar to indeterminate mode and it will animate itself showing that "some" progress is occurring.

like image 91
unholysampler Avatar answered May 22 '26 00:05

unholysampler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!