- java.lang.Object
-
- io.netty5.util.NetUtil
-
public final class NetUtil extends 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 InetAddressLOCALHOSTTheInetAddressthat represents the loopback address.static Inet4AddressLOCALHOST4TheInet4Addressthat represents the IPv4 loopback address '127.0.0.1'static Inet6AddressLOCALHOST6TheInet6Addressthat represents the IPv6 loopback address '::1'static NetworkInterfaceLOOPBACK_IFThe loopbackNetworkInterfaceof the current machinestatic intSOMAXCONNThe SOMAXCONN value of the current machine.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Class<? extends InetAddress>addressType(ProtocolFamily family)Returns the address type for the givenProtocolFamily.static StringbytesToIpAddress(byte[] bytes)Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.static StringbytesToIpAddress(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 InetAddresscreateInetAddressFromIpAddressString(String ipAddressString)Creates anInetAddressbased on an ipAddressString or might return null if it can't be parsed.static ProtocolFamilyfamily(Class<? extends InetAddress> addressType)Returns theProtocolFamilyfor the given address type.static ProtocolFamilyfamily(InetAddress address)Returns theProtocolFamilyfor the givenInetAddress.static Inet6AddressgetByName(CharSequence ip)Returns theInet6Addressrepresentation of aCharSequenceIP address.static Inet6AddressgetByName(CharSequence ip, boolean ipv4Mapped)Returns theInet6Addressrepresentation of aCharSequenceIP address.static StringgetHostname(InetSocketAddress addr)ReturnsInetSocketAddress.getHostString()if Java >= 7, orInetSocketAddress.getHostName()otherwise.static StringintToIpAddress(int i)Converts a 32-bit integer into an IPv4 address.static intipv4AddressToInt(Inet4Address ipAddress)ConvertInet4Addressintointstatic booleanisFamilySupported(Class<? extends InetAddress> addressType, ProtocolFamily family)Returnstrueif theProtocolFamilyis supported by the given address type.static booleanisFamilySupported(InetAddress address, ProtocolFamily family)static booleanisIpV4StackPreferred()Returnstrueif IPv4 should be used even if the system supports both IPv4 and IPv6.static booleanisIpV6AddressesPreferred()Returnstrueif an IPv6 address should be preferred when a host has both an IPv4 address and an IPv6 address.static booleanisValidIpV4Address(CharSequence ip)Takes aCharSequenceand parses it to see if it is a valid IPV4 address.static booleanisValidIpV4Address(String ip)Takes aStringand parses it to see if it is a valid IPV4 address.static booleanisValidIpV6Address(CharSequence ip)static booleanisValidIpV6Address(String ip)static InetAddresslocalHost(ProtocolFamily family)Returns the localhostInetAddressfor this family.static StringtoAddressString(InetAddress ip)Returns theStringrepresentation of anInetAddress.static StringtoAddressString(InetAddress ip, boolean ipv4Mapped)Returns theStringrepresentation of anInetAddress.static StringtoSocketAddressString(String host, int port)Returns theStringrepresentation of a host port combo.static StringtoSocketAddressString(InetSocketAddress addr)Returns theStringrepresentation of anInetSocketAddress.
-
-
-
Field Detail
-
LOCALHOST4
public static final Inet4Address LOCALHOST4
TheInet4Addressthat represents the IPv4 loopback address '127.0.0.1'
-
LOCALHOST6
public static final Inet6Address LOCALHOST6
TheInet6Addressthat represents the IPv6 loopback address '::1'
-
LOCALHOST
public static final InetAddress LOCALHOST
TheInetAddressthat represents the loopback address. If IPv6 stack is available, it will refer toLOCALHOST6. Otherwise,LOCALHOST4.
-
LOOPBACK_IF
public static final NetworkInterface LOOPBACK_IF
The loopbackNetworkInterfaceof the current machine
-
SOMAXCONN
public static final int SOMAXCONN
The SOMAXCONN value of the current machine. If failed to get the value,200is used as a default value for Windows and128for others.
-
-
Method Detail
-
isIpV4StackPreferred
public static boolean isIpV4StackPreferred()
Returnstrueif IPv4 should be used even if the system supports both IPv4 and IPv6. Setting this property totruewill disable IPv6 support. The default value of this property isfalse.- See Also:
- Java SE networking properties
-
isIpV6AddressesPreferred
public static boolean isIpV6AddressesPreferred()
Returnstrueif 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(String ipAddressString)
Creates an byte[] based on an ipAddressString. No error handling is performed here.
-
createInetAddressFromIpAddressString
public static InetAddress createInetAddressFromIpAddressString(String ipAddressString)
Creates anInetAddressbased on an ipAddressString or might return null if it can't be parsed. No error handling is performed here.
-
ipv4AddressToInt
public static int ipv4AddressToInt(Inet4Address ipAddress)
ConvertInet4Addressintoint
-
intToIpAddress
public static String intToIpAddress(int i)
Converts a 32-bit integer into an IPv4 address.
-
bytesToIpAddress
public static String bytesToIpAddress(byte[] bytes)
Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.- Throws:
IllegalArgumentException- iflengthis not4nor16
-
bytesToIpAddress
public static String bytesToIpAddress(byte[] bytes, int offset, int length)
Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.- Throws:
IllegalArgumentException- iflengthis not4nor16
-
isValidIpV6Address
public static boolean isValidIpV6Address(String ip)
-
isValidIpV6Address
public static boolean isValidIpV6Address(CharSequence ip)
-
isValidIpV4Address
public static boolean isValidIpV4Address(CharSequence ip)
Takes aCharSequenceand 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(String ip)
Takes aStringand 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 Inet6Address getByName(CharSequence ip)
Returns theInet6Addressrepresentation of aCharSequenceIP address.This method will treat all IPv4 type addresses as "IPv4 mapped" (see
getByName(CharSequence, boolean))- Parameters:
ip-CharSequenceIP address to be converted to aInet6Address- Returns:
Inet6Addressrepresentation of theipornullif not a valid IP address.
-
getByName
public static Inet6Address getByName(CharSequence ip, boolean ipv4Mapped)
Returns theInet6Addressrepresentation of aCharSequenceIP address.The
ipv4Mappedparameter specifies how IPv4 addresses should be treated. "IPv4 mapped" format as defined in rfc 4291 section 2 is supported.- Parameters:
ip-CharSequenceIP address to be converted to aInet6Addressipv4Mapped-trueTo allow IPv4 mapped inputs to be translated intoInet6AddressfalseConsider IPv4 mapped addresses as invalid.
- Returns:
Inet6Addressrepresentation of theipornullif not a valid IP address.
-
toSocketAddressString
public static String toSocketAddressString(InetSocketAddress addr)
- Parameters:
addr-InetSocketAddressto be converted to an address string- Returns:
Stringcontaining the text-formatted IP address
-
toSocketAddressString
public static String toSocketAddressString(String host, int port)
Returns theStringrepresentation of a host port combo.
-
toAddressString
public static String toAddressString(InetAddress ip)
Returns theStringrepresentation 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-InetAddressto be converted to an address string- Returns:
Stringcontaining the text-formatted IP address
- Inet4Address results are identical to
-
toAddressString
public static String toAddressString(InetAddress ip, boolean ipv4Mapped)
Returns theStringrepresentation of anInetAddress.- Inet4Address results are identical to
InetAddress.getHostAddress() - Inet6Address results adhere to
rfc 5952 section 4 if
ipv4Mappedis false. Ifipv4Mappedis 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-InetAddressto be converted to an address stringipv4Mapped-trueto 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 4falseto strictly follow rfc 5952
- Returns:
Stringcontaining the text-formatted IP address
- Inet4Address results are identical to
-
getHostname
public static String getHostname(InetSocketAddress addr)
ReturnsInetSocketAddress.getHostString()if Java >= 7, orInetSocketAddress.getHostName()otherwise.- Parameters:
addr- The address- Returns:
- the host string
-
isFamilySupported
public static boolean isFamilySupported(InetAddress address, ProtocolFamily family)
- Parameters:
address- the address.family- the family.- Returns:
trueif supported,falseotherwise.
-
isFamilySupported
public static boolean isFamilySupported(Class<? extends InetAddress> addressType, ProtocolFamily family)
Returnstrueif theProtocolFamilyis supported by the given address type.- Parameters:
addressType- the address type.family- the family.- Returns:
trueif supported,falseotherwise.
-
addressType
public static Class<? extends InetAddress> addressType(ProtocolFamily family)
Returns the address type for the givenProtocolFamily.- Parameters:
family- the family.- Returns:
- the address type or
nullif not supported.
-
family
public static ProtocolFamily family(InetAddress address)
Returns theProtocolFamilyfor the givenInetAddress.- Parameters:
address- the address.- Returns:
- the
ProtocolFamilyornullif not supported.
-
family
public static ProtocolFamily family(Class<? extends InetAddress> addressType)
Returns theProtocolFamilyfor the given address type.- Parameters:
addressType- the address type.- Returns:
- the
ProtocolFamilyornullif not supported.
-
localHost
public static InetAddress localHost(ProtocolFamily family)
Returns the localhostInetAddressfor this family.- Parameters:
family- the family.- Returns:
- localhost address.
-
-