Class NetworkAddress

java.lang.Object
org.elasticsearch.common.network.NetworkAddress

public final class NetworkAddress extends Object
Utility functions for presentation of network addresses.

Java's address formatting is particularly bad, every address has an optional host if its resolved, so IPv4 addresses often look like this (note the confusing leading slash):

    /127.0.0.1
 
IPv6 addresses are even worse, with no IPv6 address compression, and often containing things like numeric scopeids, which are even more confusing (e.g. not going to work in any user's browser, refer to an interface on another machine, etc):
    /0:0:0:0:0:0:0:1%1
 
Note: the %1 is the "scopeid".

This class provides sane address formatting instead, e.g. 127.0.0.1 and ::1 respectively. No methods do reverse lookups.

  • Method Details

    • format

      public static String format(InetAddress address)
      Formats a network address for display purposes.

      This formats only the address, any hostname information, if present, is ignored. IPv6 addresses are compressed and without scope identifiers.

      Example output with just an address:

      • IPv4: 127.0.0.1
      • IPv6: ::1
      Parameters:
      address - IPv4 or IPv6 address
      Returns:
      formatted string
    • format

      public static String format(InetSocketAddress address)
      Formats a network address and port for display purposes.

      This formats the address with format(InetAddress) and appends the port number. IPv6 addresses will be bracketed. Any host information, if present is ignored.

      Example output:

      • IPv4: 127.0.0.1:9300
      • IPv6: [::1]:9300
      Parameters:
      address - IPv4 or IPv6 address with port
      Returns:
      formatted string
    • format

      public static String format(InetAddress address, int port)
      Formats a network address and port for display purposes.

      This formats the address with format(InetAddress) and appends the port number. IPv6 addresses will be bracketed. Any host information, if present is ignored.

      Example output:

      • IPv4: 127.0.0.1:9300
      • IPv6: [::1]:9300
      Parameters:
      address - IPv4 or IPv6 address
      port - port
      Returns:
      formatted string
    • format

      public static String format(InetAddress address, PortsRange portsRange)
      Formats a network address and port range for display purposes.

      This formats the address with format(InetAddress) and appends the port range in brackets. In case there is only one port, the result is the same with format(InetAddress, int).

      Example output:

      • IPv4 no port: 127.0.0.1
      • IPv4 single port: 127.0.0.1:9300
      • IPv4 multiple ports: 127.0.0.1:[9300-9400]
      • IPv6 multiple ports: [::1]:[9300-9400]
      Parameters:
      address - IPv4 or IPv6 address
      portsRange - range of ports
      Returns:
      formatted string