diff --git a/core/src/main/java/hudson/Main.java b/core/src/main/java/hudson/Main.java index 6a176b23976a3b68e820043e44bc85fd6dc9790b..cf8a168ba115549ba3e6be45cbdf70423198311d 100644 --- a/core/src/main/java/hudson/Main.java +++ b/core/src/main/java/hudson/Main.java @@ -137,23 +137,27 @@ public class Main { FileOutputStream os = new FileOutputStream(tmpFile); Writer w = new OutputStreamWriter(os,"UTF-8"); - w.write(""); - w.write(""); - w.flush(); - - // run the command - long start = System.currentTimeMillis(); - - List cmd = new ArrayList(); - for( int i=1; i"+ret+""+(System.currentTimeMillis()-start)+""); - w.close(); + int ret; + try { + w.write(""); + w.write(""); + w.flush(); + + // run the command + long start = System.currentTimeMillis(); + + List cmd = new ArrayList(); + for( int i=1; i"+ret+""+(System.currentTimeMillis()-start)+""); + } finally { + IOUtils.closeQuietly(w); + } URL location = new URL(jobURL, "postBuildResult"); while(true) { @@ -170,8 +174,11 @@ 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 { + IOUtils.closeQuietly(in); + } if(con.getResponseCode()!=200) { Util.copyStream(con.getErrorStream(),System.err); diff --git a/core/src/main/java/hudson/console/ConsoleNote.java b/core/src/main/java/hudson/console/ConsoleNote.java index e4402c13308e848575d09169f1ef3fa50b1be3de..c27fec63643527d8b9f775f6e1c6b6a739a47f62 100644 --- a/core/src/main/java/hudson/console/ConsoleNote.java +++ b/core/src/main/java/hudson/console/ConsoleNote.java @@ -171,16 +171,22 @@ public abstract class ConsoleNote implements Serializable, Describable'+getHudsonWar().getAbsolutePath()+'\n'); - w.close(); + try { + w.write(by.getAbsolutePath()+'>'+getHudsonWar().getAbsolutePath()+'\n'); + } finally { + w.close(); + } } @Override diff --git a/core/src/main/java/hudson/model/FileParameterValue.java b/core/src/main/java/hudson/model/FileParameterValue.java index 08d2b7b11a644577a92ee11a617bfdac00dd5b64..915d2e3122f4a8341e46066e719274419f1e838e 100644 --- a/core/src/main/java/hudson/model/FileParameterValue.java +++ b/core/src/main/java/hudson/model/FileParameterValue.java @@ -197,12 +197,16 @@ 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 { + IOUtils.closeQuietly(data); } } } diff --git a/core/src/main/java/hudson/model/Run.java b/core/src/main/java/hudson/model/Run.java index b3c7b3f428cbfd17cba5da2429305863bc014917..7d640128677b80a965c0eab6ee7272fc5955d738 100644 --- a/core/src/main/java/hudson/model/Run.java +++ b/core/src/main/java/hudson/model/Run.java @@ -2082,8 +2082,9 @@ public abstract class Run ,RunT extends Run 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 { + IOUtils.closeQuietly(w); + } return new String(Base64.encode(baos.toByteArray())); } catch (GeneralSecurityException e) { diff --git a/core/src/main/java/hudson/scm/SCM.java b/core/src/main/java/hudson/scm/SCM.java index 573c34eda8d3439536bafa032c23694cdadc5370..6975b23e3ce9fadeb554bc0e343ff051e9918ae1 100644 --- a/core/src/main/java/hudson/scm/SCM.java +++ b/core/src/main/java/hudson/scm/SCM.java @@ -41,6 +41,7 @@ import hudson.model.Describable; import hudson.model.TaskListener; import hudson.model.Node; import hudson.model.WorkspaceCleanupThread; +import hudson.util.IOUtils; import jenkins.model.Jenkins; import hudson.model.Descriptor; import hudson.model.Api; @@ -578,14 +579,17 @@ 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; } catch (IOException e) { e.printStackTrace(listener.error(e.getMessage())); return false; + } 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 fe0449280e06092dc152842ae5600fbbe9399cbf..af34178ebfbe082a460d7834bc9e2bcd2a1ab57f 100644 --- a/core/src/main/java/hudson/tools/JDKInstaller.java +++ b/core/src/main/java/hudson/tools/JDKInstaller.java @@ -178,8 +178,11 @@ 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 { + IOUtils.closeQuietly(in); + } } ProcStarter starter; diff --git a/core/src/main/java/hudson/triggers/SCMTrigger.java b/core/src/main/java/hudson/triggers/SCMTrigger.java index 77e91698e459328e6de98c12c1f6269e8f84fd81..8ff7e5d4a4685901010ee6a327610127e4a8940d 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; @@ -337,8 +338,11 @@ 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 { + IOUtils.closeQuietly(out); + } } public AnnotatedLargeText getPollingLogText() { diff --git a/core/src/main/java/hudson/util/CompressedFile.java b/core/src/main/java/hudson/util/CompressedFile.java index 231ef36857ae6b3466a1da6d4941929eecbd6361..b6ccf22c3071f022282175548922904de589b4fd 100644 --- a/core/src/main/java/hudson/util/CompressedFile.java +++ b/core/src/main/java/hudson/util/CompressedFile.java @@ -115,11 +115,14 @@ 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 { + IOUtils.closeQuietly(r); + } return str.toString(); } diff --git a/core/src/main/java/hudson/util/io/ZipArchiver.java b/core/src/main/java/hudson/util/io/ZipArchiver.java index e0c68cc38f65bc325c615d9daf7fe2ab44d3b42d..ef0ae49658231344f1b66a978df611071636d5a6 100644 --- a/core/src/main/java/hudson/util/io/ZipArchiver.java +++ b/core/src/main/java/hudson/util/io/ZipArchiver.java @@ -70,10 +70,13 @@ 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++; diff --git a/core/src/main/java/jenkins/model/Jenkins.java b/core/src/main/java/jenkins/model/Jenkins.java index cdb21b880990619f539bb94e971d9966a400af62..06398749dac95d5f49204b467afd9d7421e8323b 100644 --- a/core/src/main/java/jenkins/model/Jenkins.java +++ b/core/src/main/java/jenkins/model/Jenkins.java @@ -172,6 +172,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; @@ -3981,12 +3982,15 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve 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 { + IOUtils.closeQuietly(is); } String ver = props.getProperty("version"); if(ver==null) ver="?"; diff --git a/core/src/test/java/hudson/util/io/ZipArchiverTest.java b/core/src/test/java/hudson/util/io/ZipArchiverTest.java index 650f1c972301c5e490c7b4b054d860052ac8a3b1..41567f5850bcbd781a59b542c7fa9bdc0ef27062 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 + } } }