@LimitedPrivate({"HDFS","MapReduce"})
@Unstable
public class NetUtils
extends java.lang.Object
| Modifier and Type | Field | Description |
|---|---|---|
static java.lang.String |
HADOOP_WIKI |
Base URL of the Hadoop Wiki: "http://wiki.apache.org/hadoop/"
|
static java.lang.String |
UNKNOWN_HOST |
text included in wrapped exceptions if the host is null: "(unknown)"
|
| Constructor | Description |
|---|---|
NetUtils() |
| Modifier and Type | Method | Description |
|---|---|---|
static void |
addStaticResolution(java.lang.String host,
java.lang.String resolvedName) |
Adds a static resolution for host.
|
static java.net.InetAddress |
bindToLocalAddress(java.net.InetAddress localAddr,
boolean bindWildCardAddress) |
Return an @
InetAddress to bind to. |
static void |
connect(java.net.Socket socket,
java.net.SocketAddress address,
int timeout) |
This is a drop-in replacement for
Socket.connect(SocketAddress, int). |
static void |
connect(java.net.Socket socket,
java.net.SocketAddress endpoint,
java.net.SocketAddress localAddr,
int timeout) |
Like
connect(Socket, SocketAddress, int) but
also takes a local address and port to bind the socket to. |
static java.net.InetSocketAddress |
createSocketAddr(java.lang.String target) |
Util method to build socket addr from either.
|
static java.net.InetSocketAddress |
createSocketAddr(java.lang.String target,
int defaultPort) |
Util method to build socket addr from either.
|
static java.net.InetSocketAddress |
createSocketAddr(java.lang.String target,
int defaultPort,
java.lang.String configName) |
Create an InetSocketAddress from the given target string and
default port.
|
static java.net.InetSocketAddress |
createSocketAddr(java.lang.String target,
int defaultPort,
java.lang.String configName,
boolean useCacheIfPresent) |
Create an InetSocketAddress from the given target string and
default port.
|
static java.net.InetSocketAddress |
createSocketAddr(java.lang.String target,
int defaultPort,
java.lang.String configName,
boolean useCacheIfPresent,
boolean isResolved) |
|
static java.net.InetSocketAddress |
createSocketAddrForHost(java.lang.String host,
int port) |
Create a socket address with the given host and port.
|
static java.net.InetSocketAddress |
createSocketAddrUnresolved(java.lang.String target) |
|
static java.util.List<java.lang.String[]> |
getAllStaticResolutions() |
This is used to get all the resolutions that were added using
addStaticResolution(String, String). |
static java.net.URI |
getCanonicalUri(java.net.URI uri,
int defaultPort) |
Resolve the uri's hostname and add the default port if not in the uri
|
static java.net.InetSocketAddress |
getConnectAddress(java.net.InetSocketAddress addr) |
Returns an InetSocketAddress that a client can use to connect to the
given listening address.
|
static java.net.InetSocketAddress |
getConnectAddress(Server server) |
Returns InetSocketAddress that a client can use to
connect to the server.
|
static javax.net.SocketFactory |
getDefaultSocketFactory(Configuration conf) |
Get the default socket factory as specified by the configuration
parameter hadoop.rpc.socket.factory.default
|
static int |
getFreeSocketPort() |
Return a free port number.
|
static java.util.Set<java.lang.Integer> |
getFreeSocketPorts(int numOfPorts) |
Return free ports.
|
static java.lang.String |
getHostname() |
Return hostname without throwing exception.
|
static java.lang.String |
getHostNameOfIP(java.lang.String ipPort) |
Attempt to obtain the host name of the given string which contains
an IP address and an optional port.
|
static java.lang.String |
getHostPortString(java.net.InetSocketAddress addr) |
Compose a "host:port" string from the address.
|
static SocketInputWrapper |
getInputStream(java.net.Socket socket) |
Same as
getInputStream(socket, socket.getSoTimeout()). |
static SocketInputWrapper |
getInputStream(java.net.Socket socket,
long timeout) |
Return a
SocketInputWrapper for the socket and set the given
timeout. |
static java.util.List<java.net.InetAddress> |
getIPs(java.lang.String subnet,
boolean returnSubinterfaces) |
Return an InetAddress for each interface that matches the
given subnet specified using CIDR notation.
|
static java.lang.String |
getLocalHostname() |
Return hostname without throwing exception.
|
static java.net.InetAddress |
getLocalInetAddress(java.lang.String host) |
Checks if
host is a local host name and return InetAddress
corresponding to that address. |
static java.io.OutputStream |
getOutputStream(java.net.Socket socket) |
Same as getOutputStream(socket, 0).
|
static java.io.OutputStream |
getOutputStream(java.net.Socket socket,
long timeout) |
Returns OutputStream for the socket.
|
static int |
getPortFromHostPortString(java.lang.String addr) |
Get port as integer from host port string like host:port.
|
static javax.net.SocketFactory |
getSocketFactory(Configuration conf,
java.lang.Class<?> clazz) |
Get the socket factory for the given class according to its
configuration parameter
hadoop.rpc.socket.factory.class.<ClassName>.
|
static javax.net.SocketFactory |
getSocketFactoryFromProperty(Configuration conf,
java.lang.String propValue) |
Get the socket factory corresponding to the given proxy URI.
|
static java.lang.String |
getStaticResolution(java.lang.String host) |
Retrieves the resolved name for the passed host.
|
static boolean |
isLocalAddress(java.net.InetAddress addr) |
Given an InetAddress, checks to see if the address is a local address, by
comparing the address with all the interfaces on the node.
|
static boolean |
isValidSubnet(java.lang.String subnet) |
isValidSubnet.
|
static java.lang.String |
normalizeHostName(java.lang.String name) |
Given a string representation of a host, return its ip address
in textual presentation.
|
static java.util.List<java.lang.String> |
normalizeHostNames(java.util.Collection<java.lang.String> names) |
Given a collection of string representation of hosts, return a list of
corresponding IP addresses in the textual representation.
|
static java.lang.String |
normalizeIP2HostName(java.lang.String ipPort) |
Attempt to normalize the given string to "host:port"
if it like "ip:port".
|
static void |
verifyHostnames(java.lang.String[] names) |
Performs a sanity check on the list of hostnames/IPs to verify they at least
appear to be valid.
|
static java.io.IOException |
wrapException(java.lang.String destHost,
int destPort,
java.lang.String localHost,
int localPort,
java.io.IOException exception) |
Take an IOException , the local host port and remote host port details and
return an IOException with the input exception as the cause and also
include the host details.
|
public static final java.lang.String UNKNOWN_HOST
public static final java.lang.String HADOOP_WIKI
public static javax.net.SocketFactory getSocketFactory(Configuration conf, java.lang.Class<?> clazz)
conf - the configurationclazz - the class (usually a VersionedProtocol)public static javax.net.SocketFactory getDefaultSocketFactory(Configuration conf)
conf - the configurationpublic static javax.net.SocketFactory getSocketFactoryFromProperty(Configuration conf, java.lang.String propValue)
conf - configuration.propValue - the property which is the class name of the
SocketFactory to instantiate; assumed non null and non empty.public static java.net.InetSocketAddress createSocketAddr(java.lang.String target)
target - target.public static java.net.InetSocketAddress createSocketAddrUnresolved(java.lang.String target)
public static java.net.InetSocketAddress createSocketAddr(java.lang.String target,
int defaultPort)
target - target.defaultPort - default port.public static java.net.InetSocketAddress createSocketAddr(java.lang.String target,
int defaultPort,
java.lang.String configName)
configName parameter is used as part of the
exception message, allowing the user to better diagnose
the misconfiguration.target - a string of either "host" or "host:port"defaultPort - the default port if target does not
include a port numberconfigName - the name of the configuration from which
target was loaded. This is used in the
exception message in the case that parsing fails.public static java.net.InetSocketAddress createSocketAddr(java.lang.String target,
int defaultPort,
java.lang.String configName,
boolean useCacheIfPresent)
configName parameter is used as part of the
exception message, allowing the user to better diagnose
the misconfiguration.target - a string of either "host" or "host:port"defaultPort - the default port if target does not
include a port numberconfigName - the name of the configuration from which
target was loaded. This is used in the
exception message in the case that parsing fails.useCacheIfPresent - Whether use cache when create URIpublic static java.net.InetSocketAddress createSocketAddr(java.lang.String target,
int defaultPort,
java.lang.String configName,
boolean useCacheIfPresent,
boolean isResolved)
public static java.net.InetSocketAddress createSocketAddrForHost(java.lang.String host,
int port)
addStaticResolution(String, String). The value of
hadoop.security.token.service.use_ip will determine whether the
standard java host resolver is used, or if the fully qualified resolver
is used.host - the hostname or IP use to instantiate the objectport - the port numberpublic static java.net.URI getCanonicalUri(java.net.URI uri,
int defaultPort)
uri - to resolvedefaultPort - if none is givenpublic static void addStaticResolution(java.lang.String host,
java.lang.String resolvedName)
getStaticResolution(String) can be used to query for
the actual hostname.host - the hostname or IP use to instantiate the object.resolvedName - resolved name.public static java.lang.String getStaticResolution(java.lang.String host)
addStaticResolution(String, String)host - the hostname or IP use to instantiate the object.public static java.util.List<java.lang.String[]> getAllStaticResolutions()
addStaticResolution(String, String). The return
value is a List each element of which contains an array of String
of the form String[0]=hostname, String[1]=resolved-hostnamepublic static java.net.InetSocketAddress getConnectAddress(Server server)
server - server.public static java.net.InetSocketAddress getConnectAddress(java.net.InetSocketAddress addr)
addr - of a listenerpublic static SocketInputWrapper getInputStream(java.net.Socket socket) throws java.io.IOException
getInputStream(socket, socket.getSoTimeout()).socket - socket.java.io.IOException - raised on errors performing I/O.getInputStream(Socket, long)public static SocketInputWrapper getInputStream(java.net.Socket socket, long timeout) throws java.io.IOException
SocketInputWrapper for the socket and set the given
timeout. If the socket does not have an associated channel, then its socket
timeout will be set to the specified value. Otherwise, a
SocketInputStream will be created which reads with the configured
timeout.
Any socket created using socket factories returned by NetUtils(),
must use this interface instead of Socket.getInputStream().
In general, this should be called only once on each socket: see the note
in SocketInputWrapper.setTimeout(long) for more information.socket - socket.timeout - timeout in milliseconds. zero for waiting as
long as necessary.java.io.IOException - raised on errors performing I/O.Socket.getChannel()public static java.io.OutputStream getOutputStream(java.net.Socket socket)
throws java.io.IOException
getOutputStream(Socket, long) : SocketOutputStream with the given timeout. If the socket does not
have a channel, Socket.getOutputStream() is returned. In the later
case, the timeout argument is ignored and the write will wait until
data is available.NetUtils,
must use this interface instead of Socket.getOutputStream().socket - socket.java.io.IOException - raised on errors performing I/O.getOutputStream(Socket, long)public static java.io.OutputStream getOutputStream(java.net.Socket socket,
long timeout)
throws java.io.IOException
SocketOutputStream with the given timeout. If the socket does not
have a channel, Socket.getOutputStream() is returned. In the later
case, the timeout argument is ignored and the write will wait until
data is available.NetUtils,
must use this interface instead of Socket.getOutputStream().socket - socket.timeout - timeout in milliseconds. This may not always apply. zero
for waiting as long as necessary.java.io.IOException - raised on errors performing I/O.Socket.getChannel()public static void connect(java.net.Socket socket,
java.net.SocketAddress address,
int timeout)
throws java.io.IOException
Socket.connect(SocketAddress, int).
In the case of normal sockets that don't have associated channels, this
just invokes socket.connect(endpoint, timeout). If
socket.getChannel() returns a non-null channel,
connect is implemented using Hadoop's selectors. This is done mainly
to avoid Sun's connect implementation from creating thread-local
selectors, since Hadoop does not have control on when these are closed
and could end up taking all the available file descriptors.socket - socket.address - the remote addresstimeout - timeout in millisecondsjava.io.IOException - raised on errors performing I/O.Socket.connect(java.net.SocketAddress, int)public static void connect(java.net.Socket socket,
java.net.SocketAddress endpoint,
java.net.SocketAddress localAddr,
int timeout)
throws java.io.IOException
connect(Socket, SocketAddress, int) but
also takes a local address and port to bind the socket to.socket - socket.endpoint - the remote addresslocalAddr - the local address to bind the socket totimeout - timeout in millisecondsjava.io.IOException - raised on errors performing I/O.public static java.lang.String normalizeHostName(java.lang.String name)
name - a string representation of a host:
either a textual representation its IP address or its host namepublic static java.util.List<java.lang.String> normalizeHostNames(java.util.Collection<java.lang.String> names)
names - a collection of string representations of hostsnormalizeHostName(String)public static void verifyHostnames(java.lang.String[] names)
throws java.net.UnknownHostException
names - - List of hostnames/IPsjava.net.UnknownHostException - Unknown Host Exception.public static java.lang.String getHostNameOfIP(java.lang.String ipPort)
ipPort - string of form ip[:port]public static java.lang.String normalizeIP2HostName(java.lang.String ipPort)
ipPort - maybe lik ip:port or host:port.public static java.lang.String getLocalHostname()
public static java.lang.String getHostname()
public static java.lang.String getHostPortString(java.net.InetSocketAddress addr)
addr - address.public static int getPortFromHostPortString(java.lang.String addr)
throws java.lang.IllegalArgumentException
addr - host + port string like host:port.java.lang.IllegalArgumentException - if the input is not in the correct format.public static java.net.InetAddress getLocalInetAddress(java.lang.String host)
throws java.net.SocketException
host is a local host name and return InetAddress
corresponding to that address.host - the specified hostInetAddress or nulljava.net.SocketException - if an I/O error occurspublic static boolean isLocalAddress(java.net.InetAddress addr)
addr - address to check if it is local node's addresspublic static java.io.IOException wrapException(java.lang.String destHost,
int destPort,
java.lang.String localHost,
int localPort,
java.io.IOException exception)
destHost - target host (nullable)destPort - target portlocalHost - local host (nullable)localPort - local portexception - the caught exception.public static boolean isValidSubnet(java.lang.String subnet)
subnet - subnet.public static java.util.List<java.net.InetAddress> getIPs(java.lang.String subnet,
boolean returnSubinterfaces)
subnet - subnet specified using CIDR notationreturnSubinterfaces - whether to return IPs associated with subinterfacesjava.lang.IllegalArgumentException - if subnet is invalidpublic static int getFreeSocketPort()
public static java.util.Set<java.lang.Integer> getFreeSocketPorts(int numOfPorts)
numOfPorts. Num of ports
provided in the argument should not exceed 25.numOfPorts - Number of free ports to acquire.public static java.net.InetAddress bindToLocalAddress(java.net.InetAddress localAddr,
boolean bindWildCardAddress)
InetAddress to bind to. If bindWildCardAddress is true
than returns null.localAddr - local addr.bindWildCardAddress - bind wildcard address.Copyright © 2008–2025 Apache Software Foundation. All rights reserved.