Menu

#15 Use of InputStream.available should be changed

open
nobody
None
5
2008-08-30
2008-08-30
No

Hi,

I came across a case where an implementation of InputStream doesn't override available() which means that available() always returns 0. This is a problem at three locations: ZipContentTypeManager, ZipPartMarshaller and OpenXMLDocument. Data is read from the stream using a while loop:

while (ins.available() > 0) {
...
}

This will fail in this case. (No data is read)

API doc for InputStream.available():

"Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream. The next caller might be the same thread or another thread.

The available method for class InputStream always returns 0.

This method should be overridden by subclasses. "

Returning 0 although data is there is valid for an InputStream. So my proposed change is to replace the while loop with this:

while (true) {
...
}

(The loop is left when read() returns -1, so this should be safe.)

Best wishes,
Rainer

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.