public final class NetworkAddress
extends java.lang.Object
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
This class provides sane address formatting instead, e.g.
127.0.0.1
and ::1
respectively. No methods do reverse
lookups.Modifier and Type | Method and Description |
---|---|
static java.lang.String |
format(java.net.InetAddress address)
Formats a network address (with optional host) for display purposes.
|
static java.lang.String |
format(java.net.InetSocketAddress address)
Formats a network address and port for display purposes.
|
static java.lang.String |
formatAddress(java.net.InetAddress address)
Formats a network address for display purposes.
|
static java.lang.String |
formatAddress(java.net.InetSocketAddress address)
Formats a network address and port for display purposes.
|
public static java.lang.String format(java.net.InetAddress address)
If the host is already resolved (typically because, we looked up
a name to do that), then we include it, otherwise it is
omitted. See formatAddress(InetAddress)
if you only
want the address.
IPv6 addresses are compressed and without scope identifiers.
Example output with already-resolved hostnames:
localhost/127.0.0.1
localhost/::1
Example output with just an address:
127.0.0.1
::1
address
- IPv4 or IPv6 addressformatAddress(InetAddress)
public static java.lang.String format(java.net.InetSocketAddress address)
If the host is already resolved (typically because, we looked up
a name to do that), then we include it, otherwise it is
omitted. See formatAddress(InetSocketAddress)
if you only
want the address.
This formats the address with format(InetAddress)
and appends the port number. IPv6 addresses will be bracketed.
Example output with already-resolved hostnames:
localhost/127.0.0.1:9300
localhost/[::1]:9300
Example output with just an address:
127.0.0.1:9300
[::1]:9300
address
- IPv4 or IPv6 address with portformatAddress(InetSocketAddress)
public static java.lang.String formatAddress(java.net.InetAddress address)
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:
127.0.0.1
::1
address
- IPv4 or IPv6 addresspublic static java.lang.String formatAddress(java.net.InetSocketAddress address)
This formats the address with formatAddress(InetAddress)
and appends the port number. IPv6 addresses will be bracketed.
Any host information, if present is ignored.
Example output:
127.0.0.1:9300
[::1]:9300
address
- IPv4 or IPv6 address with port