Package org.jboss.netty.util
Class NetUtil
- java.lang.Object
-
- org.jboss.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 private static int
IPV4_BYTE_COUNT
Number of bytes needed to represent and IPV4 valueprivate static int
IPV4_MAX_CHAR_BETWEEN_SEPARATOR
Maximum amount of value adding characters in between IPV4 separatorsprivate static int
IPV4_SEPARATORS
Number of separators that must be present in an IPv4 stringprivate static int
IPV6_BYTE_COUNT
Number of bytes needed to represent and IPV6 valueprivate static int
IPV6_MAX_CHAR_BETWEEN_SEPARATOR
Maximum amount of value adding characters in between IPV6 separatorsprivate static int
IPV6_MAX_CHAR_COUNT
The maximum number of characters for an IPV6 string with no scopeprivate static int
IPV6_MAX_SEPARATORS
Maximum number of separators that must be present in an IPv6 stringprivate static int
IPV6_MIN_SEPARATORS
Minimum number of separators that must be present in an IPv6 stringprivate static int
IPV6_WORD_COUNT
This defines how many words (represented as ints) are needed to represent an IPv6 addressprivate static InternalLogger
logger
The logger being used by this class
-
Constructor Summary
Constructors Modifier Constructor Description private
NetUtil()
A constructor to stop this class being constructed.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static void
convertToBytes(java.lang.String hexWord, byte[] ipByteArray, int byteIndex)
Converts a 4 character hex word into a 2 byte word equivalentstatic byte[]
createByteArrayFromIpAddressString(java.lang.String ipAddressString)
Creates an byte[] based on an ipAddressString.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.(package private) static int
getIntValue(char c)
private static boolean
inRangeEndExclusive(int value, int start, int end)
Does a range check onvalue
if is withinstart
(inclusive) andend
(exclusive).private static boolean
isValidHexChar(char c)
static boolean
isValidIp4Word(java.lang.String word)
static boolean
isValidIpV4Address(java.lang.String value)
Takes a string and parses it to see if it is a valid IPV4 address.static boolean
isValidIpV6Address(java.lang.String ipAddress)
private static boolean
isValidNumericChar(char c)
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
.
-
-
-
Field Detail
-
IPV6_WORD_COUNT
private static final int IPV6_WORD_COUNT
This defines how many words (represented as ints) are needed to represent an IPv6 address- See Also:
- Constant Field Values
-
IPV6_MAX_CHAR_COUNT
private static final int IPV6_MAX_CHAR_COUNT
The maximum number of characters for an IPV6 string with no scope- See Also:
- Constant Field Values
-
IPV6_BYTE_COUNT
private static final int IPV6_BYTE_COUNT
Number of bytes needed to represent and IPV6 value- See Also:
- Constant Field Values
-
IPV6_MAX_CHAR_BETWEEN_SEPARATOR
private static final int IPV6_MAX_CHAR_BETWEEN_SEPARATOR
Maximum amount of value adding characters in between IPV6 separators- See Also:
- Constant Field Values
-
IPV6_MIN_SEPARATORS
private static final int IPV6_MIN_SEPARATORS
Minimum number of separators that must be present in an IPv6 string- See Also:
- Constant Field Values
-
IPV6_MAX_SEPARATORS
private static final int IPV6_MAX_SEPARATORS
Maximum number of separators that must be present in an IPv6 string- See Also:
- Constant Field Values
-
IPV4_BYTE_COUNT
private static final int IPV4_BYTE_COUNT
Number of bytes needed to represent and IPV4 value- See Also:
- Constant Field Values
-
IPV4_MAX_CHAR_BETWEEN_SEPARATOR
private static final int IPV4_MAX_CHAR_BETWEEN_SEPARATOR
Maximum amount of value adding characters in between IPV4 separators- See Also:
- Constant Field Values
-
IPV4_SEPARATORS
private static final int IPV4_SEPARATORS
Number of separators that must be present in an IPv4 string- See Also:
- Constant Field Values
-
logger
private static final InternalLogger logger
The logger being used by this class
-
-
Method Detail
-
createByteArrayFromIpAddressString
public static byte[] createByteArrayFromIpAddressString(java.lang.String ipAddressString)
Creates an byte[] based on an ipAddressString. No error handling is performed here.
-
convertToBytes
private static void convertToBytes(java.lang.String hexWord, byte[] ipByteArray, int byteIndex)
Converts a 4 character hex word into a 2 byte word equivalent
-
getIntValue
static int getIntValue(char c)
-
isValidIpV6Address
public static boolean isValidIpV6Address(java.lang.String ipAddress)
-
isValidIp4Word
public static boolean isValidIp4Word(java.lang.String word)
-
isValidHexChar
private static boolean isValidHexChar(char c)
-
isValidNumericChar
private static boolean isValidNumericChar(char c)
-
isValidIpV4Address
public static boolean isValidIpV4Address(java.lang.String value)
Takes a string 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
Don't turn IPv4 addressed to mapped addresses
- Returns:
Inet6Address
representation of theip
ornull
if not a valid IP address.
-
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
-
inRangeEndExclusive
private static boolean inRangeEndExclusive(int value, int start, int end)
Does a range check onvalue
if is withinstart
(inclusive) andend
(exclusive).- Parameters:
value
- The value to checked if is withinstart
(inclusive) andend
(exclusive)start
- The start of the range (inclusive)end
- The end of the range (exclusive)- Returns:
true
ifvalue
if is withinstart
(inclusive) andend
(exclusive)false
otherwise
-
-