javanetwork-programmingsocketshostnameports

Java: Common way to validate and convert "host:port" to InetSocketAddress?


What is the common way in Java to validate and convert a string of the form host:port into an instance of InetSocketAddress?

It would be nice if following criteria were met:


Solution

  • I myself propose one possible workaround solution.

    Convert a string into URI (this would validate it automatically) and then query the URI's host and port components.

    Sadly, an URI with a host component MUST have a scheme. This is why this solution is "not perfect".

    String string = ... // some string which has to be validated
    
    try {
      // WORKAROUND: add any scheme to make the resulting URI valid.
      URI uri = new URI("my://" + string); // may throw URISyntaxException
      String host = uri.getHost();
      int port = uri.getPort();
    
      if (uri.getHost() == null || uri.getPort() == -1) {
        throw new URISyntaxException(uri.toString(),
          "URI must have host and port parts");
      }
    
      // here, additional checks can be performed, such as
      // presence of path, query, fragment, ...
    
    
      // validation succeeded
      return new InetSocketAddress (host, port);
    
    } catch (URISyntaxException ex) {
      // validation failed
    }
    

    This solution needs no custom string parsing, works with IPv4 (1.1.1.1:123), IPv6 ([::0]:123) and host names (my.host.com:123).

    Accidentally, this solution is well suited for my scenario. I was going to use URI schemes anyway.