diff --git a/core/src/main/java/hudson/Main.java b/core/src/main/java/hudson/Main.java index 6a176b2397..cf8a168ba1 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(); + 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 { + 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 e4402c1330..c27fec6364 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 08d2b7b11a..915d2e3122 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 b3c7b3f428..7d64012867 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 573c34eda8..6975b23e3c 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 fe0449280e..af34178ebf 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 77e91698e4..8ff7e5d4a4 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 231ef36857..b6ccf22c30 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 e0c68cc38f..ef0ae49658 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 cdb21b8809..06398749da 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 650f1c9723..41567f5850 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 + } } }