diff --git a/cli/src/main/java/hudson/cli/CLI.java b/cli/src/main/java/hudson/cli/CLI.java index 35befac2e9..6abf4ae80f 100644 --- a/cli/src/main/java/hudson/cli/CLI.java +++ b/cli/src/main/java/hudson/cli/CLI.java @@ -491,14 +491,9 @@ public class CLI { private static String computeVersion() { Properties props = new Properties(); - try { - InputStream is = CLI.class.getResourceAsStream("/jenkins/cli/jenkins-cli-version.properties"); + try (InputStream is = CLI.class.getResourceAsStream("/jenkins/cli/jenkins-cli-version.properties")) { if (is != null) { - try { - props.load(is); - } finally { - is.close(); - } + props.load(is); } } catch (IOException e) { e.printStackTrace(); // if the version properties is missing, that's OK. diff --git a/core/src/main/java/hudson/FilePath.java b/core/src/main/java/hudson/FilePath.java index 3b4f9b65de..60c213b7e0 100644 --- a/core/src/main/java/hudson/FilePath.java +++ b/core/src/main/java/hudson/FilePath.java @@ -532,13 +532,10 @@ public final class FilePath implements SerializableOnlyOverRemoting { @Override public Integer invoke(File f, VirtualChannel channel) throws IOException { - Archiver a = factory.create(out); - try { + try (Archiver a = factory.create(out)) { scanner.scan(f, ignoringSymlinks(a, verificationRoot, noFollowLinks)); - } finally { - a.close(); + return a.countEntries(); } - return a.countEntries(); } private static final long serialVersionUID = 1L; @@ -708,10 +705,9 @@ public final class FilePath implements SerializableOnlyOverRemoting { private static void unzip(File dir, File zipFile) throws IOException { dir = dir.getAbsoluteFile(); // without absolutization, getParentFile below seems to fail - ZipFile zip = new ZipFile(zipFile); - Enumeration entries = zip.getEntries(); - try { + try (ZipFile zip = new ZipFile(zipFile)) { + Enumeration entries = zip.getEntries(); while (entries.hasMoreElements()) { ZipEntry e = entries.nextElement(); File f = new File(dir, e.getName()); @@ -740,8 +736,6 @@ public final class FilePath implements SerializableOnlyOverRemoting { Files.setLastModifiedTime(Util.fileToPath(f), e.getLastModifiedTime()); } } - } finally { - zip.close(); } } @@ -922,11 +916,9 @@ public final class FilePath implements SerializableOnlyOverRemoting { * @since 1.292 */ public void untarFrom(InputStream _in, final TarCompression compression) throws IOException, InterruptedException { - try { + try (_in) { final InputStream in = new RemoteInputStream(_in, Flag.GREEDY); act(new UntarFrom(compression, in)); - } finally { - _in.close(); } } @@ -2947,10 +2939,8 @@ public final class FilePath implements SerializableOnlyOverRemoting { */ private static Integer writeToTar(File baseDir, DirScanner scanner, OutputStream out) throws IOException { Archiver tw = ArchiverFactory.TAR.create(out); - try { + try (tw) { scanner.scan(baseDir, tw); - } finally { - tw.close(); } return tw.countEntries(); } diff --git a/core/src/main/java/hudson/TcpSlaveAgentListener.java b/core/src/main/java/hudson/TcpSlaveAgentListener.java index eb6358f7a3..ddecdfa096 100644 --- a/core/src/main/java/hudson/TcpSlaveAgentListener.java +++ b/core/src/main/java/hudson/TcpSlaveAgentListener.java @@ -313,7 +313,7 @@ public final class TcpSlaveAgentListener extends Thread { * Primarily used to test the low-level connectivity. */ private void respondHello(String header, Socket s) throws IOException { - try { + try (s) { DataOutputStream out = new DataOutputStream(s.getOutputStream()); String response; if (header.startsWith("GET / ")) { @@ -336,8 +336,6 @@ public final class TcpSlaveAgentListener extends Thread { InputStream i = s.getInputStream(); IOUtils.copy(i, NullOutputStream.NULL_OUTPUT_STREAM); s.shutdownInput(); - } finally { - s.close(); } } @@ -400,20 +398,18 @@ public final class TcpSlaveAgentListener extends Thread { @Override public void handle(Socket socket) throws IOException, InterruptedException { - try { + try (socket) { try (OutputStream stream = socket.getOutputStream()) { LOGGER.log(Level.FINE, "Received ping request from {0}", socket.getRemoteSocketAddress()); stream.write(ping); stream.flush(); LOGGER.log(Level.FINE, "Sent ping response to {0}", socket.getRemoteSocketAddress()); } - } finally { - socket.close(); } } public boolean connect(Socket socket) throws IOException { - try { + try (socket) { LOGGER.log(Level.FINE, "Requesting ping from {0}", socket.getRemoteSocketAddress()); try (DataOutputStream out = new DataOutputStream(socket.getOutputStream())) { out.writeUTF("Protocol:Ping"); @@ -435,8 +431,6 @@ public final class TcpSlaveAgentListener extends Thread { } } } - } finally { - socket.close(); } } } diff --git a/core/src/main/java/hudson/Util.java b/core/src/main/java/hudson/Util.java index 6af39fc1cb..51abbf86a1 100644 --- a/core/src/main/java/hudson/Util.java +++ b/core/src/main/java/hudson/Util.java @@ -625,7 +625,7 @@ public class Util { */ @NonNull public static String getDigestOf(@NonNull InputStream source) throws IOException { - try { + try (source) { MessageDigest md5 = getMd5(); DigestInputStream in = new DigestInputStream(source, md5); // Note: IOUtils.copy() buffers the input internally, so there is no @@ -634,8 +634,6 @@ public class Util { return toHexString(md5.digest()); } catch (NoSuchAlgorithmException e) { throw new IOException("MD5 not installed", e); // impossible - } finally { - source.close(); } /* JENKINS-18178: confuses Maven 2 runner try { diff --git a/core/src/main/java/hudson/util/io/ArchiverFactory.java b/core/src/main/java/hudson/util/io/ArchiverFactory.java index b693773486..2bb32d3778 100644 --- a/core/src/main/java/hudson/util/io/ArchiverFactory.java +++ b/core/src/main/java/hudson/util/io/ArchiverFactory.java @@ -24,6 +24,7 @@ package hudson.util.io; +import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import hudson.FilePath.TarCompression; import java.io.IOException; @@ -42,6 +43,7 @@ public abstract class ArchiverFactory implements Serializable { /** * Creates an archiver on top of the given stream. */ + @NonNull public abstract Archiver create(OutputStream out) throws IOException; /** @@ -79,6 +81,7 @@ public abstract class ArchiverFactory implements Serializable { this.method = method; } + @NonNull @Override public Archiver create(OutputStream out) throws IOException { return new TarArchiver(method.compress(out)); @@ -88,6 +91,7 @@ public abstract class ArchiverFactory implements Serializable { } private static final class ZipArchiverFactory extends ArchiverFactory { + @NonNull @Override public Archiver create(OutputStream out) { return new ZipArchiver(out); @@ -103,6 +107,7 @@ public abstract class ArchiverFactory implements Serializable { this.prefix = prefix; } + @NonNull @Override public Archiver create(OutputStream out) { return new ZipArchiver(out, true, prefix);