FileUtils.copyURLToFile(URL, File, int, int) leaks its URLConnection.
Called by FileUtils.copyURLToFile(URL, File).
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -110,6 +110,10 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="IO-729" dev="ggregory" type="fix" due-to="Rob Spoor, Gary Gregory">
Prevent IllegalArgumentExceptions in BrokenInputStream/Reader/OutputStream/Writer #278.
</action>
+ <action dev="ggregory" type="fix" due-to="Gary Gregory">
+ FileUtils.copyURLToFile(URL, File, int, int) leaks its URLConnection.
+ Called by FileUtils.copyURLToFile(URL, File).
+ </action>
<!-- ADD -->
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add BrokenReader.INSTANCE.

MODIFY
src/main/java/org/apache/commons/io/FileUtils.java
src/main/java/org/apache/commons/io/FileUtils.java
@@ -1109,16 +1109,19 @@ public static void copyURLToFile(final URL source, final File destination) throw
* @throws IOException if an IO error occurs during copying
* @since 2.0
*/
- public static void copyURLToFile(final URL source, final File destination,
- final int connectionTimeoutMillis, final int readTimeoutMillis) throws IOException {
- final URLConnection connection = source.openConnection();
- connection.setConnectTimeout(connectionTimeoutMillis);
- connection.setReadTimeout(readTimeoutMillis);
- try (final InputStream stream = connection.getInputStream()) {
+ public static void copyURLToFile(final URL source, final File destination, final int connectionTimeoutMillis, final int readTimeoutMillis)
+ throws IOException {
+ final URLConnection urlConnection = source.openConnection();
+ try {
+ urlConnection.setConnectTimeout(connectionTimeoutMillis);
+ urlConnection.setReadTimeout(readTimeoutMillis);
+ try (final InputStream stream = urlConnection.getInputStream()) {
copyInputStreamToFile(stream, destination);
}
+ } finally {
+ IOUtils.close(urlConnection);
+ }
}
-
/**
* Creates all parent directories for a File object.

FileUtils.copyURLToFile(URL, File, int, int) leaks its URLConnection.

Called by FileUtils.copyURLToFile(URL, File).