public final class NetUtil
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
private static int |
IPV4_MAX_CHAR_BETWEEN_SEPARATOR
Maximum amount of value adding characters in between IPV4 separators
|
private static boolean |
IPV4_PREFERRED
true if IPv4 should be used even if the system supports both IPv4 and IPv6. |
private static int |
IPV4_SEPARATORS
Number of separators that must be present in an IPv4 string
|
private static boolean |
IPV6_ADDRESSES_PREFERRED
true if an IPv6 address should be preferred when a host has both an IPv4 address and an IPv6 address. |
private static int |
IPV6_BYTE_COUNT
Number of bytes needed to represent and IPV6 value
|
private static int |
IPV6_MAX_CHAR_BETWEEN_SEPARATOR
Maximum amount of value adding characters in between IPV6 separators
|
private static int |
IPV6_MAX_CHAR_COUNT
The maximum number of characters for an IPV6 string with no scope
|
private static int |
IPV6_MAX_SEPARATORS
Maximum number of separators that must be present in an IPv6 string
|
private static int |
IPV6_MIN_SEPARATORS
Minimum number of separators that must be present in an IPv6 string
|
private static int |
IPV6_WORD_COUNT
This defines how many words (represented as ints) are needed to represent an IPv6 address
|
static java.net.InetAddress |
LOCALHOST
The
InetAddress that represents the loopback address. |
static java.net.Inet4Address |
LOCALHOST4
The
Inet4Address that represents the IPv4 loopback address '127.0.0.1' |
static java.net.Inet6Address |
LOCALHOST6
The
Inet6Address that represents the IPv6 loopback address '::1' |
private static InternalLogger |
logger
The logger being used by this class
|
static java.net.NetworkInterface |
LOOPBACK_IF
The loopback
NetworkInterface of the current machine |
static int |
SOMAXCONN
The SOMAXCONN value of the current machine.
|
Modifier | Constructor and Description |
---|---|
private |
NetUtil()
A constructor to stop this class being constructed.
|
Modifier and Type | Method and 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.
|
private static int |
decimalDigit(java.lang.String str,
int pos) |
static java.net.Inet6Address |
getByName(java.lang.CharSequence ip)
Returns the
Inet6Address representation of a CharSequence IP address. |
static java.net.Inet6Address |
getByName(java.lang.CharSequence ip,
boolean ipv4Mapped)
Returns the
Inet6Address representation of a CharSequence IP address. |
private static byte[] |
getIPv6ByName(java.lang.CharSequence ip,
boolean ipv4Mapped)
Returns the byte array representation of a
CharSequence IP address. |
private static boolean |
inRangeEndExclusive(int value,
int start,
int end)
Does a range check on
value if is within start (inclusive) and end (exclusive). |
static java.lang.String |
intToIpAddress(int i)
Converts a 32-bit integer into an IPv4 address.
|
private static byte |
ipv4WordToByte(java.lang.String ip,
int from,
int toExclusive) |
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. |
private static boolean |
isValidHexChar(char c) |
static boolean |
isValidIpV4Address(java.lang.String ip)
Takes a string and parses it to see if it is a valid IPV4 address.
|
private static boolean |
isValidIpV4Address(java.lang.String ip,
int from,
int toExcluded) |
private static boolean |
isValidIPv4Mapped(byte[] bytes,
int currentIndex,
int compressBegin,
int compressLength) |
private static boolean |
isValidIPv4MappedChar(char c) |
private static boolean |
isValidIPv4MappedSeparators(byte b0,
byte b1,
boolean mustBeZero) |
private static boolean |
isValidIpV4Word(java.lang.CharSequence word,
int from,
int toExclusive) |
static boolean |
isValidIpV6Address(java.lang.String ip) |
private static boolean |
isValidNumericChar(char c) |
private static java.lang.StringBuilder |
newSocketAddressStringBuilder(java.lang.String host,
java.lang.String port,
boolean ipv4) |
private static java.lang.Integer |
sysctlGetInt(java.lang.String sysctlKey)
This will execute sysctl with the
sysctlKey
which is expected to return the numeric value for for sysctlKey . |
private static java.lang.String |
toAddressString(byte[] bytes,
int offset,
boolean ipv4Mapped) |
static java.lang.String |
toAddressString(java.net.InetAddress ip)
Returns the
String representation of an InetAddress . |
static java.lang.String |
toAddressString(java.net.InetAddress ip,
boolean ipv4Mapped)
Returns the
String representation of an InetAddress . |
static java.lang.String |
toSocketAddressString(java.net.InetSocketAddress addr)
Returns the
String representation of an InetSocketAddress . |
static java.lang.String |
toSocketAddressString(java.lang.String host,
int port)
Returns the
String representation of a host port combo. |
(package private) static byte[] |
validIpV4ToBytes(java.lang.String ip) |
public static final java.net.Inet4Address LOCALHOST4
Inet4Address
that represents the IPv4 loopback address '127.0.0.1'public static final java.net.Inet6Address LOCALHOST6
Inet6Address
that represents the IPv6 loopback address '::1'public static final java.net.InetAddress LOCALHOST
InetAddress
that represents the loopback address. If IPv6 stack is available, it will refer to
LOCALHOST6
. Otherwise, LOCALHOST4
.public static final java.net.NetworkInterface LOOPBACK_IF
NetworkInterface
of the current machinepublic static final int SOMAXCONN
200
is used as a
default value for Windows or 128
for others.private static final int IPV6_WORD_COUNT
private static final int IPV6_MAX_CHAR_COUNT
private static final int IPV6_BYTE_COUNT
private static final int IPV6_MAX_CHAR_BETWEEN_SEPARATOR
private static final int IPV6_MIN_SEPARATORS
private static final int IPV6_MAX_SEPARATORS
private static final int IPV4_MAX_CHAR_BETWEEN_SEPARATOR
private static final int IPV4_SEPARATORS
private static final boolean IPV4_PREFERRED
true
if IPv4 should be used even if the system supports both IPv4 and IPv6.private static final boolean IPV6_ADDRESSES_PREFERRED
true
if an IPv6 address should be preferred when a host has both an IPv4 address and an IPv6 address.private static final InternalLogger logger
private static java.lang.Integer sysctlGetInt(java.lang.String sysctlKey) throws java.io.IOException
sysctlKey
which is expected to return the numeric value for for sysctlKey
.sysctlKey
- The key which the return value corresponds to.sysctlKey
.java.io.IOException
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(java.lang.String ipAddressString)
private static int decimalDigit(java.lang.String str, int pos)
private static byte ipv4WordToByte(java.lang.String ip, int from, int toExclusive)
static byte[] validIpV4ToBytes(java.lang.String ip)
public static java.lang.String intToIpAddress(int i)
public static java.lang.String bytesToIpAddress(byte[] bytes)
java.lang.IllegalArgumentException
- if length
is not 4
nor 16
public static java.lang.String bytesToIpAddress(byte[] bytes, int offset, int length)
java.lang.IllegalArgumentException
- if length
is not 4
nor 16
public static boolean isValidIpV6Address(java.lang.String ip)
private static boolean isValidIpV4Word(java.lang.CharSequence word, int from, int toExclusive)
private static boolean isValidHexChar(char c)
private static boolean isValidNumericChar(char c)
private static boolean isValidIPv4MappedChar(char c)
private static boolean isValidIPv4MappedSeparators(byte b0, byte b1, boolean mustBeZero)
private static boolean isValidIPv4Mapped(byte[] bytes, int currentIndex, int compressBegin, int compressLength)
public static boolean isValidIpV4Address(java.lang.String ip)
private static boolean isValidIpV4Address(java.lang.String ip, int from, int toExcluded)
public static java.net.Inet6Address getByName(java.lang.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 java.net.Inet6Address getByName(java.lang.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.private static byte[] getIPv6ByName(java.lang.CharSequence ip, boolean ipv4Mapped)
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.ip
or null
if not a valid IP address.public static java.lang.String toSocketAddressString(java.net.InetSocketAddress addr)
String
representation of an InetSocketAddress
.
The output does not include Scope ID.
addr
- InetSocketAddress
to be converted to an address stringString
containing the text-formatted IP addresspublic static java.lang.String toSocketAddressString(java.lang.String host, int port)
String
representation of a host port combo.private static java.lang.StringBuilder newSocketAddressStringBuilder(java.lang.String host, java.lang.String port, boolean ipv4)
public static java.lang.String toAddressString(java.net.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 java.lang.String toAddressString(java.net.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 addressprivate static java.lang.String toAddressString(byte[] bytes, int offset, boolean ipv4Mapped)
private static boolean inRangeEndExclusive(int value, int start, int end)
value
if is within start
(inclusive) and end
(exclusive).value
- The value to checked if is within start
(inclusive) and end
(exclusive)start
- The start of the range (inclusive)end
- The end of the range (exclusive)true
if value
if is within start
(inclusive) and end
(exclusive)false
otherwise