JDK-6884800 : (file) Path.newInputStream does not usefully implement available()
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 7
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2009-09-23
  • Updated: 2010-08-19
  • Resolved: 2009-10-09
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b74Fixed
Related Reports
Relates :  
Description
===

Bad news:
I think I've found a bug in Path.newInputStream() for the default file system. The stream that is returned immediately returns 0 for available(). The javadoc for InputStream woffles a bit but somewhat indicates that 0 means end of file.  It seems unreasonable for Path.newInputStream() to return a stream like this.  The consequence is that javac immediately thinks the file is empty and stops reading it.  If I use FilterInputStream to override the value to always return 1 then javac gets a lot further with this file manager.

-- Jon 

===

This is a specific case of:
  4648049: (ch) Stream adaptors do not usefully implement available()

Where the channel is to a file (or more generally, a SeekableByteChannel) then it can be special-cased so that the input stream behaves like FileInputStream.

Comments
EVALUATION As per description - this case can be fixed by checking if the channel is a SeekableByteChannel.
23-09-2009

SUGGESTED FIX diff -r aac01ec2cec4 src/share/classes/sun/nio/ch/ChannelInputStream.java --- a/src/share/classes/sun/nio/ch/ChannelInputStream.java Mon Sep 14 17:47:26 2009 +0100 +++ b/src/share/classes/sun/nio/ch/ChannelInputStream.java Wed Sep 23 09:41:23 2009 +0100 @@ -109,6 +109,15 @@ public class ChannelInputStream return ChannelInputStream.read(ch, bb, true); } + public int available() throws IOException { + if (ch instanceof SeekableByteChannel) { + SeekableByteChannel sbc = (SeekableByteChannel)ch; + long rem = Math.max(0, sbc.size() - sbc.position()); + return (rem > Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int)rem; + } + return 0; + } + public void close() throws IOException { ch.close(); }
23-09-2009