Java HTTP Port Find resolve(final String desc, final int defaultPort)

Here you can find the source of resolve(final String desc, final int defaultPort)

Description

Parse and resolve an address string, looking up the IP address.

License

Apache License

Declaration

public static InetSocketAddress resolve(final String desc, final int defaultPort) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.net.InetAddress;
import java.net.InetSocketAddress;

import java.net.UnknownHostException;

public class Main {
    /** Parse and resolve an address string, looking up the IP address. */
    public static InetSocketAddress resolve(final String desc, final int defaultPort) {
        final InetSocketAddress addr = parse(desc, defaultPort);
        if (addr.getAddress() != null && addr.getAddress().isAnyLocalAddress()) {
            return addr;

        }/*  w w w  .j a v  a2 s . c  o  m*/
        try {
            final InetAddress host = InetAddress.getByName(addr.getHostName());
            return new InetSocketAddress(host, addr.getPort());
        } catch (UnknownHostException e) {
            throw new IllegalArgumentException("unknown host: " + desc, e);
        }
    }

    /** Parse an address string such as {@code host:port} or {@code *:port}. */
    public static InetSocketAddress parse(final String desc, final int defaultPort) {
        String hostStr;
        String portStr;

        if (desc.startsWith("[")) {
            // IPv6, as a raw IP address.
            //
            final int hostEnd = desc.indexOf(']');
            if (hostEnd < 0) {
                throw new IllegalArgumentException("invalid IPv6: " + desc);
            }

            hostStr = desc.substring(1, hostEnd);
            portStr = desc.substring(hostEnd + 1);
        } else {
            // IPv4, or a host name.
            //
            final int hostEnd = desc.indexOf(':');
            hostStr = 0 <= hostEnd ? desc.substring(0, hostEnd) : desc;
            portStr = 0 <= hostEnd ? desc.substring(hostEnd) : "";
        }

        if ("".equals(hostStr)) {
            hostStr = "*";
        }
        if (portStr.startsWith(":")) {
            portStr = portStr.substring(1);
        }

        final int port;
        if (portStr.length() > 0) {
            try {
                port = Integer.parseInt(portStr);
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("invalid port: " + desc);
            }
        } else {
            port = defaultPort;
        }

        if ("*".equals(hostStr)) {
            return new InetSocketAddress(port);
        }
        return InetSocketAddress.createUnresolved(hostStr, port);
    }
}

Related

  1. randomFreePort()
  2. randomFreePort()
  3. read(String host, int port)
  4. readHttpContent(String domain, String resource, int port)
  5. replace(final String host, final int port, final String user, final String program, final String relation, final String dataset, final String format)
  6. safePort(final String port)
  7. searchPort(int number, int even, boolean stream)
  8. selectAvailablePort(int preferedPort)
  9. sendCommand(final String command, final int monitorPort)