From 45e409ac0a42716137994797cd2f9de21d1f96e9 Mon Sep 17 00:00:00 2001 From: Steven Post Date: Mon, 18 Mar 2013 11:37:58 +0100 Subject: [PATCH 01/16] Close filewriter on exception --- .../java/hudson/lifecycle/WindowsServiceLifecycle.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/hudson/lifecycle/WindowsServiceLifecycle.java b/core/src/main/java/hudson/lifecycle/WindowsServiceLifecycle.java index 6a8b4bec8d..0246c163e4 100644 --- a/core/src/main/java/hudson/lifecycle/WindowsServiceLifecycle.java +++ b/core/src/main/java/hudson/lifecycle/WindowsServiceLifecycle.java @@ -111,8 +111,12 @@ public class WindowsServiceLifecycle extends Lifecycle { File copyFiles = new File(rootDir,baseName+".copies"); FileWriter w = new FileWriter(copyFiles, true); - w.write(by.getAbsolutePath()+'>'+getHudsonWar().getAbsolutePath()+'\n'); - w.close(); + try { + w.write(by.getAbsolutePath()+'>'+getHudsonWar().getAbsolutePath()+'\n'); + } + finally { + w.close(); + } } @Override From 1966b554635b9c7dc85ff5d19e65cfc1600a565d Mon Sep 17 00:00:00 2001 From: Steven Post Date: Mon, 18 Mar 2013 11:39:05 +0100 Subject: [PATCH 02/16] Close streams on exception --- .../main/java/hudson/model/UpdateCenter.java | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/hudson/model/UpdateCenter.java b/core/src/main/java/hudson/model/UpdateCenter.java index bb5ad2ccf8..77198ca28a 100644 --- a/core/src/main/java/hudson/model/UpdateCenter.java +++ b/core/src/main/java/hudson/model/UpdateCenter.java @@ -758,16 +758,18 @@ public class UpdateCenter extends AbstractModelObject implements Saveable, OnMas * @see DownloadJob */ public File download(DownloadJob job, URL src) throws IOException { + CountingInputStream in = null; + OutputStream out = null; try { URLConnection con = connect(job,src); int total = con.getContentLength(); - CountingInputStream in = new CountingInputStream(con.getInputStream()); + in = new CountingInputStream(con.getInputStream()); byte[] buf = new byte[8192]; int len; File dst = job.getDestination(); File tmp = new File(dst.getPath()+".tmp"); - OutputStream out = new FileOutputStream(tmp); + out = new FileOutputStream(tmp); LOGGER.info("Downloading "+job.getName()); try { @@ -779,9 +781,6 @@ public class UpdateCenter extends AbstractModelObject implements Saveable, OnMas throw new IOException2("Failed to load "+src+" to "+tmp,e); } - in.close(); - out.close(); - if (total!=-1 && total!=tmp.length()) { // don't know exactly how this happens, but report like // http://www.ashlux.com/wordpress/2009/08/14/hudson-and-the-sonar-plugin-fail-maveninstallation-nosuchmethoderror/ @@ -793,6 +792,24 @@ public class UpdateCenter extends AbstractModelObject implements Saveable, OnMas } catch (IOException e) { throw new IOException2("Failed to download from "+src,e); } + finally { + if (in != null) { + try { + in.close(); + } + catch (IOException ioe) { + // swallow exception + } + } + if (out != null) { + try { + out.close(); + } + catch (IOException ioe) { + // swallow exception + } + } + } } /** From 24ac78c58d7a83f167891a4dfe1e0dd5a73b3926 Mon Sep 17 00:00:00 2001 From: Steven Post Date: Mon, 18 Mar 2013 11:43:06 +0100 Subject: [PATCH 03/16] Close FileInputStream on exception --- core/src/main/java/hudson/util/io/ZipArchiver.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/hudson/util/io/ZipArchiver.java b/core/src/main/java/hudson/util/io/ZipArchiver.java index e0c68cc38f..2e539ab880 100644 --- a/core/src/main/java/hudson/util/io/ZipArchiver.java +++ b/core/src/main/java/hudson/util/io/ZipArchiver.java @@ -70,10 +70,14 @@ final class ZipArchiver extends Archiver { fileZipEntry.setTime(f.lastModified()); zip.putNextEntry(fileZipEntry); FileInputStream in = new FileInputStream(f); - int len; - while((len=in.read(buf))>=0) - zip.write(buf,0,len); - in.close(); + try { + int len; + while((len=in.read(buf))>0) + zip.write(buf,0,len); + } + finally { + in.close(); + } zip.closeEntry(); } entriesWritten++; From 4b023ebc54420d374cfd921cd27d20226d32235a Mon Sep 17 00:00:00 2001 From: Steven Post Date: Mon, 18 Mar 2013 15:16:44 +0100 Subject: [PATCH 04/16] Close stream on exception --- .../main/java/hudson/console/ConsoleNote.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/hudson/console/ConsoleNote.java b/core/src/main/java/hudson/console/ConsoleNote.java index c8ea004643..d36df3c811 100644 --- a/core/src/main/java/hudson/console/ConsoleNote.java +++ b/core/src/main/java/hudson/console/ConsoleNote.java @@ -172,16 +172,24 @@ public abstract class ConsoleNote implements Serializable, Describable Date: Mon, 18 Mar 2013 15:22:15 +0100 Subject: [PATCH 05/16] Close stream on exception --- core/src/main/java/hudson/Main.java | 49 ++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/hudson/Main.java b/core/src/main/java/hudson/Main.java index 45067f932b..a3dc3b00cb 100644 --- a/core/src/main/java/hudson/Main.java +++ b/core/src/main/java/hudson/Main.java @@ -138,23 +138,33 @@ public class Main { FileOutputStream os = new FileOutputStream(tmpFile); Writer w = new OutputStreamWriter(os,"UTF-8"); - w.write(""); - w.write(""); - w.flush(); + int ret; + try { + w.write(""); + w.write(""); + w.flush(); - // run the command - long start = System.currentTimeMillis(); + // run the command + long start = System.currentTimeMillis(); - List cmd = new ArrayList(); - for( int i=1; i cmd = new ArrayList(); + for( int i=1; i"+ret+""+(System.currentTimeMillis()-start)+""); - w.close(); + w.write(""+ret+""+(System.currentTimeMillis()-start)+""); + } + finally { + try { + w.close(); + } + catch (IOException ioe) { + // swallow exception + } + } String location = home+"job/"+projectNameEnc+"/postBuildResult"; while(true) { @@ -171,8 +181,17 @@ public class Main { con.connect(); // send the data FileInputStream in = new FileInputStream(tmpFile); - Util.copyStream(in,con.getOutputStream()); - in.close(); + try { + Util.copyStream(in,con.getOutputStream()); + } + finally { + try { + in.close(); + } + catch (IOException ioe) { + // swallow exception + } + } if(con.getResponseCode()!=200) { Util.copyStream(con.getErrorStream(),System.err); From 5743b6f672c01cf273485e013fe650f39d34fbec Mon Sep 17 00:00:00 2001 From: Steven Post Date: Mon, 18 Mar 2013 15:23:45 +0100 Subject: [PATCH 06/16] Close stream on exception --- core/src/main/java/hudson/model/Run.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/hudson/model/Run.java b/core/src/main/java/hudson/model/Run.java index addc19fa9c..fba3e653a8 100644 --- a/core/src/main/java/hudson/model/Run.java +++ b/core/src/main/java/hudson/model/Run.java @@ -2052,7 +2052,14 @@ public abstract class Run ,RunT extends Run Date: Mon, 18 Mar 2013 15:25:49 +0100 Subject: [PATCH 07/16] Close stream on exception --- .../src/main/java/hudson/model/UsageStatistics.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/hudson/model/UsageStatistics.java b/core/src/main/java/hudson/model/UsageStatistics.java index f350b06ee9..0ab5e68a66 100644 --- a/core/src/main/java/hudson/model/UsageStatistics.java +++ b/core/src/main/java/hudson/model/UsageStatistics.java @@ -172,8 +172,17 @@ public class UsageStatistics extends PageDecorator { // json -> UTF-8 encode -> gzip -> encrypt -> base64 -> string OutputStreamWriter w = new OutputStreamWriter(new GZIPOutputStream(new CombinedCipherOutputStream(baos,getKey(),"AES")), "UTF-8"); - o.write(w); - w.close(); + try { + o.write(w); + } + finally { + try { + w.close(); + } + catch (IOException ioe) { + // swallow exception + } + } return new String(Base64.encode(baos.toByteArray())); } catch (GeneralSecurityException e) { From 8d0811666ba6364a08cbad7ce562d764c680b737 Mon Sep 17 00:00:00 2001 From: Steven Post Date: Mon, 18 Mar 2013 15:30:17 +0100 Subject: [PATCH 08/16] Close stream on exception --- core/src/main/java/hudson/scm/SCM.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/hudson/scm/SCM.java b/core/src/main/java/hudson/scm/SCM.java index 2b243d3ca5..79fbf234a6 100644 --- a/core/src/main/java/hudson/scm/SCM.java +++ b/core/src/main/java/hudson/scm/SCM.java @@ -577,8 +577,9 @@ public abstract class SCM implements Describable, ExtensionPoint { // protected final boolean createEmptyChangeLog(File changelogFile, BuildListener listener, String rootTag) { + FileWriter w = null; try { - FileWriter w = new FileWriter(changelogFile); + w = new FileWriter(changelogFile); w.write("<"+rootTag +"/>"); w.close(); return true; @@ -586,6 +587,15 @@ public abstract class SCM implements Describable, ExtensionPoint { e.printStackTrace(listener.error(e.getMessage())); return false; } + finally { + if (w != null) { + try { + w.close(); + } catch (IOException e) { + // swallow exception + } + } + } } protected final String nullify(String s) { From 9696e0eda8e2c7b63bcacc45fbb06ac257b64499 Mon Sep 17 00:00:00 2001 From: Steven Post Date: Mon, 18 Mar 2013 15:40:13 +0100 Subject: [PATCH 09/16] JDKInstaller: close stream on exception --- core/src/main/java/hudson/tools/JDKInstaller.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/hudson/tools/JDKInstaller.java b/core/src/main/java/hudson/tools/JDKInstaller.java index 66a4f10fd6..71ace14f35 100644 --- a/core/src/main/java/hudson/tools/JDKInstaller.java +++ b/core/src/main/java/hudson/tools/JDKInstaller.java @@ -170,8 +170,17 @@ public class JDKInstaller extends ToolInstaller { byte[] header = new byte[2]; { DataInputStream in = new DataInputStream(fs.read(jdkBundle)); - in.readFully(header); - in.close(); + try { + in.readFully(header); + } + finally { + try { + in.close(); + } + catch (IOException ioe) { + // swallow exception + } + } } ProcStarter starter; From dfb0935a0ca811a7ae5066117a42b70edbf16c1d Mon Sep 17 00:00:00 2001 From: Steven Post Date: Mon, 18 Mar 2013 15:43:58 +0100 Subject: [PATCH 10/16] Close streams on exception --- .../main/java/hudson/triggers/SCMTrigger.java | 13 +++++++++++-- .../main/java/hudson/util/CompressedFile.java | 19 ++++++++++++++----- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/hudson/triggers/SCMTrigger.java b/core/src/main/java/hudson/triggers/SCMTrigger.java index 2f93968bf0..df8c286e62 100644 --- a/core/src/main/java/hudson/triggers/SCMTrigger.java +++ b/core/src/main/java/hudson/triggers/SCMTrigger.java @@ -322,8 +322,17 @@ public class SCMTrigger extends Trigger { rsp.setContentType("text/plain;charset=UTF-8"); // Prevent jelly from flushing stream so Content-Length header can be added afterwards FlushProofOutputStream out = new FlushProofOutputStream(rsp.getCompressedOutputStream(req)); - getPollingLogText().writeLogTo(0, out); - out.close(); + try { + getPollingLogText().writeLogTo(0, out); + } + finally { + try { + out.close(); + } + catch (IOException ioe) { + // swallow exception + } + } } public AnnotatedLargeText getPollingLogText() { diff --git a/core/src/main/java/hudson/util/CompressedFile.java b/core/src/main/java/hudson/util/CompressedFile.java index 93c79a4d8c..8938f3276e 100644 --- a/core/src/main/java/hudson/util/CompressedFile.java +++ b/core/src/main/java/hudson/util/CompressedFile.java @@ -115,11 +115,20 @@ public class CompressedFile { StringBuilder str = new StringBuilder((int)sizeGuess); Reader r = new InputStreamReader(read()); - char[] buf = new char[8192]; - int len; - while((len=r.read(buf,0,buf.length))>0) - str.append(buf,0,len); - r.close(); + try { + char[] buf = new char[8192]; + int len; + while((len=r.read(buf,0,buf.length))>0) + str.append(buf,0,len); + } + finally { + try { + r.close(); + } + catch (IOException ioe) { + // swallow exception + } + } return str.toString(); } From db9bc18143a2f8bbdf5fe53bcc91425e227339b8 Mon Sep 17 00:00:00 2001 From: Steven Post Date: Mon, 18 Mar 2013 16:02:09 +0100 Subject: [PATCH 11/16] Close stream The stream was not properly closed. In addition, when the abort would fail, the exception caused by the abort would be propagated up the stack instead of the original exception. This patch makes sure to close the stream and let the original exception propagate. --- core/src/main/java/hudson/util/TextFile.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/hudson/util/TextFile.java b/core/src/main/java/hudson/util/TextFile.java index f010f12e1a..2d804a7ebe 100644 --- a/core/src/main/java/hudson/util/TextFile.java +++ b/core/src/main/java/hudson/util/TextFile.java @@ -86,7 +86,18 @@ public class TextFile { w.write(text); w.commit(); } finally { - w.abort(); + try { + w.abort(); + } + catch (IOException ioe) { + // swallow exception + } + try { + w.close(); + } + catch (IOException ioe) { + // swallow exception + } } } From 170bb7bf6e5b7b42c8c506898eb7f48c5e3e24d0 Mon Sep 17 00:00:00 2001 From: Steven Post Date: Mon, 18 Mar 2013 16:07:31 +0100 Subject: [PATCH 12/16] Close stream --- core/src/main/java/jenkins/model/Jenkins.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/jenkins/model/Jenkins.java b/core/src/main/java/jenkins/model/Jenkins.java index e689859728..1a16d0b35b 100755 --- a/core/src/main/java/jenkins/model/Jenkins.java +++ b/core/src/main/java/jenkins/model/Jenkins.java @@ -3921,13 +3921,24 @@ public class Jenkins extends AbstractCIBase implements ModifiableTopLevelItemGro private static void computeVersion(ServletContext context) { // set the version Properties props = new Properties(); + InputStream is = null; try { - InputStream is = Jenkins.class.getResourceAsStream("jenkins-version.properties"); + is = Jenkins.class.getResourceAsStream("jenkins-version.properties"); if(is!=null) props.load(is); } catch (IOException e) { e.printStackTrace(); // if the version properties is missing, that's OK. } + finally { + if (is != null) { + try { + is.close(); + } + catch (IOException ioe) { + // swallow exception + } + } + } String ver = props.getProperty("version"); if(ver==null) ver="?"; VERSION = ver; From 7e45780aa43fd8e019682fd55b1cadb77f66606e Mon Sep 17 00:00:00 2001 From: Steven Post Date: Mon, 18 Mar 2013 16:09:03 +0100 Subject: [PATCH 13/16] Close stream --- .../main/java/jenkins/model/PeepholePermalink.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/jenkins/model/PeepholePermalink.java b/core/src/main/java/jenkins/model/PeepholePermalink.java index b4667d32a9..d36ab3f86f 100644 --- a/core/src/main/java/jenkins/model/PeepholePermalink.java +++ b/core/src/main/java/jenkins/model/PeepholePermalink.java @@ -178,7 +178,18 @@ public abstract class PeepholePermalink extends Permalink implements Predicate Date: Mon, 18 Mar 2013 16:12:15 +0100 Subject: [PATCH 14/16] Close stream --- .../java/hudson/model/FileParameterValue.java | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/hudson/model/FileParameterValue.java b/core/src/main/java/hudson/model/FileParameterValue.java index e5bfda9881..00e35a44fa 100644 --- a/core/src/main/java/hudson/model/FileParameterValue.java +++ b/core/src/main/java/hudson/model/FileParameterValue.java @@ -197,12 +197,22 @@ public class FileParameterValue extends ParameterValue { File fileParameter = getLocationUnderBuild(build); if (fileParameter.isFile()) { InputStream data = new FileInputStream(fileParameter); - long lastModified = fileParameter.lastModified(); - long contentLength = fileParameter.length(); - if (request.hasParameter("view")) { - response.serveFile(request, data, lastModified, contentLength, "plain.txt"); - } else { - response.serveFile(request, data, lastModified, contentLength, originalFileName); + try { + long lastModified = fileParameter.lastModified(); + long contentLength = fileParameter.length(); + if (request.hasParameter("view")) { + response.serveFile(request, data, lastModified, contentLength, "plain.txt"); + } else { + response.serveFile(request, data, lastModified, contentLength, originalFileName); + } + } + finally { + try { + data.close(); + } + catch (IOException ioe) { + // swallow exception + } } } } From 9600a0aa0b1fe0e2bed90234f324bf03797f9c8c Mon Sep 17 00:00:00 2001 From: Steven Post Date: Mon, 18 Mar 2013 16:38:46 +0100 Subject: [PATCH 15/16] Test: avoid NPE when zipFile could not be created --- .../java/hudson/util/io/ZipArchiverTest.java | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/core/src/test/java/hudson/util/io/ZipArchiverTest.java b/core/src/test/java/hudson/util/io/ZipArchiverTest.java index d30adf9a23..ddea8690d8 100644 --- a/core/src/test/java/hudson/util/io/ZipArchiverTest.java +++ b/core/src/test/java/hudson/util/io/ZipArchiverTest.java @@ -65,13 +65,13 @@ public class ZipArchiverTest extends TestCase { } catch (Exception e) { fail("exception driving ZipArchiver", e); } finally { - try { - archiver.close(); - } catch (IOException e) { - // ignored + if (archiver != null) { + try { + archiver.close(); + } catch (IOException e) { + // ignored + } } - - archiver = null; } // examine zip contents and assert that none of the entry names (paths) have @@ -86,10 +86,12 @@ public class ZipArchiverTest extends TestCase { } catch (Exception e) { fail("failure enumerating zip entries", e); } finally { - try { - zipFileVerify.close(); - } catch (IOException e) { - // ignored + if (zipFileVerify != null) { + try { + zipFileVerify.close(); + } catch (IOException e) { + // ignored + } } } From d1fb126b48f7668eedfe6665ebaa1630f4cedf97 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Sat, 19 Apr 2014 16:40:10 -0700 Subject: [PATCH 16/16] Massaging the fix by stevenpost - coding style was not matching the rest of Jenkins - IOUtils.closeQuietly is a lot more concise way of achieving the same thing. - AtomicFileWriter.abort() does close, so no need to close it one more time. --- core/src/main/java/hudson/Main.java | 20 ++++------------- .../main/java/hudson/console/ConsoleNote.java | 6 ++--- .../lifecycle/WindowsServiceLifecycle.java | 3 +-- .../java/hudson/model/FileParameterValue.java | 10 ++------- core/src/main/java/hudson/model/Run.java | 10 ++------- .../main/java/hudson/model/UpdateCenter.java | 22 ++++--------------- .../java/hudson/model/UsageStatistics.java | 11 +++------- core/src/main/java/hudson/scm/SCM.java | 12 +++------- .../main/java/hudson/tools/JDKInstaller.java | 10 ++------- .../main/java/hudson/triggers/SCMTrigger.java | 11 +++------- .../main/java/hudson/util/CompressedFile.java | 10 ++------- core/src/main/java/hudson/util/TextFile.java | 13 +---------- .../main/java/hudson/util/io/ZipArchiver.java | 5 ++--- core/src/main/java/jenkins/model/Jenkins.java | 13 +++-------- .../java/jenkins/model/PeepholePermalink.java | 13 +---------- 15 files changed, 35 insertions(+), 134 deletions(-) diff --git a/core/src/main/java/hudson/Main.java b/core/src/main/java/hudson/Main.java index a3dc3b00cb..3e34cd1427 100644 --- a/core/src/main/java/hudson/Main.java +++ b/core/src/main/java/hudson/Main.java @@ -156,14 +156,8 @@ public class Main { ret = proc.join(); w.write(""+ret+""+(System.currentTimeMillis()-start)+""); - } - finally { - try { - w.close(); - } - catch (IOException ioe) { - // swallow exception - } + } finally { + IOUtils.closeQuietly(w); } String location = home+"job/"+projectNameEnc+"/postBuildResult"; @@ -183,14 +177,8 @@ public class Main { FileInputStream in = new FileInputStream(tmpFile); try { Util.copyStream(in,con.getOutputStream()); - } - finally { - try { - in.close(); - } - catch (IOException ioe) { - // swallow exception - } + } finally { + IOUtils.closeQuietly(in); } if(con.getResponseCode()!=200) { diff --git a/core/src/main/java/hudson/console/ConsoleNote.java b/core/src/main/java/hudson/console/ConsoleNote.java index d36df3c811..639b2106f7 100644 --- a/core/src/main/java/hudson/console/ConsoleNote.java +++ b/core/src/main/java/hudson/console/ConsoleNote.java @@ -174,8 +174,7 @@ public abstract class ConsoleNote implements Serializable, Describable implements Serializable, Describable'+getHudsonWar().getAbsolutePath()+'\n'); - } - finally { + } finally { w.close(); } } diff --git a/core/src/main/java/hudson/model/FileParameterValue.java b/core/src/main/java/hudson/model/FileParameterValue.java index 00e35a44fa..8544042ce4 100644 --- a/core/src/main/java/hudson/model/FileParameterValue.java +++ b/core/src/main/java/hudson/model/FileParameterValue.java @@ -205,14 +205,8 @@ public class FileParameterValue extends ParameterValue { } else { response.serveFile(request, data, lastModified, contentLength, originalFileName); } - } - finally { - try { - data.close(); - } - catch (IOException ioe) { - // swallow exception - } + } finally { + IOUtils.closeQuietly(data); } } } diff --git a/core/src/main/java/hudson/model/Run.java b/core/src/main/java/hudson/model/Run.java index fba3e653a8..19fa980108 100644 --- a/core/src/main/java/hudson/model/Run.java +++ b/core/src/main/java/hudson/model/Run.java @@ -2051,14 +2051,8 @@ public abstract class Run ,RunT extends Run, ExtensionPoint { } catch (IOException e) { e.printStackTrace(listener.error(e.getMessage())); return false; - } - finally { - if (w != null) { - try { - w.close(); - } catch (IOException e) { - // swallow exception - } - } + } finally { + IOUtils.closeQuietly(w); } } diff --git a/core/src/main/java/hudson/tools/JDKInstaller.java b/core/src/main/java/hudson/tools/JDKInstaller.java index 71ace14f35..40abe42ee7 100644 --- a/core/src/main/java/hudson/tools/JDKInstaller.java +++ b/core/src/main/java/hudson/tools/JDKInstaller.java @@ -172,14 +172,8 @@ public class JDKInstaller extends ToolInstaller { DataInputStream in = new DataInputStream(fs.read(jdkBundle)); try { in.readFully(header); - } - finally { - try { - in.close(); - } - catch (IOException ioe) { - // swallow exception - } + } finally { + IOUtils.closeQuietly(in); } } diff --git a/core/src/main/java/hudson/triggers/SCMTrigger.java b/core/src/main/java/hudson/triggers/SCMTrigger.java index df8c286e62..0f140bf91b 100644 --- a/core/src/main/java/hudson/triggers/SCMTrigger.java +++ b/core/src/main/java/hudson/triggers/SCMTrigger.java @@ -31,6 +31,7 @@ import hudson.model.AbstractBuild; import hudson.model.AbstractProject; import hudson.model.Action; import hudson.model.Cause; +import hudson.util.IOUtils; import jenkins.model.Jenkins; import hudson.model.Item; import hudson.model.Project; @@ -324,14 +325,8 @@ public class SCMTrigger extends Trigger { FlushProofOutputStream out = new FlushProofOutputStream(rsp.getCompressedOutputStream(req)); try { getPollingLogText().writeLogTo(0, out); - } - finally { - try { - out.close(); - } - catch (IOException ioe) { - // swallow exception - } + } finally { + IOUtils.closeQuietly(out); } } diff --git a/core/src/main/java/hudson/util/CompressedFile.java b/core/src/main/java/hudson/util/CompressedFile.java index 8938f3276e..f3a9133588 100644 --- a/core/src/main/java/hudson/util/CompressedFile.java +++ b/core/src/main/java/hudson/util/CompressedFile.java @@ -120,14 +120,8 @@ public class CompressedFile { int len; while((len=r.read(buf,0,buf.length))>0) str.append(buf,0,len); - } - finally { - try { - r.close(); - } - catch (IOException ioe) { - // swallow exception - } + } finally { + IOUtils.closeQuietly(r); } return str.toString(); diff --git a/core/src/main/java/hudson/util/TextFile.java b/core/src/main/java/hudson/util/TextFile.java index 2d804a7ebe..f010f12e1a 100644 --- a/core/src/main/java/hudson/util/TextFile.java +++ b/core/src/main/java/hudson/util/TextFile.java @@ -86,18 +86,7 @@ public class TextFile { w.write(text); w.commit(); } finally { - try { - w.abort(); - } - catch (IOException ioe) { - // swallow exception - } - try { - w.close(); - } - catch (IOException ioe) { - // swallow exception - } + w.abort(); } } diff --git a/core/src/main/java/hudson/util/io/ZipArchiver.java b/core/src/main/java/hudson/util/io/ZipArchiver.java index 2e539ab880..ef0ae49658 100644 --- a/core/src/main/java/hudson/util/io/ZipArchiver.java +++ b/core/src/main/java/hudson/util/io/ZipArchiver.java @@ -72,10 +72,9 @@ final class ZipArchiver extends Archiver { FileInputStream in = new FileInputStream(f); try { int len; - while((len=in.read(buf))>0) + while((len=in.read(buf))>=0) zip.write(buf,0,len); - } - finally { + } finally { in.close(); } zip.closeEntry(); diff --git a/core/src/main/java/jenkins/model/Jenkins.java b/core/src/main/java/jenkins/model/Jenkins.java index 1a16d0b35b..dae24c69df 100755 --- a/core/src/main/java/jenkins/model/Jenkins.java +++ b/core/src/main/java/jenkins/model/Jenkins.java @@ -176,6 +176,7 @@ import hudson.util.FormValidation; import hudson.util.Futures; import hudson.util.HudsonIsLoading; import hudson.util.HudsonIsRestarting; +import hudson.util.IOUtils; import hudson.util.Iterators; import hudson.util.JenkinsReloadFailed; import hudson.util.Memoizer; @@ -3928,16 +3929,8 @@ public class Jenkins extends AbstractCIBase implements ModifiableTopLevelItemGro props.load(is); } catch (IOException e) { e.printStackTrace(); // if the version properties is missing, that's OK. - } - finally { - if (is != null) { - try { - is.close(); - } - catch (IOException ioe) { - // swallow exception - } - } + } finally { + IOUtils.closeQuietly(is); } String ver = props.getProperty("version"); if(ver==null) ver="?"; diff --git a/core/src/main/java/jenkins/model/PeepholePermalink.java b/core/src/main/java/jenkins/model/PeepholePermalink.java index d36ab3f86f..b4667d32a9 100644 --- a/core/src/main/java/jenkins/model/PeepholePermalink.java +++ b/core/src/main/java/jenkins/model/PeepholePermalink.java @@ -178,18 +178,7 @@ public abstract class PeepholePermalink extends Permalink implements Predicate