Skip to content

Commit

Permalink
Cleanup - Use try-with-resources for automatic resource cleanup
Browse files Browse the repository at this point in the history
This reduces boilerplate code.
  • Loading branch information
darxriggs committed Feb 14, 2019
1 parent a5d1027 commit a9aae82
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 18 deletions.
Expand Up @@ -87,19 +87,15 @@ public boolean checkout(AbstractBuild<?,?> build, Launcher launcher, FilePath wo
workspace.unzipFrom(secondZip.openStream());

// Get list of files changed in secondZip.
ZipInputStream zip = new ZipInputStream(secondZip.openStream());
ZipEntry e;
ExtractChangeLogParser.ExtractChangeLogEntry changeLog = new ExtractChangeLogParser.ExtractChangeLogEntry(secondZip.toString());

try {
try (ZipInputStream zip = new ZipInputStream(secondZip.openStream())) {
ZipEntry e;
while ((e = zip.getNextEntry()) != null) {
if (!e.isDirectory())
changeLog.addFile(new ExtractChangeLogParser.FileInZip(e.getName()));
}
}
finally {
zip.close();
}
saveToChangeLog(changeLogFile, changeLog);

return true;
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/org/jvnet/hudson/test/JenkinsRule.java
Expand Up @@ -1068,12 +1068,9 @@ public JSONWebResponse postJSON(@Nonnull String path, @Nonnull Object json) thro

byte[] content = json.toString().getBytes(UTF8);
conn.setRequestProperty("Content-Length", String.valueOf(content.length));
final OutputStream os = conn.getOutputStream();
try {
try (OutputStream os = conn.getOutputStream()) {
os.write(content);
os.flush();
} finally {
os.close();
}

WebResponseData webResponseData;
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/org/jvnet/hudson/test/PropertiesTestSuite.java
Expand Up @@ -66,11 +66,8 @@ public synchronized Object put(Object key, Object value) {
return null;
}
};
InputStream is = resource.openStream();
try {
try (InputStream is = resource.openStream()) {
props.load(is);
} finally {
is.close();
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/main/java/org/jvnet/hudson/test/TestPluginManager.java
Expand Up @@ -114,8 +114,7 @@ private Set<String> loadBundledPlugins(File fromDir) throws IOException, URISynt
// and copy them into $JENKINS_HOME/plugins.
URL index = getClass().getResource("/test-dependencies/index");
if (index!=null) {// if built with maven-hpi-plugin < 1.52 this file won't exist.
BufferedReader r = new BufferedReader(new InputStreamReader(index.openStream(), StandardCharsets.UTF_8));
try {
try (BufferedReader r = new BufferedReader(new InputStreamReader(index.openStream(), StandardCharsets.UTF_8))) {
String line;
while ((line=r.readLine())!=null) {
final URL url = new URL(index, line + ".jpi");
Expand All @@ -140,8 +139,6 @@ private Set<String> loadBundledPlugins(File fromDir) throws IOException, URISynt
copyBundledPlugin(new URL(index, line + ".hpi"), line + ".jpi"); // fallback to hpi
}
}
} finally {
r.close();
}
}

Expand Down

0 comments on commit a9aae82

Please sign in to comment.