Skip to content

Commit 0fdb500

Browse files
committed
Putting try-with-resources in action
1 parent 7367258 commit 0fdb500

9 files changed

Lines changed: 25 additions & 108 deletions

File tree

java/client/src/org/openqa/selenium/firefox/FirefoxProfile.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,14 +287,10 @@ public void updateUserPrefs(File userPrefs) {
287287
prefs.setPreference("browser.startup.page", 1);
288288
}
289289

290-
FileWriter writer = null;
291-
try {
292-
writer = new FileWriter(userPrefs);
290+
try (FileWriter writer = new FileWriter(userPrefs)) {
293291
prefs.writeTo(writer);
294292
} catch (IOException e) {
295293
throw new WebDriverException(e);
296-
} finally {
297-
IOUtils.closeQuietly(writer);
298294
}
299295
}
300296

java/client/src/org/openqa/selenium/firefox/Preferences.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
import com.google.common.annotations.VisibleForTesting;
2424
import com.google.common.collect.Maps;
2525
import com.google.common.io.CharStreams;
26+
import com.google.common.io.Closeables;
2627
import com.google.common.io.LineReader;
2728

2829
import org.openqa.selenium.WebDriverException;
29-
import org.openqa.selenium.io.IOUtils;
3030
import org.openqa.selenium.remote.JsonToBeanConverter;
3131

3232
import java.io.File;
@@ -68,25 +68,25 @@ public Preferences(Reader defaults) {
6868

6969
public Preferences(Reader defaults, File userPrefs) {
7070
readDefaultPreferences(defaults);
71-
FileReader reader = null;
72-
try {
73-
reader = new FileReader(userPrefs);
71+
try (FileReader reader = new FileReader(userPrefs)) {
7472
readPreferences(reader);
7573
} catch (IOException e) {
7674
throw new WebDriverException(e);
77-
} finally {
78-
IOUtils.closeQuietly(reader);
7975
}
8076
}
8177

78+
@VisibleForTesting
8279
public Preferences(Reader defaults, Reader reader) {
8380
readDefaultPreferences(defaults);
8481
try {
8582
readPreferences(reader);
8683
} catch (IOException e) {
8784
throw new WebDriverException(e);
8885
} finally {
89-
IOUtils.closeQuietly(reader);
86+
try {
87+
Closeables.close(reader, true);
88+
} catch (IOException ignoted) {
89+
}
9090
}
9191
}
9292

java/client/src/org/openqa/selenium/io/FileHandler.java

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,17 @@
1919
package org.openqa.selenium.io;
2020

2121
import com.google.common.collect.Lists;
22-
import com.google.common.io.Closeables;
2322

2423
import org.openqa.selenium.Platform;
2524

2625
import java.io.BufferedReader;
2726
import java.io.File;
28-
import java.io.FileInputStream;
2927
import java.io.FileOutputStream;
3028
import java.io.OutputStream;
3129
import java.io.FileReader;
3230
import java.io.IOException;
3331
import java.io.InputStream;
3432
import java.io.Reader;
35-
import java.nio.channels.FileChannel;
3633
import java.nio.file.Files;
3734
import java.util.List;
3835

@@ -54,11 +51,8 @@ public static void copyResource(File outputDir, Class<?> forClassLoader, String.
5451
Zip zip = new Zip();
5552

5653
for (String name : names) {
57-
InputStream is = locateResource(forClassLoader, name);
58-
try {
54+
try (InputStream is = locateResource(forClassLoader, name)) {
5955
zip.unzipFile(outputDir, is, name);
60-
} finally {
61-
is.close();
6256
}
6357
}
6458
}
@@ -104,19 +98,11 @@ public static boolean createDir(File dir) throws IOException {
10498
}
10599

106100
public static boolean makeWritable(File file) throws IOException {
107-
if (file.canWrite()) {
108-
return true;
109-
}
110-
111-
return file.setWritable(true);
101+
return file.canWrite() || file.setWritable(true);
112102
}
113103

114104
public static boolean makeExecutable(File file) throws IOException {
115-
if (canExecute(file)) {
116-
return true;
117-
}
118-
119-
return file.setExecutable(true);
105+
return canExecute(file) || file.setExecutable(true);
120106
}
121107

122108
public static Boolean canExecute(File file) {
@@ -226,9 +212,7 @@ public boolean isRequired(File file) {
226212
}
227213

228214
public static String readAsString(File toRead) throws IOException {
229-
Reader reader = null;
230-
try {
231-
reader = new BufferedReader(new FileReader(toRead));
215+
try (Reader reader = new BufferedReader(new FileReader(toRead))) {
232216
StringBuilder builder = new StringBuilder();
233217

234218
char[] buffer = new char[4096];
@@ -240,8 +224,6 @@ public static String readAsString(File toRead) throws IOException {
240224
}
241225

242226
return builder.toString();
243-
} finally {
244-
Closeables.close(reader, false);
245227
}
246228
}
247229
}

java/client/src/org/openqa/selenium/io/IOUtils.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@
1818

1919
package org.openqa.selenium.io;
2020

21-
import java.io.Closeable;
2221
import java.io.IOException;
2322
import java.io.InputStream;
2423

25-
import com.google.common.io.Closeables;
26-
2724
public class IOUtils {
2825
private static final int BUFFER = 4096;
2926

@@ -40,10 +37,4 @@ public static String readFully(InputStream in) throws IOException {
4037
return sb.toString();
4138
}
4239

43-
public static void closeQuietly(Closeable closeable) {
44-
try {
45-
Closeables.close(closeable, true);
46-
} catch (IOException ignoted) {
47-
}
48-
}
4940
}

java/client/src/org/openqa/selenium/io/Zip.java

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
package org.openqa.selenium.io;
2020

21-
import com.google.common.io.Closeables;
22-
2321
import java.io.BufferedOutputStream;
2422
import java.io.ByteArrayInputStream;
2523
import java.io.ByteArrayOutputStream;
@@ -44,49 +42,32 @@ public void zip(File inputDir, File output) throws IOException {
4442
throw new IOException("File already exists: " + output);
4543
}
4644

47-
FileOutputStream fos = null;
48-
try {
49-
fos = new FileOutputStream(output);
45+
try (FileOutputStream fos = new FileOutputStream(output)) {
5046
zip(inputDir, fos);
51-
} finally {
52-
Closeables.close(fos, false);
5347
}
5448
}
5549

5650
public String zip(File inputDir) throws IOException {
57-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
58-
59-
try {
51+
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
6052
zip(inputDir, bos);
6153
return Base64.getEncoder().encodeToString(bos.toByteArray());
62-
} finally {
63-
bos.close();
6454
}
6555
}
6656

6757
public String zipFile(File baseDir, File fileToCompress) throws IOException {
6858
checkArgument(fileToCompress.isFile(), "File should be a file: " + fileToCompress);
6959

70-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
71-
ZipOutputStream zos = new ZipOutputStream(bos);
72-
73-
try {
60+
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
61+
ZipOutputStream zos = new ZipOutputStream(bos)) {
7462
addToZip(baseDir.getAbsolutePath(), zos, fileToCompress);
7563
return Base64.getEncoder().encodeToString(bos.toByteArray());
76-
} finally {
77-
zos.close();
78-
bos.close();
7964
}
8065

8166
}
8267

8368
private void zip(File inputDir, OutputStream writeTo) throws IOException {
84-
ZipOutputStream zos = null;
85-
try {
86-
zos = new ZipOutputStream(writeTo);
69+
try (ZipOutputStream zos = new ZipOutputStream(writeTo)) {
8770
addToZip(inputDir.getAbsolutePath(), zos, inputDir);
88-
} finally {
89-
Closeables.close(zos, false);
9071
}
9172
}
9273

@@ -119,30 +100,19 @@ private void addToZip(String basePath, ZipOutputStream zos, File toAdd) throws I
119100
public void unzip(String source, File outputDir) throws IOException {
120101
byte[] bytes = Base64.getMimeDecoder().decode(source);
121102

122-
ByteArrayInputStream bis = null;
123-
try {
124-
bis = new ByteArrayInputStream(bytes);
103+
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes)) {
125104
unzip(bis, outputDir);
126-
} finally {
127-
Closeables.close(bis, false);
128105
}
129106
}
130107

131108
public void unzip(File source, File outputDir) throws IOException {
132-
FileInputStream fis = null;
133-
134-
try {
135-
fis = new FileInputStream(source);
109+
try (FileInputStream fis = new FileInputStream(source)) {
136110
unzip(fis, outputDir);
137-
} finally {
138-
Closeables.close(fis, false);
139111
}
140112
}
141113

142114
public void unzip(InputStream source, File outputDir) throws IOException {
143-
ZipInputStream zis = new ZipInputStream(source);
144-
145-
try {
115+
try (ZipInputStream zis = new ZipInputStream(source)) {
146116
ZipEntry entry;
147117
while ((entry = zis.getNextEntry()) != null) {
148118
File file = new File(outputDir, entry.getName());
@@ -153,8 +123,6 @@ public void unzip(InputStream source, File outputDir) throws IOException {
153123

154124
unzipFile(outputDir, zis, entry.getName());
155125
}
156-
} finally {
157-
zis.close();
158126
}
159127
}
160128

@@ -165,15 +133,12 @@ public void unzipFile(File output, InputStream zipStream, String name)
165133
if (!FileHandler.createDir(toWrite.getParentFile()))
166134
throw new IOException("Cannot create parent director for: " + name);
167135

168-
OutputStream out = new BufferedOutputStream(new FileOutputStream(toWrite), BUF_SIZE);
169-
try {
136+
try (OutputStream out = new BufferedOutputStream(new FileOutputStream(toWrite), BUF_SIZE)) {
170137
byte[] buffer = new byte[BUF_SIZE];
171138
int read;
172139
while ((read = zipStream.read(buffer)) != -1) {
173140
out.write(buffer, 0, read);
174141
}
175-
} finally {
176-
out.close();
177142
}
178143
}
179144
}

java/client/src/org/openqa/selenium/safari/SafariLocator.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
package org.openqa.selenium.safari;
2020

21-
import org.openqa.selenium.io.IOUtils;
2221
import org.openqa.selenium.os.CommandLine;
2322

2423
import java.io.File;
@@ -118,20 +117,16 @@ protected BrowserInstallation retrieveValidInstallationPath(File launcher) {
118117

119118
protected boolean isScriptFile(File aFile) {
120119
final char firstTwoChars[] = new char[2];
121-
FileReader reader = null;
122120
int charsRead;
123121

124-
try {
125-
reader = new FileReader(aFile);
122+
try (FileReader reader = new FileReader(aFile)) {
126123
charsRead = reader.read(firstTwoChars);
127124
if (2 != charsRead) {
128125
return false;
129126
}
130127
return (firstTwoChars[0] == '#' && firstTwoChars[1] == '!');
131128
} catch (IOException e) {
132129
throw new RuntimeException(e);
133-
} finally {
134-
IOUtils.closeQuietly(reader);
135130
}
136131
}
137132

java/client/test/org/openqa/selenium/environment/webserver/ManifestServlet.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,10 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
3939
String manifestPath = this.getServletContext().getRealPath(servletPath);
4040
String manifestContent = "";
4141

42-
InputStream is = null;
43-
try {
44-
is = new FileInputStream(manifestPath);
42+
try (InputStream is = new FileInputStream(manifestPath)) {
4543
manifestContent = new String(ByteStreams.toByteArray(is));
4644
} catch (IOException e) {
4745
throw new ServletException("Failed to read cache-manifest file: " + manifestPath);
48-
} finally {
49-
IOUtils.closeQuietly(is);
5046
}
5147

5248
response.setContentType("text/cache-manifest");

java/client/test/org/openqa/selenium/environment/webserver/Utf8Servlet.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,13 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
3939
String fileName = this.getServletContext().getRealPath(request.getPathInfo());
4040
String fileContent = "";
4141

42-
InputStream is = null;
43-
try {
44-
is = new FileInputStream(fileName);
42+
try (InputStream is = new FileInputStream(fileName)) {
4543
// Note: Must read the content as UTF8.
4644
fileContent = new String(ByteStreams.toByteArray(is), Charset.forName("UTF-8"));
4745
} catch (IOException e) {
4846
throw new ServletException("Failed to file: " + fileName + " based on request path: " +
4947
request.getPathInfo() + ", servlet path: " + request.getServletPath() +
5048
" and context path: " + request.getContextPath());
51-
} finally {
52-
IOUtils.closeQuietly(is);
5349
}
5450

5551
response.setContentType("text/html; charset=UTF-8");

java/server/src/org/openqa/grid/internal/TestSession.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import org.openqa.grid.web.servlet.handler.SeleniumBasedRequest;
4242
import org.openqa.grid.web.servlet.handler.SeleniumBasedResponse;
4343
import org.openqa.grid.web.servlet.handler.WebDriverRequest;
44-
import org.openqa.selenium.io.IOUtils;
4544

4645
import java.io.BufferedReader;
4746
import java.io.ByteArrayInputStream;
@@ -419,8 +418,7 @@ private HttpRequest prepareProxyRequest(HttpServletRequest request
419418
}
420419

421420
private void writeRawBody(HttpServletResponse response, byte[] rawBody) throws IOException {
422-
OutputStream out = response.getOutputStream();
423-
try {
421+
try (OutputStream out = response.getOutputStream()) {
424422
// We need to set the Content-Length header before we write to the output stream. Usually
425423
// the
426424
// Content-Length header is already set because we take it from the proxied request. But, it
@@ -437,8 +435,6 @@ private void writeRawBody(HttpServletResponse response, byte[] rawBody) throws I
437435
out.write(rawBody);
438436
} catch (IOException e) {
439437
throw new ClientGoneException(e);
440-
} finally {
441-
IOUtils.closeQuietly(out);
442438
}
443439
}
444440

0 commit comments

Comments
 (0)