@Internal public class NetUtils extends Object
| Modifier and Type | Class and Description |
|---|---|
static interface |
NetUtils.SocketFactory
A factory for a local socket from port number.
|
| Constructor and Description |
|---|
NetUtils() |
| Modifier and Type | Method and Description |
|---|---|
static Socket |
acceptWithoutTimeout(ServerSocket serverSocket)
Calls
ServerSocket.accept() on the provided server socket, suppressing any thrown
SocketTimeoutExceptions. |
static ServerSocket |
createSocketFromPorts(Iterator<Integer> portsIterator,
NetUtils.SocketFactory factory)
Tries to allocate a socket from the given sets of ports.
|
static int |
getAvailablePort()
Find a non-occupied port.
|
static URL |
getCorrectHostnamePort(String hostPort)
Converts a string of the form "host:port" into an
URL. |
static String |
getHostnameFromFQDN(String fqdn)
Turn a fully qualified domain name (fqdn) into a hostname.
|
static Iterator<Integer> |
getPortRangeFromString(String rangeDefinition)
Returns an iterator over available ports defined by the range definition.
|
static String |
getWildcardIPAddress()
Returns the wildcard address to listen on all interfaces.
|
static String |
hostAndPortToUrlString(String host,
int port)
Normalizes and encodes a hostname and port to be included in URL.
|
static String |
ipAddressAndPortToUrlString(InetAddress address,
int port)
Encodes an IP address and port to be included in URL.
|
static String |
ipAddressToUrlString(InetAddress address)
Encodes an IP address properly as a URL string.
|
static boolean |
isValidClientPort(int port)
Check whether the given port is in right range when connecting to somewhere.
|
static boolean |
isValidHostPort(int port)
check whether the given port is in right range when getting port from local system.
|
static InetSocketAddress |
parseHostPortAddress(String hostPort)
Converts a string of the form "host:port" into an
InetSocketAddress. |
static String |
socketAddressToUrlString(InetSocketAddress address)
Encodes an IP address and port to be included in URL.
|
static String |
unresolvedHostAndPortToNormalizedString(String host,
int port)
Returns a valid address for Akka.
|
static String |
unresolvedHostToNormalizedString(String host)
Returns an address in a normalized format for Akka.
|
public static String getHostnameFromFQDN(String fqdn)
fqdn - The fully qualified domain name.public static URL getCorrectHostnamePort(String hostPort)
URL.hostPort - The "host:port" string.public static InetSocketAddress parseHostPortAddress(String hostPort)
InetSocketAddress.hostPort - The "host:port" string.public static Socket acceptWithoutTimeout(ServerSocket serverSocket) throws IOException
ServerSocket.accept() on the provided server socket, suppressing any thrown
SocketTimeoutExceptions. This is a workaround for the underlying JDK-8237858 bug in
JDK 11 that can cause errant SocketTimeoutExceptions to be thrown at unexpected times.
This method expects the provided ServerSocket has no timeout set (SO_TIMEOUT of 0), indicating an infinite timeout. It will suppress all SocketTimeoutExceptions, even if a ServerSocket with a non-zero timeout is passed in.
serverSocket - a ServerSocket with SO_TIMEOUT set to 0;
if SO_TIMEOUT is greater than 0, then this method will suppress SocketTimeoutException;
must not be null; SO_TIMEOUT option must be set to 0IOException - see ServerSocket.accept()public static int getAvailablePort()
public static String unresolvedHostToNormalizedString(String host)
host - The hostname, IPv4 or IPv6 addresspublic static String unresolvedHostAndPortToNormalizedString(String host, int port)
host - The hostname, IPv4 or IPv6 addressport - The portpublic static String ipAddressToUrlString(InetAddress address)
address - The IP address to encode.public static String ipAddressAndPortToUrlString(InetAddress address, int port)
address - The address to be included in the URL.port - The port for the URL address.public static String socketAddressToUrlString(InetSocketAddress address)
address - The socket address with the IP address and port.public static String hostAndPortToUrlString(String host, int port) throws UnknownHostException
host - The address to be included in the URL.port - The port for the URL address.UnknownHostException - Thrown, if the hostname cannot be translated into a
URL.public static Iterator<Integer> getPortRangeFromString(String rangeDefinition) throws NumberFormatException
rangeDefinition - String describing a single port, a range of ports or multiple ranges.NumberFormatException - If an invalid string is passed.public static ServerSocket createSocketFromPorts(Iterator<Integer> portsIterator, NetUtils.SocketFactory factory)
portsIterator - A set of ports to choose from.factory - A factory for creating the SocketServerpublic static String getWildcardIPAddress()
public static boolean isValidClientPort(int port)
port - the port to checkpublic static boolean isValidHostPort(int port)
port - the port to checkCopyright © 2014–2022 The Apache Software Foundation. All rights reserved.