question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Off by one when wrapping InputStream

See original GitHub issue

image

I use ProgressBar.wrap(InputStream, ProgressBarBuilder) where the stream is created by URL.openStream(). I set the initial max to the actual file size. Reducing the max by one (or even 10) has zero effect and will still show the actual file size (as if I set it to the correct size).

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:15 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
mordechaimcommented, Jan 28, 2020

So it is still a bug, right?

Yes, this is a bug. Here you do it correctly.

1reaction
mordechaimcommented, Jan 27, 2020

Looking at the code, Util.getInputStreamSize() returns -1 for non-file streams (which I use) and that is used in the string constructor, but passing my own builder and leaving out the max initial it somehow detects it correctly.

While the second constructor has a check for -1, the first will set the initial max regardless. This means that the first will have -1 while the second will have 0.

This all is only about the apparent inconsistency of the different behaviour in the different constructors. This still doesn’t explain the off by one.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Effect on the original InputStream after wrapping with ...
This method need to wrap this InputStream with a BufferedInputStream to use its mark and reset functionality. However, the passed in InputStream ......
Read more >
InputStream Wrapper that closes the stream once reading is ...
The general idea of a wrapper stream that closes the inner stream automatically once its end is reached is fine, but your implementation...
Read more >
Streams - Learning Java, 4th Edition [Book] - O'Reilly
A method specifying an InputStream as an argument can accept any subclass of InputStream . Specialized types of streams can also be layered...
Read more >
Java InputStream - Jenkov.com
Here is an example of wrapping a Java InputStream in a BufferedInputStream : InputStream input = new BufferedInputStream( new FileInputStream("c ...
Read more >
InputStreamReader (Java Platform SE 7 ) - Oracle Help Center
Each invocation of one of an InputStreamReader's read() methods may cause one or more bytes to be read from the underlying byte-input stream....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found