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