public class Nets extends Object
| 限定符和类型 | 字段和说明 |
|---|---|
static Inet4Address |
INET4_ANY
The "any" address for IPv4.
|
static Inet4Address |
INET4_BROADCAST
The broadcast-all address for IPv4.
|
static Inet6Address |
INET6_ANY
The "any" address for IPv6.
|
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 int |
SOMAXCONN
The SOMAXCONN value of the current machine.
|
| 构造器和说明 |
|---|
Nets() |
| 限定符和类型 | 方法和说明 |
|---|---|
static Enumeration<InetAddress> |
addressesFromNetworkInterface(NetworkInterface intf) |
static byte[] |
createByteArrayFromIpAddressString(String ipAddressString)
Creates an byte[] based on an ipAddressString.
|
static NetworkInterface |
findInterfaceWithScopeId(String scopeName) |
static Set<InetAddress> |
getAddresses()
Returns a
Set of InetAddress that are non-loopback or mac. |
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 InetAddress |
getCurrentAddress()
获取本机IP
|
static String |
getFirstValidMac() |
static NetworkInterface |
getFirstValidNetworkInterface()
获取第一个有效的网卡接口
|
static String |
getHostname(InetSocketAddress addr)
Returns InetSocketAddress#getHostString() if Java >= 7,
or
InetSocketAddress.getHostName() otherwise. |
static Inet4Address |
getInet4Address(int s1,
int s2,
int s3,
int s4)
Get an IPv4 address from four integer segments.
|
static Inet6Address |
getInet6Address(int s1,
int s2,
int s3,
int s4,
int s5,
int s6,
int s7,
int s8)
Get an IPv6 address from eight integer segments.
|
static String |
getLocalHostName()
Returns the local hostname.
|
static String |
getLocalIp()
Returns the local IP.
|
static String |
getMac(byte[] bytes) |
static String |
getMac(NetworkInterface networkInterface) |
static Map<String,Set<InetAddress>> |
getNetworkInterfaceAddresses()
|
static Map<String,Set<InetAddress>> |
getNetworkInterfaceAddresses(Predicate<NetworkInterface> networkInterfacePredicate,
Predicate<InetAddress> inetAddressPredicate) |
static List<NetworkInterface> |
getNetworkInterfaces() |
static List<NetworkInterface> |
getNetworkInterfaces(Predicate<NetworkInterface> predicate) |
static int |
getScopeId(InetAddress address)
Get the scope ID of the given address (if it is an IPv6 address).
|
static int |
getScopeId(NetworkInterface networkInterface) |
static int |
getScopeId(NetworkInterface networkInterface,
InetAddress compareWith) |
static int |
getScopeId(String scopeName)
Attempt to get the scope ID of the given string.
|
static int |
getScopeId(String scopeName,
InetAddress compareWith)
Attempt to get the scope ID of the given string.
|
static int |
indexOf(CharSequence cs,
char searchChar,
int start)
Finds the first index in the
CharSequence that matches the
specified character. |
static boolean |
isValidDomainAddress(String domain)
Checks validity of a domain name.
|
static boolean |
isValidEmailDomainAddress(String domain)
Checks the validity of the domain name used in an email.
|
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 boolean |
isValidPort(int port) |
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 |
toOptimalString(byte[] addressBytes)
Get the optimal string representation of the bytes of an IP address.
|
static String |
toOptimalString(InetAddress inetAddress)
Get the optimal string representation of an IP address.
|
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 Inet4Address INET4_ANY
public static final Inet6Address LOCALHOST6
Inet6Address that represents the IPv6 loopback address '::1'public static final Inet4Address INET4_BROADCAST
public static final Inet6Address INET6_ANY
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 int SOMAXCONN
200 is used as a
default value for Windows or 128 for others.public static Inet4Address getInet4Address(int s1, int s2, int s3, int s4)
s1 - the first segments2 - the second segments3 - the third segments4 - the fourth segmentnull)public static Inet6Address getInet6Address(int s1, int s2, int s3, int s4, int s5, int s6, int s7, int s8)
0xffff).s1 - the first segments2 - the second segments3 - the third segments4 - the fourth segments5 - the fifth segments6 - the sixth segments7 - the seventh segments8 - the eighth segmentnull)public static boolean isValidEmailDomainAddress(String domain)
domain - domain to check for validitytrue if the provided string is a valid domain, false otherwisepublic static boolean isValidDomainAddress(String domain)
domain - the domain to check for validitytrue if the provided string is a valid domain, false otherwisepublic static boolean isValidIpV6Address(String ip)
public static boolean isValidIpV6Address(CharSequence ip)
public static boolean isValidIpV4Address(String ip)
String and parses it to see if it is a valid IPV4 address.public static boolean isValidPort(int port)
public static int indexOf(CharSequence cs, char searchChar, int start)
Finds the first index in the CharSequence that matches the
specified character.
cs - the CharSequence to be processed, not nullsearchChar - the char to be searched forstart - the start index, negative starts at the string startsearchChar is not found or cs == nullpublic 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 Inet6AddressInet6Address 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 Inet6Addressipv4Mapped - true To allow IPv4 mapped inputs to be translated into Inet6Addressfalse 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.getHostName() otherwise.addr - The addresspublic static Enumeration<InetAddress> addressesFromNetworkInterface(NetworkInterface intf)
public static byte[] createByteArrayFromIpAddressString(String ipAddressString)
public static Map<String,Set<InetAddress>> getNetworkInterfaceAddresses()
public static Map<String,Set<InetAddress>> getNetworkInterfaceAddresses(@Nullable Predicate<NetworkInterface> networkInterfacePredicate, @Nullable Predicate<InetAddress> inetAddressPredicate)
public static List<NetworkInterface> getNetworkInterfaces()
public static List<NetworkInterface> getNetworkInterfaces(Predicate<NetworkInterface> predicate)
public static NetworkInterface getFirstValidNetworkInterface()
public static String getFirstValidMac()
@Nullable public static String getMac(@NonNull NetworkInterface networkInterface)
public static Set<InetAddress> getAddresses()
Set of InetAddress that are non-loopback or mac.public static InetAddress getCurrentAddress()
public static String getLocalHostName() throws UnknownHostException
public static String getLocalIp() throws UnknownHostException
public static int getScopeId(InetAddress address)
public static int getScopeId(String scopeName)
scopeName - the scope number or name as a string (must not be null)public static int getScopeId(String scopeName, InetAddress compareWith)
scopeName - the scope number or name as a string (must not be null)compareWith - the address to compare with, to ensure that the wrong local scope is not selected (may be null)public static NetworkInterface findInterfaceWithScopeId(String scopeName)
scopeName - public static int getScopeId(NetworkInterface networkInterface)
networkInterface - public static int getScopeId(NetworkInterface networkInterface, InetAddress compareWith)
networkInterface - compareWith - public static String toOptimalString(InetAddress inetAddress)
inetAddress - the address (must not be null)null)public static String toOptimalString(byte[] addressBytes)
addressBytes - the address bytes (must not be null)null)Copyright © 2021. All rights reserved.