Java Utililty Methods HTTP Port Find

List of utility methods to do HTTP Port Find

Description

The list of methods to do HTTP Port Find are organized into topic(s).

Method

StringgetPortInRange(int minPort, int maxPort)
Get a free port in supplied port range.
final int MIN_PORT_LIMIT = 1;
final int MAX_PORT_LIMIT = 65536;
if (minPort < 0)
    minPort = MIN_PORT_LIMIT;
if (maxPort > MAX_PORT_LIMIT)
    maxPort = MAX_PORT_LIMIT;
IOException ioEx = null;
for (int i = minPort; i <= maxPort; i++) {
...
int[]getPorts(int count)
Returns an array containing the specified number of available local ports.
int[] ports = new int[count];
Set<ServerSocket> openSockets = new HashSet<ServerSocket>(count + USED_PORTS.size());
for (int i = 0; i < count;) {
    try {
        ServerSocket socket = new ServerSocket(0);
        int port = socket.getLocalPort();
        openSockets.add(socket);
        if (!USED_PORTS.contains(port)) {
...
ListgetPorts(int number, int MIN_PORT_NUMBER, int MAX_PORT_NUMBER)
get Ports
int start = MIN_PORT_NUMBER + 1;
boolean stop = false;
List<Integer> result = new ArrayList<Integer>();
while (!stop && start < MAX_PORT_NUMBER) {
    if (available(start, MIN_PORT_NUMBER, MAX_PORT_NUMBER)) {
        result.add(start);
        if (result.size() == number) {
            stop = true;
...
StringgetPrometheusMetrics(int metricsPort)
get Prometheus Metrics
StringBuilder result = new StringBuilder();
URL url = new URL(String.format("http://%s:%s", InetAddress.getLocalHost().getHostAddress(), metricsPort));
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
    result.append(line + System.lineSeparator());
...
intgetRandomAvailablePort()
get Random Available Port
int port = 2048;
boolean br = false;
while (port < 49151 && !br) {
    try {
        new ServerSocket(port).close();
        new DatagramSocket(port).close();
        br = true;
    } catch (Exception ex) {
...
intgetRandomFreePort()
get Random Free Port
return getRandomFreePort(MIN_PORT, MAX_PORT);
intgetRandomFreePort()
get Random Free Port
Random r = new Random();
int count = 0;
while (count < 13) {
    int port = r.nextInt((1 << 16) - 1024) + 1024;
    try (ServerSocket so = new ServerSocket(port)) {
        so.setReuseAddress(true);
        return port;
    } catch (IOException e) {
...
intgetRandomOpenPort()
get Random Open Port
try (ServerSocket socket = new ServerSocket(0)) {
    return socket.getLocalPort();
intgetRandomPort()
get Random Port
for (int port = 22332; port < 22500; port++) {
    if (trySocket(port)) {
        return port;
throw new IllegalStateException("Cannot find a single free port");
intgetRandomPort()
get Random Port
ServerSocket server = null;
try {
    server = new ServerSocket(0);
    return server.getLocalPort();
} catch (IOException e) {
    throw new Error(e);
} finally {
    if (server != null) {
...