diff --git a/core/src/main/java/hudson/FilePath.java b/core/src/main/java/hudson/FilePath.java index bb82ffc39c..34794a5b98 100644 --- a/core/src/main/java/hudson/FilePath.java +++ b/core/src/main/java/hudson/FilePath.java @@ -40,6 +40,7 @@ import hudson.remoting.RemoteInputStream; import hudson.util.IOException2; import hudson.util.HeadBufferingStream; import hudson.util.FormValidation; +import hudson.util.IOUtils; import static hudson.util.jna.GNUCLibrary.LIBC; import static hudson.Util.fixEmpty; import static hudson.FilePath.TarCompression.GZIP; @@ -54,7 +55,6 @@ import org.apache.tools.ant.types.FileSet; import org.apache.tools.tar.TarEntry; import org.apache.tools.zip.ZipOutputStream; import org.apache.tools.zip.ZipEntry; -import org.apache.commons.io.IOUtils; import org.apache.commons.io.input.CountingInputStream; import org.apache.commons.fileupload.FileItem; import org.kohsuke.stapler.Stapler; @@ -405,12 +405,7 @@ public final class FilePath implements Serializable { } else { File p = f.getParentFile(); if(p!=null) p.mkdirs(); - FileOutputStream out = new FileOutputStream(f); - try { - IOUtils.copy(zip, out); - } finally { - out.close(); - } + IOUtils.copy(zip, f); f.setLastModified(e.getTime()); zip.closeEntry(); } @@ -1731,12 +1726,7 @@ public final class FilePath implements Serializable { File parent = f.getParentFile(); if (parent != null) parent.mkdirs(); - OutputStream fos = new FileOutputStream(f); - try { - IOUtils.copy(t,fos); - } finally { - fos.close(); - } + IOUtils.copy(t,f); f.setLastModified(te.getModTime().getTime()); int mode = te.getMode()&0777; if(mode!=0 && !Hudson.isWindows()) // be defensive diff --git a/core/src/main/java/hudson/model/UpdateCenter.java b/core/src/main/java/hudson/model/UpdateCenter.java index cc30cd247e..e415d35d24 100644 --- a/core/src/main/java/hudson/model/UpdateCenter.java +++ b/core/src/main/java/hudson/model/UpdateCenter.java @@ -32,11 +32,8 @@ import hudson.PluginWrapper; import hudson.ProxyConfiguration; import hudson.Util; import hudson.XmlFile; -import hudson.triggers.SafeTimerTask; -import hudson.init.Initializer; -import hudson.init.InitMilestone; -import static hudson.init.InitMilestone.JOB_LOADED; import static hudson.init.InitMilestone.PLUGINS_STARTED; +import hudson.init.Initializer; import hudson.lifecycle.Lifecycle; import hudson.model.UpdateSite.Data; import hudson.model.UpdateSite.Plugin; @@ -45,9 +42,7 @@ import hudson.util.DaemonThreadFactory; import hudson.util.IOException2; import hudson.util.PersistedList; import hudson.util.XStream2; -import hudson.util.DoubleLaunchChecker; import org.acegisecurity.Authentication; -import org.apache.commons.io.IOUtils; import org.apache.commons.io.input.CountingInputStream; import org.apache.commons.io.output.NullOutputStream; import org.kohsuke.stapler.StaplerResponse; @@ -57,7 +52,6 @@ import javax.servlet.ServletException; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; @@ -522,9 +516,7 @@ public class UpdateCenter extends AbstractModelObject implements Saveable { private void testConnection(URL url) throws IOException { try { - InputStream in = ProxyConfiguration.open(url).getInputStream(); - IOUtils.copy(in,new NullOutputStream()); - in.close(); + Util.copyStreamAndClose(ProxyConfiguration.open(url).getInputStream(),new NullOutputStream()); } catch (SSLHandshakeException e) { if (e.getMessage().contains("PKIX path building failed")) // fix up this crappy error message from JDK diff --git a/core/src/main/java/hudson/util/IOUtils.java b/core/src/main/java/hudson/util/IOUtils.java new file mode 100644 index 0000000000..839e1d784a --- /dev/null +++ b/core/src/main/java/hudson/util/IOUtils.java @@ -0,0 +1,34 @@ +package hudson.util; + +import java.io.OutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.FileOutputStream; + +/** + * Adds more to commons-io. + * + * @author Kohsuke Kawaguchi + * @since 1.337 + */ +public class IOUtils extends org.apache.commons.io.IOUtils { + public static void copy(File src, OutputStream out) throws IOException { + FileInputStream in = new FileInputStream(src); + try { + copy(in,out); + } finally { + closeQuietly(in); + } + } + + public static void copy(InputStream in, File out) throws IOException { + FileOutputStream fos = new FileOutputStream(out); + try { + copy(in,fos); + } finally { + closeQuietly(fos); + } + } +} diff --git a/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java b/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java index 46012eb788..ede2de6a81 100644 --- a/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java +++ b/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java @@ -76,7 +76,6 @@ import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; -import java.io.OutputStream; import java.lang.annotation.Annotation; import java.lang.ref.WeakReference; import java.lang.reflect.Method; @@ -106,7 +105,6 @@ import junit.framework.TestCase; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; import org.apache.commons.beanutils.PropertyUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException; @@ -408,12 +406,7 @@ public abstract class HudsonTestCase extends TestCase implements RootAction { LOGGER.warning("Extracting a copy of Maven bundled in the test harness. " + "To avoid a performance hit, set the system property 'maven.home' to point to a Maven2 installation."); FilePath mvn = hudson.getRootPath().createTempFile("maven", "zip"); - OutputStream os = mvn.write(); - try { - IOUtils.copy(HudsonTestCase.class.getClassLoader().getResourceAsStream(mavenVersion + "-bin.zip"), os); - } finally { - os.close(); - } + mvn.copyFrom(HudsonTestCase.class.getClassLoader().getResource(mavenVersion + "-bin.zip")); File mvnHome = createTmpDir(); mvn.unzip(new FilePath(mvnHome)); // TODO: switch to tar that preserves file permissions more easily @@ -437,12 +430,7 @@ public abstract class HudsonTestCase extends TestCase implements RootAction { LOGGER.warning("Extracting a copy of Ant bundled in the test harness. " + "To avoid a performance hit, set the environment variable ANT_HOME to point to an Ant installation."); FilePath ant = hudson.getRootPath().createTempFile("ant", "zip"); - OutputStream os = ant.write(); - try { - IOUtils.copy(HudsonTestCase.class.getClassLoader().getResourceAsStream("apache-ant-1.7.1-bin.zip"), os); - } finally { - os.close(); - } + ant.copyFrom(HudsonTestCase.class.getClassLoader().getResource("apache-ant-1.7.1-bin.zip")); File antHome = createTmpDir(); ant.unzip(new FilePath(antHome)); // TODO: switch to tar that preserves file permissions more easily diff --git a/test/src/main/java/org/jvnet/hudson/test/JavaNetReverseProxy.java b/test/src/main/java/org/jvnet/hudson/test/JavaNetReverseProxy.java index 269ec6fce7..564be39940 100644 --- a/test/src/main/java/org/jvnet/hudson/test/JavaNetReverseProxy.java +++ b/test/src/main/java/org/jvnet/hudson/test/JavaNetReverseProxy.java @@ -1,8 +1,8 @@ package org.jvnet.hudson.test; import hudson.Util; +import hudson.util.IOUtils; import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; import org.mortbay.jetty.Server; import org.mortbay.jetty.bio.SocketConnector; import org.mortbay.jetty.handler.ContextHandlerCollection; @@ -86,7 +86,7 @@ public class JavaNetReverseProxy extends HttpServlet { } resp.setContentType(getMimeType(path)); - IOUtils.copy(new FileInputStream(cache),resp.getOutputStream()); + IOUtils.copy(cache,resp.getOutputStream()); } private String getMimeType(String path) {