aspose file tools*
The moose likes Java in General and the fly likes Is ProgressMonitorInputStream broken? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is ProgressMonitorInputStream broken?" Watch "Is ProgressMonitorInputStream broken?" New topic
Author

Is ProgressMonitorInputStream broken?

Christian Beil
Greenhorn

Joined: Jan 03, 2010
Posts: 7
Hi there,

Is ProgressMonitorInputStream broken or am I missing something?

In the constructor it reads the "size" of the InputStream and sets is as maximum on the ProgressMonitor.
Therefore it uses InputStream.available().
As the JavaDoc of this method states (java.io.InputStream#available())
Returns an estimate of the number of bytes that can be read
...
Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not.
...

the return value is just an estimation and not necessarily the total number of bytes.

I noticed this as I was implementing a class that works similar to ProgressMonitorInputStream.
Using it with a FileInputStream, available() sometimes returned the same as File.length() and sometimes less.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

ProgressMonitorInputStream is only a wrapper around another input stream. It can only retrieve the total length if that input stream knows it. There is only one method for that in InputStream, available(). If that method returns an incorrect value there is nothing ProgressMonitorInputStream can do about that. If for FileInputStreams it does not return the file size then that's not a fault of ProgressMonitorInputStream but of the FileInputStream.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Christian Beil
Greenhorn

Joined: Jan 03, 2010
Posts: 7
But the JavaDoc of java.lang.ProgressMonitorInputStream suggests that it would usually be used with a FileInputStream.
Also in the article Making Progress With Swing's Progress Monitoring API for example, a FileInputStream is used.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

But in the end it is still designed to work with all InputStream. Besides, FileInputStream also has no direct way to get the size (getChannel().size() would do it though).

What you should more ask yourself is: why is the FileInputStream's available() method not returning the file size? How large is the file?
Christian Beil
Greenhorn

Joined: Jan 03, 2010
Posts: 7
But in the end it is still designed to work with all InputStream.

Only under the assumption that available() returns all bytes. And this assumption is either false or should be stated in the JavaDoc, I think.

FileInputStream also has no direct way to get the size (getChannel().size() would do it though).

Yes, File.length() or FileInputStream.getChannel().size() can both be used to get the file size.

What you should more ask yourself is: why is the FileInputStream's available() method not returning the file size? How large is the file?

This method is implemented natively. I don't know how it works exactly, but it is totally ok for the method not to return the number of all available bytes.
I had different results with both bigger and smaller files (only a few KB). Both sometimes returned the true file size and sometimes less.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

Christian Beil wrote:
But in the end it is still designed to work with all InputStream.

Only under the assumption that available() returns all bytes. And this assumption is either false or should be stated in the JavaDoc, I think.

You are right about that. I agree that there is a flaw in the Javadoc.
Christian Beil
Greenhorn

Joined: Jan 03, 2010
Posts: 7
Do you think this should be reported somewhere?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

You could file a bug report at Sun, but to be honest I doubt they will do anything about it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is ProgressMonitorInputStream broken?