United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6313849 Provide API to close/disconnect an UrlConnection
JDK-6313849 : Provide API to close/disconnect an UrlConnection

Details
Type:
Enhancement
Submit Date:
2005-08-21
Status:
Open
Updated Date:
2011-02-16
Project Name:
JDK
Resolved Date:
Component:
core-libs
OS:
windows_2000
Sub-Component:
java.net
CPU:
x86
Priority:
P5
Resolution:
Unresolved
Affected Versions:
5.0
Targeted Versions:

Related Reports

Sub Tasks

Description
A DESCRIPTION OF THE REQUEST :
Provide a method on the UrlConnection class to free all connected resources, e.g. close opened input and output streams.

Proposed method name: disconnect()

JUSTIFICATION :
Some methods on the UrlConnection class, such as getContentLength, might silently connect to network resources, without releasing them again. I assume that releasing them again and again might be too costly. Still, a way to easily free all those resources explicitely when done would be handy.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
All external resources occupied while working on the connection are freed.
ACTUAL -
No such method seems to be available.

---------- BEGIN SOURCE ----------
/* Example showing resource allocation for a file url connection */

import java.io.File;
import java.net.URLConnection;

public class FileUrlGetContentLength {
  public static void main(String[] args) throws Exception {
    File file = new File("foo.txt");
    file.delete();
    System.out.println("create: " + file.createNewFile());

    URLConnection connection = file.toURL().openConnection();
    connection.getContentLength();
    //    connection.getInputStream().close();
    System.out.println(file.delete());  // *** deleting the file will fail unless above line is uncommented
  }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Knowing which methods access which resources (such as the input stream) close those resources explicitely.

Although the JavaDoc states that this might be necessary, it isn't a very obvious or elegant way of doing it.

                                    

Comments



Hardware and Software, Engineered to Work Together