JDK-8203036 : com.sun.net.httpserver.HttpExchange should implement AutoCloseable
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 10,11
  • Priority: P5
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2018-05-12
  • Updated: 2019-09-26
  • Resolved: 2019-09-02
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 14
14 b13Fixed
Related Reports
CSR :  
Relates :  
Relates :  
Description
A DESCRIPTION OF THE PROBLEM :
Class com.sun.net.httpserver.HttpExchanage has a close() method which should be called when an HttpExchange ends. Therefore, class com.sun.net.httpserver.HttpExchanage  should implement AutoCloseable.

Expected behavior: Classes which need to be closed can be used with try-with-resources statements because they implement AutoCloseable.
Actual behavior: HttpExchange cannot be used with try-with-resources statements because it doesn't implement AutoCloseable.



Comments
URL: https://hg.openjdk.java.net/jdk/jdk/rev/c4ec55644b4b User: michaelm Date: 2019-09-02 15:01:12 +0000
02-09-2019

The com.sun.net HTTP Server API ( added in 1.6 ) pre-dates the java.lang.AutoCloseable interface ( added in 1.7 ). So this explains why Http(s)Exchange does not implement AutoCloseable. Http(s)Exchange has not, yet, been revisited since, to determine if it makes sense to be AutoCloseable, or not. The typical use-case for an exchange is to read all the exchange's input data, then write an appropriate response. Both input and output are accessed through InputStream and OutputStream, respectively. Since the streams are I/O streams they are AutoCloseable, and are often observed in code within try-with-resources constructs. Since the `close` method is just a convenience to close both input and output streams, then it is less likely to be used in try-with-resources constructs. That said, it does seem reasonable to retrofit AutoCloseable, although low priority.
14-05-2018