FileUrlConnection can leak file descriptors
-------------------------------------------
Key: JBCOMMON-82
URL:
https://jira.jboss.org/jira/browse/JBCOMMON-82
Project: JBoss Common
Issue Type: Bug
Security Level: Public (Everyone can see)
Components: common-core (2.x)
Environment: jdk 1.6
Reporter: Nikolay Tsankov
The following code demonstrates the problem, I would suggest storing the FileInputStream
on first call of getInputStream() and return it on subsequent calls :
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.jboss.net.protocol.file.FileURLConnection;
public class FileUrlConnectionBug {
static File dir = new File("C:/test_file_url_connection");
public static void main(String[] args) throws Exception {
File tmp = makeFile("file.txt");
FileURLConnection connection = new FileURLConnection(tmp.toURI().toURL());
InputStream inputStream = connection.getInputStream();
//get the content, which is also an input stream
//calling this method causes getInputStream() to be called twice, see getContent()
//in java.net.URLConnection
InputStream contentInputStream = (InputStream) connection.getContent();
//close both streams
inputStream.close();
contentInputStream.close();
//file cannot be deleted, as there is an open file descriptor left
if (!tmp.delete()) {
System.err.println("Streams closed, but file cannot be deleted after getContent()
is called");
}
//When getContent() is not called, everything is ok, file can be deleted
File tmpOk = makeFile("fileOk.txt");
FileURLConnection connectionOk = new FileURLConnection(tmpOk.toURI().toURL());
InputStream inputStreamOk = connectionOk.getInputStream();
inputStreamOk.close();
if (tmpOk.delete()) {
System.out.println("File deleted ok if getContent() is not called");
}
}
private static File makeFile(String name) throws Exception {
dir.mkdir();
File tmp = new File(dir, name);
OutputStream out = new FileOutputStream(tmp);
out.write(1);
out.close();
return tmp;
}
}
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
https://jira.jboss.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira