From df6e16ab5f83605b7bb379d04af8eb4be0e2a858 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Mon, 29 Apr 2002 11:19:35 +0000 Subject: [PATCH] Make sure dest file is only created if URL can be opened PR: 8575 and make sure the output file gets closed in case of an error, while we are at it. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@272588 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/tools/ant/taskdefs/Get.java | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/Get.java b/src/main/org/apache/tools/ant/taskdefs/Get.java index 1fad15a4f..f78a09ce7 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Get.java +++ b/src/main/org/apache/tools/ant/taskdefs/Get.java @@ -189,8 +189,6 @@ public class Get extends Task { //newer. Some protocols (FTP) dont include dates, of //course. - FileOutputStream fos = new FileOutputStream(dest); - InputStream is = null; for (int i = 0; i < 3 ; i++) { try { @@ -209,20 +207,26 @@ public class Get extends Task { location); } - byte[] buffer = new byte[100 * 1024]; - int length; - - while ((length = is.read(buffer)) >= 0) { - fos.write(buffer, 0, length); + FileOutputStream fos = new FileOutputStream(dest); + try { + byte[] buffer = new byte[100 * 1024]; + int length; + + while ((length = is.read(buffer)) >= 0) { + fos.write(buffer, 0, length); + if (verbose) { + System.out.print("."); + } + } if (verbose) { - System.out.print("."); + System.out.println(); } + } finally { + if (fos != null) { + fos.close(); + } + is.close(); } - if (verbose) { - System.out.println(); - } - fos.close(); - is.close(); //if (and only if) the use file time option is set, then //the saved file now has its timestamp set to that of the