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.
|