Skip to content

Commit

Permalink
[java] consume the input of the connection
Browse files Browse the repository at this point in the history
  • Loading branch information
joerg1985 committed Nov 16, 2024
1 parent 7f4d4bf commit bee1571
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions java/src/org/openqa/selenium/net/UrlChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static java.util.concurrent.TimeUnit.MILLISECONDS;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
Expand Down Expand Up @@ -75,6 +76,7 @@ public void waitUntilAvailable(long timeout, TimeUnit unit, final URL... urls)
// Ok, try again.
} finally {
if (connection != null) {
consume(connection);
connection.disconnect();
}
}
Expand Down Expand Up @@ -127,6 +129,7 @@ public void waitUntilUnavailable(long timeout, TimeUnit unit, final URL url)
return null;
} finally {
if (connection != null) {
consume(connection);
connection.disconnect();
}
}
Expand Down Expand Up @@ -154,6 +157,27 @@ public void waitUntilUnavailable(long timeout, TimeUnit unit, final URL url)
}
}

/**
* Read and closes the ErrorStream / InputStream of the HttpURLConnection to allow Java reusing
* the open socket.
*
* @param connection the connection to consume the input
*/
private static void consume(HttpURLConnection connection) {
try {
InputStream data = connection.getErrorStream();
if (data == null) {
data = connection.getInputStream();
}
if (data != null) {
data.readAllBytes();
data.close();
}
} catch (IOException e) {
// swallow
}
}

private HttpURLConnection connectToUrl(URL url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(CONNECT_TIMEOUT_MS);
Expand Down

0 comments on commit bee1571

Please sign in to comment.