public final class NetUtil extends Object
Modifier and Type | Field and Description |
---|---|
static InetAddress |
LOCALHOST
The
InetAddress that represents the loopback address. |
static Inet4Address |
LOCALHOST4
The
Inet4Address that represents the IPv4 loopback address '127.0.0.1' |
static Inet6Address |
LOCALHOST6
The
Inet6Address that represents the IPv6 loopback address '::1' |
static NetworkInterface |
LOOPBACK_IF
The loopback
NetworkInterface of the current machine |
static Collection<NetworkInterface> |
NETWORK_INTERFACES
An unmodifiable Collection of all the interfaces on this machine.
|
static int |
SOMAXCONN
The SOMAXCONN value of the current machine.
|
Modifier and Type | Method and Description |
---|---|
static String |
bytesToIpAddress(byte[] bytes)
Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.
|
static 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(String ipAddressString)
Creates an byte[] based on an ipAddressString.
|
static InetAddress |
createInetAddressFromIpAddressString(String ipAddressString)
Creates an
InetAddress based on an ipAddressString or might return null if it can't be parsed. |
static Inet6Address |
getByName(CharSequence ip)
Returns the
Inet6Address representation of a CharSequence IP address. |
static Inet6Address |
getByName(CharSequence ip,
boolean ipv4Mapped)
Returns the
Inet6Address representation of a CharSequence IP address. |
static String |
getHostname(InetSocketAddress addr)
Returns
InetSocketAddress.getHostString() if Java >= 7,
or InetSocketAddress.getHostName() otherwise. |
static String |
intToIpAddress(int i)
Converts a 32-bit integer into an IPv4 address.
|
static int |
ipv4AddressToInt(Inet4Address ipAddress)
Convert
Inet4Address into int |
static boolean |
isIpV4StackPreferred()
Returns
true if IPv4 should be used even if the system supports both IPv4 and IPv6. |
static boolean |
isIpV6AddressesPreferred()
Returns
true if an IPv6 address should be preferred when a host has both an IPv4 address and an IPv6
address. |
static boolean |
isValidIpV4Address(CharSequence ip)
Takes a
CharSequence and parses it to see if it is a valid IPV4 address. |
static boolean |
isValidIpV4Address(String ip)
Takes a
String and parses it to see if it is a valid IPV4 address. |
static boolean |
isValidIpV6Address(CharSequence ip) |
static boolean |
isValidIpV6Address(String ip) |
static String |
toAddressString(InetAddress ip)
Returns the
String representation of an InetAddress . |
static String |
toAddressString(InetAddress ip,
boolean ipv4Mapped)
Returns the
String representation of an InetAddress . |
static String |
toSocketAddressString(InetSocketAddress addr)
Returns the
String representation of an InetSocketAddress . |
static String |
toSocketAddressString(String host,
int port)
Returns the
String representation of a host port combo. |
public static final Inet4Address LOCALHOST4
Inet4Address
that represents the IPv4 loopback address '127.0.0.1'public static final Inet6Address LOCALHOST6
Inet6Address
that represents the IPv6 loopback address '::1'public static final InetAddress LOCALHOST
InetAddress
that represents the loopback address. If IPv6 stack is available, it will refer to
LOCALHOST6
. Otherwise, LOCALHOST4
.public static final NetworkInterface LOOPBACK_IF
NetworkInterface
of the current machinepublic static final Collection<NetworkInterface> NETWORK_INTERFACES
public static final int SOMAXCONN
200
is used as a
default value for Windows and 128
for others.public static boolean isIpV4StackPreferred()
true
if IPv4 should be used even if the system supports both IPv4 and IPv6. Setting this
property to true
will disable IPv6 support. The default value of this property is false
.public static boolean isIpV6AddressesPreferred()
true
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 is false
.public static byte[] createByteArrayFromIpAddressString(String ipAddressString)
public static InetAddress createInetAddressFromIpAddressString(String ipAddressString)
InetAddress
based on an ipAddressString or might return null if it can't be parsed.
No error handling is performed here.public static int ipv4AddressToInt(Inet4Address ipAddress)
Inet4Address
into int
public static String intToIpAddress(int i)
public static String bytesToIpAddress(byte[] bytes)
IllegalArgumentException
- if length
is not 4
nor 16
public static String bytesToIpAddress(byte[] bytes, int offset, int length)
IllegalArgumentException
- if length
is not 4
nor 16
public static boolean isValidIpV6Address(String ip)
public static boolean isValidIpV6Address(CharSequence ip)
public static boolean isValidIpV4Address(CharSequence ip)
CharSequence
and parses it to see if it is a valid IPV4 address.public static boolean isValidIpV4Address(String ip)
String
and parses it to see if it is a valid IPV4 address.public static Inet6Address getByName(CharSequence ip)
Inet6Address
representation of a CharSequence
IP address.
This method will treat all IPv4 type addresses as "IPv4 mapped" (see getByName(CharSequence, boolean)
)
ip
- CharSequence
IP address to be converted to a Inet6Address
Inet6Address
representation of the ip
or null
if not a valid IP address.public static Inet6Address getByName(CharSequence ip, boolean ipv4Mapped)
Inet6Address
representation of a CharSequence
IP address.
The ipv4Mapped
parameter specifies how IPv4 addresses should be treated.
"IPv4 mapped" format as
defined in rfc 4291 section 2 is supported.
ip
- CharSequence
IP address to be converted to a Inet6Address
ipv4Mapped
- true
To allow IPv4 mapped inputs to be translated into Inet6Address
false
Consider IPv4 mapped addresses as invalid.Inet6Address
representation of the ip
or null
if not a valid IP address.public static String toSocketAddressString(InetSocketAddress addr)
addr
- InetSocketAddress
to be converted to an address stringString
containing the text-formatted IP addresspublic static String toSocketAddressString(String host, int port)
String
representation of a host port combo.public static String toAddressString(InetAddress ip)
String
representation of an InetAddress
.
InetAddress.getHostAddress()
The output does not include Scope ID.
ip
- InetAddress
to be converted to an address stringString
containing the text-formatted IP addresspublic static String toAddressString(InetAddress ip, boolean ipv4Mapped)
String
representation of an InetAddress
.
InetAddress.getHostAddress()
ipv4Mapped
is false. If ipv4Mapped
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 4The output does not include Scope ID.
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 5952String
containing the text-formatted IP addresspublic static String getHostname(InetSocketAddress addr)
InetSocketAddress.getHostString()
if Java >= 7,
or InetSocketAddress.getHostName()
otherwise.addr
- The addressCopyright © 2008–2024 The Netty Project. All rights reserved.