Skip to content

Commit 70a74a7

Browse files
joerg1985jkim2492
authored andcommitted
[java] consume the input of the connection
1 parent ddb5ce9 commit 70a74a7

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

java/src/org/openqa/selenium/net/UrlChecker.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static java.util.concurrent.TimeUnit.MILLISECONDS;
2121

2222
import java.io.IOException;
23+
import java.io.InputStream;
2324
import java.net.HttpURLConnection;
2425
import java.net.URL;
2526
import java.util.Arrays;
@@ -75,6 +76,7 @@ public void waitUntilAvailable(long timeout, TimeUnit unit, final URL... urls)
7576
// Ok, try again.
7677
} finally {
7778
if (connection != null) {
79+
consume(connection);
7880
connection.disconnect();
7981
}
8082
}
@@ -127,6 +129,7 @@ public void waitUntilUnavailable(long timeout, TimeUnit unit, final URL url)
127129
return null;
128130
} finally {
129131
if (connection != null) {
132+
consume(connection);
130133
connection.disconnect();
131134
}
132135
}
@@ -154,6 +157,27 @@ public void waitUntilUnavailable(long timeout, TimeUnit unit, final URL url)
154157
}
155158
}
156159

160+
/**
161+
* Read and closes the ErrorStream / InputStream of the HttpURLConnection to allow Java reusing
162+
* the open socket.
163+
*
164+
* @param connection the connection to consume the input
165+
*/
166+
private static void consume(HttpURLConnection connection) {
167+
try {
168+
InputStream data = connection.getErrorStream();
169+
if (data == null) {
170+
data = connection.getInputStream();
171+
}
172+
if (data != null) {
173+
data.readAllBytes();
174+
data.close();
175+
}
176+
} catch (IOException e) {
177+
// swallow
178+
}
179+
}
180+
157181
private HttpURLConnection connectToUrl(URL url) throws IOException {
158182
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
159183
connection.setConnectTimeout(CONNECT_TIMEOUT_MS);

0 commit comments

Comments
 (0)