On Tue, Nov 24, 2009 at 11:43 AM, Jesse Wilson <jessewil...@google.com> wrote: > On Tue, Nov 24, 2009 at 9:12 AM, Tim Ellison <t.p.elli...@gmail.com> wrote: > >> Seems to be caused by a change in the implementation of available() for >> InputStreams on Zip entries. >> >> The failing code is in >> org.apache.harmony.sound.utils.ProviderService#getProviders(String) when >> it tries to read the whole content using available()... >> >> <snip> >> InputStream in = urls.nextElement() >> .openStream(); >> bytes = new byte[in.available()]; >> in.read(bytes); >> in.close(); >> <snip> >> > > Aahhhh.. gotcha. > > > >> Of course, this is bad form, and when I switch to use the old favourite >> getAllBytesFromStreamAndClose(InputStream) things start working again. >> > > Sweet. > > >> We can debate whether we should try to improve the available() impl or not. >> > > If we want to be true to the > spec<http://java.sun.com/javase/6/docs/api/java/util/zip/InflaterInputStream.html#available()>, > we can't change it.
Even if we consider the generic InputStream#available() doc [1], which is what the above code is working with, the code is broken - you can't use available as a reliable means of determining what will be returned. At most, available will return a best guess. [1] http://java.sun.com/javase/6/docs/api/java/io/InputStream.html#available%28%29 > > > public int *available*() > throws IOException <../../../java/io/IOException.html> > > Returns 0 after EOF has been reached, otherwise always return 1. > > Programs should not count on this method to return the actual number of > bytes that could be read without blocking. > > *Overrides:*available <../../../java/io/FilterInputStream.html#available()> in > class FilterInputStream <../../../java/io/FilterInputStream.html> *Returns:*1 > before EOF and 0 after > EOF.*Throws:*IOException<../../../java/io/IOException.html> - > if an I/O error occurs. >