Skip to content

Commit

Permalink
Use try-with-resources where possible (jenkinsci#6765)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored Jul 5, 2022
1 parent 8833407 commit 76e5c98
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 35 deletions.
9 changes: 2 additions & 7 deletions cli/src/main/java/hudson/cli/CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -491,14 +491,9 @@ synchronized int exit() throws InterruptedException {

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.
Expand Down
22 changes: 6 additions & 16 deletions core/src/main/java/hudson/FilePath.java
Original file line number Diff line number Diff line change
Expand Up @@ -532,13 +532,10 @@ private static class Archive extends MasterToSlaveFileCallable<Integer> {

@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;
Expand Down Expand Up @@ -708,10 +705,9 @@ private static void unzip(File dir, InputStream in) throws IOException {

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<ZipEntry> entries = zip.getEntries();

try {
try (ZipFile zip = new ZipFile(zipFile)) {
Enumeration<ZipEntry> entries = zip.getEntries();
while (entries.hasMoreElements()) {
ZipEntry e = entries.nextElement();
File f = new File(dir, e.getName());
Expand Down Expand Up @@ -740,8 +736,6 @@ private static void unzip(File dir, File zipFile) throws IOException {
Files.setLastModifiedTime(Util.fileToPath(f), e.getLastModifiedTime());
}
}
} finally {
zip.close();
}
}

Expand Down Expand Up @@ -922,11 +916,9 @@ public OutputStream compress(OutputStream out) throws IOException {
* @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();
}
}

Expand Down Expand Up @@ -2947,10 +2939,8 @@ public int tar(OutputStream out, DirScanner scanner) throws IOException, Interru
*/
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();
}
Expand Down
12 changes: 3 additions & 9 deletions core/src/main/java/hudson/TcpSlaveAgentListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public void run() {
* 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 / ")) {
Expand All @@ -336,8 +336,6 @@ private void respondHello(String header, Socket s) throws IOException {
InputStream i = s.getInputStream();
IOUtils.copy(i, NullOutputStream.NULL_OUTPUT_STREAM);
s.shutdownInput();
} finally {
s.close();
}
}

Expand Down Expand Up @@ -400,20 +398,18 @@ public String getDisplayName() {

@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");
Expand All @@ -435,8 +431,6 @@ public boolean connect(Socket socket) throws IOException {
}
}
}
} finally {
socket.close();
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions core/src/main/java/hudson/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ public static String ensureEndsWith(@CheckForNull String subject, @CheckForNull
*/
@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
Expand All @@ -634,8 +634,6 @@ public static String getDigestOf(@NonNull InputStream source) throws IOException
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 {
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/hudson/util/io/ArchiverFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -79,6 +81,7 @@ private TarArchiverFactory(TarCompression method) {
this.method = method;
}

@NonNull
@Override
public Archiver create(OutputStream out) throws IOException {
return new TarArchiver(method.compress(out));
Expand All @@ -88,6 +91,7 @@ public Archiver create(OutputStream out) throws IOException {
}

private static final class ZipArchiverFactory extends ArchiverFactory {
@NonNull
@Override
public Archiver create(OutputStream out) {
return new ZipArchiver(out);
Expand All @@ -103,6 +107,7 @@ private static final class ZipWithoutSymLinksArchiverFactory extends ArchiverFac
this.prefix = prefix;
}

@NonNull
@Override
public Archiver create(OutputStream out) {
return new ZipArchiver(out, true, prefix);
Expand Down

0 comments on commit 76e5c98

Please sign in to comment.