Skip to content

Commit e140e22

Browse files
committed
[java] Using try-with-resources in ProtProber to make sure that the checked socket is closed
1 parent 456563b commit e140e22

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

java/client/src/org/openqa/selenium/net/PortProber.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,10 @@ private static int createAcceptablePort() {
104104
}
105105

106106
private static int checkPortIsFree(int port) {
107-
ServerSocket socket;
108-
try {
109-
socket = new ServerSocket();
107+
try (ServerSocket socket = new ServerSocket()) {
110108
socket.setReuseAddress(true);
111109
socket.bind(new InetSocketAddress("localhost", port));
112-
int localPort = socket.getLocalPort();
113-
socket.close();
114-
return localPort;
110+
return socket.getLocalPort();
115111
} catch (IOException e) {
116112
return -1;
117113
}
@@ -120,10 +116,8 @@ private static int checkPortIsFree(int port) {
120116
public static void waitForPortUp(int port, int timeout, TimeUnit unit) {
121117
long end = System.currentTimeMillis() + unit.toMillis(timeout);
122118
while (System.currentTimeMillis() < end) {
123-
try {
124-
Socket socket = new Socket();
119+
try (Socket socket = new Socket()) {
125120
socket.connect(new InetSocketAddress("localhost", port), 1000);
126-
socket.close();
127121
return;
128122
} catch (ConnectException | SocketTimeoutException e) {
129123
// Ignore this

0 commit comments

Comments
 (0)