JDK-6344172 : Closing of returned streams is not specified in Socket, URL, and URLConnection
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 6
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2005-11-01
  • Updated: 2012-01-11
  • Resolved: 2006-04-29
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 6
6 b83Fixed
Related Reports
Relates :  
Description
Bug 5012725 describes several places in java.net where additional documenation regarding the impact of calling close() on the returned streams is necessary. 
 
Here are the pertinent portions of that bug: 
 
For various methods in the JDK API that return an already-open InputStream, OutputStream, Reader, or Writer, the JDK documentation (javadoc) does not specify whether or not it is the caller's responsibility to call 'close()' on the returned stream, or whether something else (e.g. the object on which the stream-returning method was called) may also close the returned stream (and under what circumstances) or what the consequences of the caller calling 'close()' will be. 
 
Some of the methods for which I have seen this problem include: 
    Socket.getInputStream(), getOutputStream(), etc. 
    Process.getInputStream(), getOutputStream(), etc. 
    URL.openStream(), etc. 
    URLConnection.openInputStream(), etc. 
 
[ ... ] 
 
As a result, there is a great deal of confusion on how to use these methods, and in many cases, the most commonly accepted method of using  them is either unnecessarily inefficient (e.g. a stream getting ultimately closed by finalize() when garbage collection occurs) or 
outright wrong (e.g. having the object that created the stream close it, causing clients which may still be reading or writing to it to throw exceptions at the next access). 
 
I believe strongly that EVERY method in the JDK which returns an InputStream, OutputStream, Reader, or Writer object should specify in its documentation: 
    1) If the caller is required to call close() on the returned 
       stream. 
    2) If the class supplying the stream will ever call close() on the  
       stream, and if so, at what times. (Does closing a socket also 
       close its output stream? Or vice versa?) 
    3) What the consequences of calling close() on the returned stream  
       will be, and how that will affect future calls to the 
       stream-producing method or other methods on the 
       stream-producing object. (Does closing a socket's output stream  
       also close the socket?)

Comments
EVALUATION The javadoc for Socket.close(), Socket.getInputStream() and Socket.getOutputStream() need to be more specific.
24-04-2006