ctongfei / progressbar

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

Wrapped InputStream doesn't automatically detect size #127

Closed Markus74a closed 2 years ago

Markus74a commented 2 years ago

I'm trying to use ProgressBar.warp() to show progress on a FileInputStream, but it will not detect the size of the stream unless I first set some initial size.

This is the current ProgressBar 0.9.3 implementation:

    public static InputStream wrap(InputStream is, ProgressBarBuilder pbb) {
        long size = Util.getInputStreamSize(is);
        if (size != -1 && pbb.initialMaxIsSet())
            pbb.setInitialMax(size);
        return new ProgressBarWrappedInputStream(is, pbb.build());
    }

Even if the InputStream size gets detected correctly, it will not use that information.

The comparison should be !pbb.initialMaxIsSet() instead of pbb.initialMaxIsSet().

This code will not detect the correct file size:

ProgressBar.wrap(is, new ProgressBarBuilder());

This code will detect the correct file size:

ProgressBar.wrap(is, new ProgressBarBuilder().setInitialMax(123456));

Having to set a value which gets ignored is not intuitive.

ctongfei commented 2 years ago

Thanks for the great catch! This should be fixed with #126 .