ctongfei / progressbar

Terminal-based progress bar for Java / JVM
http://tongfei.me/progressbar/
MIT License
1.07k stars 102 forks source link

Max size keeps increasing with current size problem #125

Closed NikR-IV closed 2 years ago

NikR-IV commented 2 years ago

Hello! This library work all fine for me, but when I tried to visualize the process of reading from a HTTP response bodystream, progress bar's max size kept increasing with current size from 0, but not set to the value used in .setInitialMax() here's my code:

        ProgressBarBuilder pbb = new ProgressBarBuilder().setTaskName("download").setInitialMax(trueSize).setUnit("MiB", 1048576);  //trueSize here is 722
        try(InputStream is =  ProgressBar.wrap(response.bodyStream(),pbb)){
            Files.copy(is, Paths.get(downloadPath));
        } catch (IOException e) {
            e.printStackTrace();
        }

and console output's just like this: download 100% │████████████████████████████│ 0/0MiB (0:00:00 / 0:00:00) download 100% │████████████████████████████│ 1/1MiB (0:00:03 / 0:00:00) download 100% │████████████████████████████│ 2/2MiB (0:00:05 / 0:00:00) download 100% │████████████████████████████│ 3/3MiB (0:00:08 / 0:00:00) ...

ctongfei commented 2 years ago

the trueSize here should be 722 * 1048576 since you are setting the unit to be MiB = 1048576.

NikR-IV commented 2 years ago

It still doesn't work for me :(

        long trueSize = 757071872;
        ProgressBarBuilder pbb = new ProgressBarBuilder().setTaskName("download").setInitialMax(trueSize).setUnit("MiB", 1048576);
        try(InputStream is =  ProgressBar.wrap(response.bodyStream(),pbb)){
            Files.copy(is, Paths.get(downloadPath));
        } catch (IOException e) {
            e.printStackTrace();
        }
NikR-IV commented 2 years ago

I change the code in public static InputStream wrap(InputStream is, ProgressBarBuilder pbb) from if (size != -1 && pbb.initialMaxIsSet()) to if (size != -1 && !pbb.initialMaxIsSet()), and it works.

NikR-IV commented 2 years ago

I change the code in public static InputStream wrap(InputStream is, ProgressBarBuilder pbb) from if (size != -1 && pbb.initialMaxIsSet()) to if (size != -1 && !pbb.initialMaxIsSet()), and it works.

ctongfei commented 2 years ago

Thanks @NikR-IV ! Great catch!