Socket InputStream.available may return a positive value after shutdown

Alan Bateman Alan.Bateman at oracle.com
Wed Jan 26 04:32:18 PST 2011


Jing LV wrote:
> Hello,
>
> I am using java.net.Socket and find an strange behaviour. If Socket
> inputstream is shutdown, and there was still some data remaining, its
> available() method may still return a positive value. I checked with the
> java spec, and the document of Socket.shutdownInput() said:
> "Places the input stream for this socket at "end of stream". Any data
> sent to the input stream side of the socket is acknowledged and then
> silently discarded.
> If you read from a socket input stream after invoking shutdownInput() on
> the socket, the stream will return EOF. "
> What's more, as I check inputStream.read(), it return a EOF, So I guess
> it would not be correct if available return a positive value.
>   
I agree this needs to be looked at and I've created a bug to track it:

7014860: Socket.getInputStream().available() not clear for case that 
connection is shutdown for reading

(should be available on bugs.sun.com within 24h).

As it stands the behavior is highly platform specific. Solaris's 
ioctl(FIONREAD) does return 0 when the connection has been shutdown for 
reading. Linux continues to return the number of bytes in the socket 
buffer, as does Windows. There may be alternative SocketImpls that do 
something different. Minimally, Socket#getInputStream and 
Socket#shutdownInput should be clarified to make it clear what the 
expected behavior is, even if is platform specific.

-Alan



More information about the net-dev mailing list