Package io.netty.util
Class NetUtil
- java.lang.Object
-
- io.netty.util.NetUtil
-
public final class NetUtil extends java.lang.Object
A class that holds a number of network-related constants. This class borrowed some of its methods from a modified fork of the Inet6Util class which was part of Apache Harmony.
-
-
Field Summary
Fields Modifier and Type Field Description static java.net.InetAddress
LOCALHOST
TheInetAddress
that represents the loopback address.static java.net.Inet4Address
LOCALHOST4
TheInet4Address
that represents the IPv4 loopback address '127.0.0.1'static java.net.Inet6Address
LOCALHOST6
TheInet6Address
that represents the IPv6 loopback address '::1'static java.net.NetworkInterface
LOOPBACK_IF
The loopbackNetworkInterface
of the current machinestatic java.util.Collection<java.net.NetworkInterface>
NETWORK_INTERFACES
An unmodifiable Collection of all the interfaces on this machine.static int
SOMAXCONN
The SOMAXCONN value of the current machine.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
bytesToIpAddress(byte[] bytes)
Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.static java.lang.String
bytesToIpAddress(byte[] bytes, int offset, int length)
Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.static byte[]
createByteArrayFromIpAddressString(java.lang.String ipAddressString)
Creates an byte[] based on an ipAddressString.static java.net.InetAddress
createInetAddressFromIpAddressString(java.lang.String ipAddressString)
Creates anInetAddress
based on an ipAddressString or might return null if it can't be parsed.static java.net.Inet6Address
getByName(java.lang.CharSequence ip)
Returns theInet6Address
representation of aCharSequence
IP address.static java.net.Inet6Address
getByName(java.lang.CharSequence ip, boolean ipv4Mapped)
Returns theInet6Address
representation of aCharSequence
IP address.static java.lang.String
getHostname(java.net.InetSocketAddress addr)
ReturnsInetSocketAddress.getHostString()
.static java.lang.String
intToIpAddress(int i)
Converts a 32-bit integer into an IPv4 address.static int
ipv4AddressToInt(java.net.Inet4Address ipAddress)
ConvertInet4Address
intoint
static boolean
isIpV4StackPreferred()
Returnstrue
if IPv4 should be used even if the system supports both IPv4 and IPv6.static boolean
isIpV6AddressesPreferred()
Returnstrue
if an IPv6 address should be preferred when a host has both an IPv4 address and an IPv6 address.static boolean
isValidIpV4Address(java.lang.CharSequence ip)
Takes aCharSequence
and parses it to see if it is a valid IPV4 address.static boolean
isValidIpV4Address(java.lang.String ip)
Takes aString
and parses it to see if it is a valid IPV4 address.static boolean
isValidIpV6Address(java.lang.CharSequence ip)
static boolean
isValidIpV6Address(java.lang.String ip)
static java.lang.String
toAddressString(java.net.InetAddress ip)
Returns theString
representation of anInetAddress
.static java.lang.String
toAddressString(java.net.InetAddress ip, boolean ipv4Mapped)
Returns theString
representation of anInetAddress
.static java.lang.String
toSocketAddressString(java.lang.String host, int port)
Returns theString
representation of a host port combo.static java.lang.String
toSocketAddressString(java.net.InetSocketAddress addr)
Returns theString
representation of anInetSocketAddress
.
-
-
-
Field Detail
-
LOCALHOST4
public static final java.net.Inet4Address LOCALHOST4
TheInet4Address
that represents the IPv4 loopback address '127.0.0.1'
-
LOCALHOST6
public static final java.net.Inet6Address LOCALHOST6
TheInet6Address
that represents the IPv6 loopback address '::1'
-
LOCALHOST
public static final java.net.InetAddress LOCALHOST
TheInetAddress
that represents the loopback address. If IPv6 stack is available, it will refer toLOCALHOST6
. Otherwise,LOCALHOST4
.
-
LOOPBACK_IF
public static final java.net.NetworkInterface LOOPBACK_IF
The loopbackNetworkInterface
of the current machine
-
NETWORK_INTERFACES
public static final java.util.Collection<java.net.NetworkInterface> NETWORK_INTERFACES
An unmodifiable Collection of all the interfaces on this machine.
-
SOMAXCONN
public static final int SOMAXCONN
The SOMAXCONN value of the current machine. If failed to get the value,200
is used as a default value for Windows and128
for others.
-
-
Method Detail
-
isIpV4StackPreferred
public static boolean isIpV4StackPreferred()
Returnstrue
if IPv4 should be used even if the system supports both IPv4 and IPv6. Setting this property totrue
will disable IPv6 support. The default value of this property isfalse
.- See Also:
- Java SE networking properties
-
isIpV6AddressesPreferred
public static boolean isIpV6AddressesPreferred()
Returnstrue
if an IPv6 address should be preferred when a host has both an IPv4 address and an IPv6 address. The default value of this property isfalse
.- See Also:
- Java SE networking properties
-
createByteArrayFromIpAddressString
public static byte[] createByteArrayFromIpAddressString(java.lang.String ipAddressString)
Creates an byte[] based on an ipAddressString. No error handling is performed here.
-
createInetAddressFromIpAddressString
public static java.net.InetAddress createInetAddressFromIpAddressString(java.lang.String ipAddressString)
Creates anInetAddress
based on an ipAddressString or might return null if it can't be parsed. No error handling is performed here.
-
ipv4AddressToInt
public static int ipv4AddressToInt(java.net.Inet4Address ipAddress)
ConvertInet4Address
intoint
-
intToIpAddress
public static java.lang.String intToIpAddress(int i)
Converts a 32-bit integer into an IPv4 address.
-
bytesToIpAddress
public static java.lang.String bytesToIpAddress(byte[] bytes)
Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.- Throws:
java.lang.IllegalArgumentException
- iflength
is not4
nor16
-
bytesToIpAddress
public static java.lang.String bytesToIpAddress(byte[] bytes, int offset, int length)
Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.- Throws:
java.lang.IllegalArgumentException
- iflength
is not4
nor16
-
isValidIpV6Address
public static boolean isValidIpV6Address(java.lang.String ip)
-
isValidIpV6Address
public static boolean isValidIpV6Address(java.lang.CharSequence ip)
-
isValidIpV4Address
public static boolean isValidIpV4Address(java.lang.CharSequence ip)
Takes aCharSequence
and parses it to see if it is a valid IPV4 address.- Returns:
- true, if the string represents an IPV4 address in dotted notation, false otherwise
-
isValidIpV4Address
public static boolean isValidIpV4Address(java.lang.String ip)
Takes aString
and parses it to see if it is a valid IPV4 address.- Returns:
- true, if the string represents an IPV4 address in dotted notation, false otherwise
-
getByName
public static java.net.Inet6Address getByName(java.lang.CharSequence ip)
Returns theInet6Address
representation of aCharSequence
IP address.This method will treat all IPv4 type addresses as "IPv4 mapped" (see
getByName(CharSequence, boolean)
)- Parameters:
ip
-CharSequence
IP address to be converted to aInet6Address
- Returns:
Inet6Address
representation of theip
ornull
if not a valid IP address.
-
getByName
public static java.net.Inet6Address getByName(java.lang.CharSequence ip, boolean ipv4Mapped)
Returns theInet6Address
representation of aCharSequence
IP address.The
ipv4Mapped
parameter specifies how IPv4 addresses should be treated. "IPv4 mapped" format as defined in rfc 4291 section 2 is supported.- Parameters:
ip
-CharSequence
IP address to be converted to aInet6Address
ipv4Mapped
-true
To allow IPv4 mapped inputs to be translated intoInet6Address
false
Consider IPv4 mapped addresses as invalid.
- Returns:
Inet6Address
representation of theip
ornull
if not a valid IP address.
-
toSocketAddressString
public static java.lang.String toSocketAddressString(java.net.InetSocketAddress addr)
Returns theString
representation of anInetSocketAddress
.The output does not include Scope ID.
- Parameters:
addr
-InetSocketAddress
to be converted to an address string- Returns:
String
containing the text-formatted IP address
-
toSocketAddressString
public static java.lang.String toSocketAddressString(java.lang.String host, int port)
Returns theString
representation of a host port combo.
-
toAddressString
public static java.lang.String toAddressString(java.net.InetAddress ip)
Returns theString
representation of anInetAddress
.- Inet4Address results are identical to
InetAddress.getHostAddress()
- Inet6Address results adhere to rfc 5952 section 4
The output does not include Scope ID.
- Parameters:
ip
-InetAddress
to be converted to an address string- Returns:
String
containing the text-formatted IP address
- Inet4Address results are identical to
-
toAddressString
public static java.lang.String toAddressString(java.net.InetAddress ip, boolean ipv4Mapped)
Returns theString
representation of anInetAddress
.- Inet4Address results are identical to
InetAddress.getHostAddress()
- Inet6Address results adhere to
rfc 5952 section 4 if
ipv4Mapped
is false. Ifipv4Mapped
is true then "IPv4 mapped" format from rfc 4291 section 2 will be supported. The compressed result will always obey the compression rules defined in rfc 5952 section 4
The output does not include Scope ID.
- Parameters:
ip
-InetAddress
to be converted to an address stringipv4Mapped
-true
to stray from strict rfc 5952 and support the "IPv4 mapped" format defined in rfc 4291 section 2 while still following the updated guidelines in rfc 5952 section 4false
to strictly follow rfc 5952
- Returns:
String
containing the text-formatted IP address
- Inet4Address results are identical to
-
getHostname
public static java.lang.String getHostname(java.net.InetSocketAddress addr)
ReturnsInetSocketAddress.getHostString()
.- Parameters:
addr
- The address- Returns:
- the host string
-
-