Top Description Inners Fields Constructors Methods
io.netty.util

public final Class NetUtil

extends Object
Class Inheritance
Imports
io.netty.util.NetUtilInitializations.NetworkIfaceAndInetAddress, io.netty.util.internal.PlatformDependent, .StringUtil, .SystemPropertyUtil, io.netty.util.internal.logging.InternalLogger, .InternalLoggerFactory, java.io.BufferedReader, .File, .FileReader, .IOException, .InputStream, .InputStreamReader, java.net.Inet4Address, .Inet6Address, .InetAddress, .InetSocketAddress, .NetworkInterface, .UnknownHostException, java.security.AccessController, .PrivilegedAction, java.util.Arrays, .Collection

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.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private static final int
IPV4_MAX_CHAR_BETWEEN_SEPARATOR

Maximum amount of value adding characters in between 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 int
IPV4_SEPARATORS

Number of separators that must be present in an IPv4 string

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 int
IPV6_BYTE_COUNT

Number of bytes needed to represent an IPV6 value

private static final int
IPV6_MAX_CHAR_BETWEEN_SEPARATOR

Maximum amount of value adding characters in between IPV6 separators

private static final int
IPV6_MAX_CHAR_COUNT

The maximum number of characters for an IPV6 string with no scope

private static final int
IPV6_MAX_SEPARATORS

Maximum number of separators that must be present in an IPv6 string

private static final int
IPV6_MIN_SEPARATORS

Minimum number of separators that must be present in an IPv6 string

private static final int
IPV6_WORD_COUNT

This defines how many words (represented as ints) are needed to represent an IPv6 address

public static final InetAddress
LOCALHOST

The InetAddress that represents the loopback address.

public static final Inet4Address
LOCALHOST4

The Inet4Address that represents the IPv4 loopback address '127.0.0.1'

public static final Inet6Address
LOCALHOST6

The Inet6Address that represents the IPv6 loopback address '::1'

private static final InternalLogger
logger

The logger being used by this class

public static final NetworkInterface
LOOPBACK_IF

The loopback NetworkInterface of the current machine

public static final Collection<NetworkInterface>
NETWORK_INTERFACES

An unmodifiable Collection of all the interfaces on this machine.

public static final int
SOMAXCONN

The SOMAXCONN value of the current machine.

Constructor Summary

AccessConstructor and Description
private
NetUtil()

A constructor to stop this class being constructed.

Method Summary

Modifier and TypeMethod and Description
public static String
bytesToIpAddress(byte[] bytes)

Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.

public static String
bytesToIpAddress(byte[] bytes, int offset, int length)

Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.

public static byte[]
createByteArrayFromIpAddressString(String ipAddressString)

Creates an byte[] based on an ipAddressString.

public static InetAddress
createInetAddressFromIpAddressString(String ipAddressString)

Creates an InetAddress based on an ipAddressString or might return null if it can't be parsed.

private static int
decimalDigit(String str, int pos)

public static Inet6Address

Returns:

Inet6Address representation of the ip or null if not a valid IP address.
getByName
(CharSequence
CharSequence IP address to be converted to a Inet6Address
ip
)

Returns the Inet6Address representation of a CharSequence IP address.

public static Inet6Address

Returns:

Inet6Address representation of the ip or null if not a valid IP address.
getByName
(CharSequence
CharSequence IP address to be converted to a Inet6Address
ip
,
boolean
  • true To allow IPv4 mapped inputs to be translated into Inet6Address
  • false Consider IPv4 mapped addresses as invalid.
ipv4Mapped
)

Returns the Inet6Address representation of a CharSequence IP address.

public static String

Returns:

the host string
getHostname
(InetSocketAddress
The address
addr
)

Returns InetSocketAddress#getHostString() if Java >= 7, or InetSocketAddress#getHostName() otherwise.

pack-priv static byte[]

Returns:

byte array representation of the ip or null if not a valid IP address.
getIPv6ByName
(CharSequence
CharSequence IP address to be converted to a Inet6Address
ip
,
boolean
  • true To allow IPv4 mapped inputs to be translated into Inet6Address
  • false Consider IPv4 mapped addresses as invalid.
ipv4Mapped
)

Returns the byte array representation of a CharSequence IP address.

private static boolean

Returns:

  • true if value if is within start (inclusive) and end (exclusive)
  • false otherwise
inRangeEndExclusive
(int
The value to checked if is within start (inclusive) and end (exclusive)
value
,
int
The start of the range (inclusive)
start
,
int
The end of the range (exclusive)
end
)

Does a range check on value if is within start (inclusive) and end (exclusive).

public static String
intToIpAddress(int i)

Converts a 32-bit integer into an IPv4 address.

public static int
ipv4AddressToInt(Inet4Address ipAddress)

Convert Inet4Address into int

private static byte
ipv4WordToByte(String ip, int from, int toExclusive)

public static boolean
isIpV4StackPreferred()

Returns true if IPv4 should be used even if the system supports both IPv4 and IPv6.

public 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
public static boolean

Returns:

true, if the string represents an IPV4 address in dotted notation, false otherwise
isValidIpV4Address
(CharSequence ip)

Takes a CharSequence and parses it to see if it is a valid IPV4 address.

public static boolean

Returns:

true, if the string represents an IPV4 address in dotted notation, false otherwise
isValidIpV4Address
(String ip)

Takes a String and parses it to see if it is a valid IPV4 address.

private static boolean
isValidIpV4Address(CharSequence ip, int from, int toExcluded)

private static boolean
isValidIpV4Address(String ip, int from, int toExcluded)

private static boolean
isValidIpV4Address(AsciiString ip, int from, int toExcluded)

private static boolean
isValidIpV4Address0(CharSequence ip, int from, int toExcluded)

private static boolean
isValidIPv4Mapped(byte[] bytes, int currentIndex, int compressBegin, int compressLength)

private static boolean
private static boolean
isValidIPv4MappedSeparators(byte b0, byte b1, boolean mustBeZero)

private static boolean
isValidIpV4Word(CharSequence word, int from, int toExclusive)

public static boolean
public static boolean
private static boolean
private static StringBuilder
newSocketAddressStringBuilder(String host, String port, boolean ipv4)

private static Integer

Returns:

The sysctl value for sysctlKey.
sysctlGetInt
(String
The key which the return value corresponds to.
sysctlKey
)

This will execute sysctl with the sysctlKey which is expected to return the numeric value for for sysctlKey.

public static String

Returns:

String containing the text-formatted IP address
toAddressString
(InetAddress
InetAddress to be converted to an address string
ip
)

Returns the String representation of an InetAddress.

public static String

Returns:

String containing the text-formatted IP address
toAddressString
(InetAddress
InetAddress to be converted to an address string
ip
,
boolean
  • 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 4
  • false to strictly follow rfc 5952
ipv4Mapped
)

Returns the String representation of an InetAddress.

private static String
toAddressString(byte[] bytes, int offset, boolean ipv4Mapped)

public static String

Returns:

String containing the text-formatted IP address
toSocketAddressString
(InetSocketAddress
InetSocketAddress to be converted to an address string
addr
)

Returns the String representation of an InetSocketAddress.

public static String
toSocketAddressString(String host, int port)

Returns the String representation of a host port combo.

pack-priv static byte[]
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

IPV4_MAX_CHAR_BETWEEN_SEPARATORback to summary
private static final int IPV4_MAX_CHAR_BETWEEN_SEPARATOR

Maximum amount of value adding characters in between IPV4 separators

IPV4_PREFERREDback to summary
private static final boolean IPV4_PREFERRED

true if IPv4 should be used even if the system supports both IPv4 and IPv6.

IPV4_SEPARATORSback to summary
private static final int IPV4_SEPARATORS

Number of separators that must be present in an IPv4 string

IPV6_ADDRESSES_PREFERREDback to summary
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.

IPV6_BYTE_COUNTback to summary
private static final int IPV6_BYTE_COUNT

Number of bytes needed to represent an IPV6 value

IPV6_MAX_CHAR_BETWEEN_SEPARATORback to summary
private static final int IPV6_MAX_CHAR_BETWEEN_SEPARATOR

Maximum amount of value adding characters in between IPV6 separators

IPV6_MAX_CHAR_COUNTback to summary
private static final int IPV6_MAX_CHAR_COUNT

The maximum number of characters for an IPV6 string with no scope

IPV6_MAX_SEPARATORSback to summary
private static final int IPV6_MAX_SEPARATORS

Maximum number of separators that must be present in an IPv6 string

IPV6_MIN_SEPARATORSback to summary
private static final int IPV6_MIN_SEPARATORS

Minimum number of separators that must be present in an IPv6 string

IPV6_WORD_COUNTback to summary
private static final int IPV6_WORD_COUNT

This defines how many words (represented as ints) are needed to represent an IPv6 address

LOCALHOSTback to summary
public static final InetAddress LOCALHOST

The InetAddress that represents the loopback address. If IPv6 stack is available, it will refer to LOCALHOST6. Otherwise, LOCALHOST4.

LOCALHOST4back to summary
public static final Inet4Address LOCALHOST4

The Inet4Address that represents the IPv4 loopback address '127.0.0.1'

LOCALHOST6back to summary
public static final Inet6Address LOCALHOST6

The Inet6Address that represents the IPv6 loopback address '::1'

loggerback to summary
private static final InternalLogger logger

The logger being used by this class

LOOPBACK_IFback to summary
public static final NetworkInterface LOOPBACK_IF

The loopback NetworkInterface of the current machine

NETWORK_INTERFACESback to summary
public static final Collection<NetworkInterface> NETWORK_INTERFACES

An unmodifiable Collection of all the interfaces on this machine.

SOMAXCONNback to summary
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 and 128 for others.

Constructor Detail

NetUtilback to summary
private NetUtil()

A constructor to stop this class being constructed.

Method Detail

bytesToIpAddressback to summary
public static String bytesToIpAddress(byte[] bytes)

Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.

Exceptions
IllegalArgumentException:
if length is not 4 nor 16
bytesToIpAddressback to summary
public static String bytesToIpAddress(byte[] bytes, int offset, int length)

Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.

Exceptions
IllegalArgumentException:
if length is not 4 nor 16
createByteArrayFromIpAddressStringback to summary
public static byte[] createByteArrayFromIpAddressString(String ipAddressString)

Creates an byte[] based on an ipAddressString. No error handling is performed here.

createInetAddressFromIpAddressStringback to summary
public static InetAddress createInetAddressFromIpAddressString(String ipAddressString)

Creates an InetAddress based on an ipAddressString or might return null if it can't be parsed. No error handling is performed here.

decimalDigitback to summary
private static int decimalDigit(String str, int pos)
getByNameback to summary
public static Inet6Address getByName(CharSequence ip)

Returns the Inet6Address representation of a CharSequence IP address.

This method will treat all IPv4 type addresses as "IPv4 mapped" (see getByName(CharSequence, boolean))

Parameters
ip:CharSequence

CharSequence IP address to be converted to a Inet6Address

Returns:Inet6Address

Inet6Address representation of the ip or null if not a valid IP address.

getByNameback to summary
public static Inet6Address getByName(CharSequence ip, boolean ipv4Mapped)

Returns the 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.

Parameters
ip:CharSequence

CharSequence IP address to be converted to a Inet6Address

ipv4Mapped:boolean

  • true To allow IPv4 mapped inputs to be translated into Inet6Address
  • false Consider IPv4 mapped addresses as invalid.
Returns:Inet6Address

Inet6Address representation of the ip or null if not a valid IP address.

getHostnameback to summary
public static String getHostname(InetSocketAddress addr)

Returns InetSocketAddress#getHostString() if Java >= 7, or InetSocketAddress#getHostName() otherwise.

Parameters
addr:InetSocketAddress

The address

Returns:String

the host string

getIPv6ByNameback to summary
pack-priv static byte[] getIPv6ByName(CharSequence ip, boolean ipv4Mapped)

Returns the byte array 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.

Parameters
ip:CharSequence

CharSequence IP address to be converted to a Inet6Address

ipv4Mapped:boolean

  • true To allow IPv4 mapped inputs to be translated into Inet6Address
  • false Consider IPv4 mapped addresses as invalid.
Returns:byte[]

byte array representation of the ip or null if not a valid IP address.

inRangeEndExclusiveback to summary
private static boolean inRangeEndExclusive(int value, int start, int end)

Does a range check on value if is within start (inclusive) and end (exclusive).

Parameters
value:int

The value to checked if is within start (inclusive) and end (exclusive)

start:int

The start of the range (inclusive)

end:int

The end of the range (exclusive)

Returns:boolean

  • true if value if is within start (inclusive) and end (exclusive)
  • false otherwise
intToIpAddressback to summary
public static String intToIpAddress(int i)

Converts a 32-bit integer into an IPv4 address.

ipv4AddressToIntback to summary
public static int ipv4AddressToInt(Inet4Address ipAddress)

Convert Inet4Address into int

ipv4WordToByteback to summary
private static byte ipv4WordToByte(String ip, int from, int toExclusive)
isIpV4StackPreferredback to summary
public static boolean isIpV4StackPreferred()

Returns 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.

See Also
Java SE networking properties
isIpV6AddressesPreferredback to summary
public static boolean isIpV6AddressesPreferred()

Returns 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.

See Also
Java SE networking properties
isValidHexCharback to summary
private static boolean isValidHexChar(char c)
isValidIpV4Addressback to summary
public static boolean isValidIpV4Address(CharSequence ip)

Takes a CharSequence and parses it to see if it is a valid IPV4 address.

Returns:boolean

true, if the string represents an IPV4 address in dotted notation, false otherwise

isValidIpV4Addressback to summary
public static boolean isValidIpV4Address(String ip)

Takes a String and parses it to see if it is a valid IPV4 address.

Returns:boolean

true, if the string represents an IPV4 address in dotted notation, false otherwise

isValidIpV4Addressback to summary
private static boolean isValidIpV4Address(CharSequence ip, int from, int toExcluded)
isValidIpV4Addressback to summary
private static boolean isValidIpV4Address(String ip, int from, int toExcluded)
Annotations
@SuppressWarnings:DuplicateBooleanBranch
isValidIpV4Addressback to summary
private static boolean isValidIpV4Address(AsciiString ip, int from, int toExcluded)
Annotations
@SuppressWarnings:DuplicateBooleanBranch
isValidIpV4Address0back to summary
private static boolean isValidIpV4Address0(CharSequence ip, int from, int toExcluded)
Annotations
@SuppressWarnings:DuplicateBooleanBranch
isValidIPv4Mappedback to summary
private static boolean isValidIPv4Mapped(byte[] bytes, int currentIndex, int compressBegin, int compressLength)
isValidIPv4MappedCharback to summary
private static boolean isValidIPv4MappedChar(char c)
isValidIPv4MappedSeparatorsback to summary
private static boolean isValidIPv4MappedSeparators(byte b0, byte b1, boolean mustBeZero)
isValidIpV4Wordback to summary
private static boolean isValidIpV4Word(CharSequence word, int from, int toExclusive)
isValidIpV6Addressback to summary
public static boolean isValidIpV6Address(String ip)
isValidIpV6Addressback to summary
public static boolean isValidIpV6Address(CharSequence ip)
isValidNumericCharback to summary
private static boolean isValidNumericChar(char c)
newSocketAddressStringBuilderback to summary
private static StringBuilder newSocketAddressStringBuilder(String host, String port, boolean ipv4)
sysctlGetIntback to summary
private static Integer sysctlGetInt(String sysctlKey) throws IOException

This will execute sysctl with the sysctlKey which is expected to return the numeric value for for sysctlKey.

Parameters
sysctlKey:String

The key which the return value corresponds to.

Returns:Integer

The sysctl value for sysctlKey.

toAddressStringback to summary
public static String toAddressString(InetAddress ip)

Returns the String representation of an InetAddress.

The output does not include Scope ID.

Parameters
ip:InetAddress

InetAddress to be converted to an address string

Returns:String

String containing the text-formatted IP address

toAddressStringback to summary
public static String toAddressString(InetAddress ip, boolean ipv4Mapped)

Returns the String representation of an InetAddress.

The output does not include Scope ID.

Parameters
ip:InetAddress

InetAddress to be converted to an address string

ipv4Mapped:boolean

  • 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 4
  • false to strictly follow rfc 5952
Returns:String

String containing the text-formatted IP address

toAddressStringback to summary
private static String toAddressString(byte[] bytes, int offset, boolean ipv4Mapped)
toSocketAddressStringback to summary
public static String toSocketAddressString(InetSocketAddress addr)

Returns the String representation of an InetSocketAddress.

The output does not include Scope ID.

Parameters
addr:InetSocketAddress

InetSocketAddress to be converted to an address string

Returns:String

String containing the text-formatted IP address

toSocketAddressStringback to summary
public static String toSocketAddressString(String host, int port)

Returns the String representation of a host port combo.

validIpV4ToBytesback to summary
pack-priv static byte[] validIpV4ToBytes(String ip)
io.netty.util back to summary

private final Class NetUtil.SoMaxConnAction

extends Object
implements PrivilegedAction<Integer>
Class Inheritance
All Implemented Interfaces
java.security.PrivilegedAction

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public Integer
run()

Implements java.security.PrivilegedAction.run.

Performs the computation.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

SoMaxConnActionback to summary
private SoMaxConnAction()

Method Detail

runback to summary
public Integer run()

Implements java.security.PrivilegedAction.run.

Doc from java.security.PrivilegedAction.run.

Performs the computation. This method will be called by AccessController.doPrivileged after enabling privileges.

Returns:Integer

a class-dependent value that may represent the results of the computation. Each class that implements PrivilegedAction should document what (if anything) this value represents.

Annotations
@Override