Skip to content

Commit 1e0666d

Browse files
committed
IO-381 Add FileUtils.copyInputStreamToFile API with option to leave the source open.
See copyInputStreamToFile(final InputStream source, final File destination, boolean closeSource) git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1480300 13f79535-47bb-0310-9956-ffa450edef68
1 parent 1def234 commit 1e0666d

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

src/changes/changes.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ The <action> type attribute can be add,update,fix,remove.
4747
<body>
4848
<!-- The release date is the date RC is cut -->
4949
<release version="2.5" date="2013-??-??" description="New features and bug fixes.">
50-
<action issue="IO-380" dev="sebb" type="fix">
50+
<action issue="IO-381" dev="ggregory" type="add">
51+
Add FileUtils.copyInputStreamToFile API with option to leave the source open.
52+
See copyInputStreamToFile(final InputStream source, final File destination, boolean closeSource)
53+
</action>
54+
<action issue="IO-380" dev="sebb" type="fix" due-to="claudio_ch">
5155
FileUtils.copyInputStreamToFile should document it closes the input source
5256
</action>
5357
<action issue="IO-279" dev="sebb" type="fix">

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,8 +1459,7 @@ private static void doCopyDirectory(final File srcDir, final File destDir, final
14591459
* @throws IOException if an IO error occurs during copying
14601460
*/
14611461
public static void copyURLToFile(final URL source, final File destination) throws IOException {
1462-
final InputStream input = source.openStream();
1463-
copyInputStreamToFile(input, destination);
1462+
copyInputStreamToFile(source.openStream(), destination);
14641463
}
14651464

14661465
/**
@@ -1488,8 +1487,7 @@ public static void copyURLToFile(final URL source, final File destination,
14881487
final URLConnection connection = source.openConnection();
14891488
connection.setConnectTimeout(connectionTimeout);
14901489
connection.setReadTimeout(readTimeout);
1491-
final InputStream input = connection.getInputStream();
1492-
copyInputStreamToFile(input, destination);
1490+
copyInputStreamToFile(connection.getInputStream(), destination);
14931491
}
14941492

14951493
/**
@@ -1509,6 +1507,27 @@ public static void copyURLToFile(final URL source, final File destination,
15091507
* @since 2.0
15101508
*/
15111509
public static void copyInputStreamToFile(final InputStream source, final File destination) throws IOException {
1510+
copyInputStreamToFile(source, destination, true);
1511+
}
1512+
1513+
/**
1514+
* Copies bytes from an {@link InputStream} <code>source</code> to a file
1515+
* <code>destination</code>. The directories up to <code>destination</code>
1516+
* will be created if they don't already exist. <code>destination</code>
1517+
* will be overwritten if it already exists.
1518+
*
1519+
* @param source the <code>InputStream</code> to copy bytes from, must not be {@code null}, will be closed
1520+
* @param destination the non-directory <code>File</code> to write bytes to
1521+
* (possibly overwriting), must not be {@code null}
1522+
* @param closeSource If true, closes the <code>source</code>
1523+
* @throws IOException if <code>destination</code> is a directory
1524+
* @throws IOException if <code>destination</code> cannot be written
1525+
* @throws IOException if <code>destination</code> needs creating but can't be
1526+
* @throws IOException if an IO error occurs during copying
1527+
* @since 2.5
1528+
*/
1529+
public static void copyInputStreamToFile(final InputStream source, final File destination, boolean closeSource)
1530+
throws IOException {
15121531
try {
15131532
final FileOutputStream output = openOutputStream(destination);
15141533
try {
@@ -1518,7 +1537,9 @@ public static void copyInputStreamToFile(final InputStream source, final File de
15181537
IOUtils.closeQuietly(output);
15191538
}
15201539
} finally {
1521-
IOUtils.closeQuietly(source);
1540+
if (closeSource) {
1541+
IOUtils.closeQuietly(source);
1542+
}
15221543
}
15231544
}
15241545

0 commit comments

Comments
 (0)