Skip to content

Commit 227c613

Browse files
committed
[IO-664] org.apache.commons.io.FileUtils.copyURLToFile(*) open but do
not close streams.
1 parent 5004792 commit 227c613

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ The <action> type attribute can be add,update,fix,remove.
204204
<action issue="IO-662" dev="ggregory" type="add" due-to="Adam Retter, Gary Gregory">
205205
Refactor ByteArrayOutputStream into synchronized and unsynchronized versions #108.
206206
</action>
207+
<action issue="IO-664" dev="ggregory" type="fix" due-to="Gary Gregory">
208+
org.apache.commons.io.FileUtils.copyURLToFile(*) open but do not close streams.
209+
</action>
207210
</release>
208211

209212
<release version="2.6" date="2017-10-15" description="Java 7 required, Java 9 supported.">

src/main/java/org/apache/commons/io/FileUtils.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,9 @@ public static void copyToFile(final InputStream source, final File destination)
10221022
* @throws IOException if an IO error occurs during copying
10231023
*/
10241024
public static void copyURLToFile(final URL source, final File destination) throws IOException {
1025-
copyInputStreamToFile(source.openStream(), destination);
1025+
try (final InputStream stream = source.openStream()) {
1026+
copyInputStreamToFile(stream, destination);
1027+
}
10261028
}
10271029

10281030
/**
@@ -1046,11 +1048,13 @@ public static void copyURLToFile(final URL source, final File destination) throw
10461048
* @since 2.0
10471049
*/
10481050
public static void copyURLToFile(final URL source, final File destination,
1049-
final int connectionTimeout, final int readTimeout) throws IOException {
1051+
final int connectionTimeout, final int readTimeout) throws IOException {
10501052
final URLConnection connection = source.openConnection();
10511053
connection.setConnectTimeout(connectionTimeout);
10521054
connection.setReadTimeout(readTimeout);
1053-
copyInputStreamToFile(connection.getInputStream(), destination);
1055+
try (final InputStream stream = connection.getInputStream()) {
1056+
copyInputStreamToFile(stream, destination);
1057+
}
10541058
}
10551059

10561060
/**

0 commit comments

Comments
 (0)