本文整理汇总了Java中com.google.common.net.InetAddresses类的典型用法代码示例。如果您正苦于以下问题:Java InetAddresses类的具体用法?Java InetAddresses怎么用?Java InetAddresses使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
InetAddresses类属于com.google.common.net包,在下文中一共展示了InetAddresses类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getLocalInterfaceAddrs
import com.google.common.net.InetAddresses; //导入依赖的package包/类
/**
* Return the socket addresses to use with each configured
* local interface. Local interfaces may be specified by IP
* address, IP address range using CIDR notation, interface
* name (e.g. eth0) or sub-interface name (e.g. eth0:0).
* The socket addresses consist of the IPs for the interfaces
* and the ephemeral port (port 0). If an IP, IP range, or
* interface name matches an interface with sub-interfaces
* only the IP of the interface is used. Sub-interfaces can
* be used by specifying them explicitly (by IP or name).
*
* @return SocketAddresses for the configured local interfaces,
* or an empty array if none are configured
* @throws UnknownHostException if a given interface name is invalid
*/
private static SocketAddress[] getLocalInterfaceAddrs(
String interfaceNames[]) throws UnknownHostException {
List<SocketAddress> localAddrs = new ArrayList<>();
for (String interfaceName : interfaceNames) {
if (InetAddresses.isInetAddress(interfaceName)) {
localAddrs.add(new InetSocketAddress(interfaceName, 0));
} else if (NetUtils.isValidSubnet(interfaceName)) {
for (InetAddress addr : NetUtils.getIPs(interfaceName, false)) {
localAddrs.add(new InetSocketAddress(addr, 0));
}
} else {
for (String ip : DNS.getIPs(interfaceName, false)) {
localAddrs.add(new InetSocketAddress(ip, 0));
}
}
}
return localAddrs.toArray(new SocketAddress[localAddrs.size()]);
}
示例2: anonymizeIp
import com.google.common.net.InetAddresses; //导入依赖的package包/类
/**
* Anonymize an IP address
* @param inetAddress the IP address to be anonymized
* @param replaceLastOctetWith the String which shall replace the last octet in IPv4
* @return
*/
public static String anonymizeIp(final InetAddress inetAddress, String replaceLastOctetWith) {
try
{
final byte[] address = inetAddress.getAddress();
address[address.length - 1] = 0;
if (address.length > 4) // ipv6
{
for (int i = 6; i < address.length; i++)
address[i] = 0;
}
String result = InetAddresses.toAddrString(InetAddress.getByAddress(address));
if (address.length == 4)
result = result.replaceFirst(".0$", replaceLastOctetWith);
return result;
}
catch (final Exception e)
{
e.printStackTrace();
return null;
}
}
示例3: VersionMessage
import com.google.common.net.InetAddresses; //导入依赖的package包/类
public VersionMessage(NetworkParameters params, int newBestHeight) {
super(params);
clientVersion = params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.CURRENT);
localServices = 0;
time = System.currentTimeMillis() / 1000;
// Note that the Bitcoin Core doesn't do anything with these, and finding out your own external IP address
// is kind of tricky anyway, so we just put nonsense here for now.
InetAddress localhost = InetAddresses.forString("127.0.0.1");
myAddr = new PeerAddress(params, localhost, params.getPort(), 0, BigInteger.ZERO);
theirAddr = new PeerAddress(params, localhost, params.getPort(), 0, BigInteger.ZERO);
subVer = LIBRARY_SUBVER;
bestHeight = newBestHeight;
relayTxesBeforeFilter = true;
length = 85;
if (protocolVersion > 31402)
length += 8;
length += VarInt.sizeOf(subVer.length()) + subVer.length();
}
示例4: ipToLong
import com.google.common.net.InetAddresses; //导入依赖的package包/类
public static long ipToLong(String ip) {
try {
if (!InetAddresses.isInetAddress(ip)) {
throw new IllegalArgumentException("failed to parse ip [" + ip + "], not a valid ip address");
}
String[] octets = pattern.split(ip);
if (octets.length != 4) {
throw new IllegalArgumentException("failed to parse ip [" + ip + "], not a valid ipv4 address (4 dots)");
}
return (Long.parseLong(octets[0]) << 24) + (Integer.parseInt(octets[1]) << 16) +
(Integer.parseInt(octets[2]) << 8) + Integer.parseInt(octets[3]);
} catch (Exception e) {
if (e instanceof IllegalArgumentException) {
throw (IllegalArgumentException) e;
}
throw new IllegalArgumentException("failed to parse ip [" + ip + "]", e);
}
}
示例5: checkWifi
import com.google.common.net.InetAddresses; //导入依赖的package包/类
@Override
public CheckWifiResult checkWifi(IosDevice device) throws IosDeviceException {
String output = runAppRobust(device, Duration.ofSeconds(15), "--check_wifi");
Matcher ipMatcher = CHECK_WIFI_IP_ADDRESS_PATTERN.matcher(output);
Matcher ssidMatcher = CHECK_WIFI_SSID_PATTERN.matcher(output);
Optional<WifiConnection> connection;
if (ipMatcher.find() && ssidMatcher.find()) {
InetAddress ip = InetAddresses.forString(ipMatcher.group(1));
String ssid = ssidMatcher.group(1);
connection = Optional.of(new AutoValue_AppResult_WifiConnection(ip, ssid));
} else {
connection = Optional.empty();
}
return new CheckWifiResult(output, connection);
}
示例6: testOneIpv4
import com.google.common.net.InetAddresses; //导入依赖的package包/类
@Test
public void testOneIpv4() {
runCommonTestsWith(
AuthorityClassifiers.builder()
.host(InetAddresses.forUriString("127.0.0.1"))
.build(),
// Localhost does not show here because we do not assume
// localhost == 127.0.0.1 or resolve hostnames.
"file://[email protected]/bar"
);
runCommonTestsWith(
AuthorityClassifiers.builder()
.host((Inet4Address) InetAddresses.forUriString("127.0.0.1"))
.build(),
"file://[email protected]/bar"
);
}
示例7: getLocalInterfaceAddrs
import com.google.common.net.InetAddresses; //导入依赖的package包/类
/**
* Return the socket addresses to use with each configured
* local interface. Local interfaces may be specified by IP
* address, IP address range using CIDR notation, interface
* name (e.g. eth0) or sub-interface name (e.g. eth0:0).
* The socket addresses consist of the IPs for the interfaces
* and the ephemeral port (port 0). If an IP, IP range, or
* interface name matches an interface with sub-interfaces
* only the IP of the interface is used. Sub-interfaces can
* be used by specifying them explicitly (by IP or name).
*
* @return SocketAddresses for the configured local interfaces,
* or an empty array if none are configured
* @throws UnknownHostException if a given interface name is invalid
*/
private static SocketAddress[] getLocalInterfaceAddrs(
String interfaceNames[]) throws UnknownHostException {
List<SocketAddress> localAddrs = new ArrayList<SocketAddress>();
for (String interfaceName : interfaceNames) {
if (InetAddresses.isInetAddress(interfaceName)) {
localAddrs.add(new InetSocketAddress(interfaceName, 0));
} else if (NetUtils.isValidSubnet(interfaceName)) {
for (InetAddress addr : NetUtils.getIPs(interfaceName, false)) {
localAddrs.add(new InetSocketAddress(addr, 0));
}
} else {
for (String ip : DNS.getIPs(interfaceName, false)) {
localAddrs.add(new InetSocketAddress(ip, 0));
}
}
}
return localAddrs.toArray(new SocketAddress[localAddrs.size()]);
}
示例8: isValidIpString
import com.google.common.net.InetAddresses; //导入依赖的package包/类
/**
* Validates IP string, both IPV6 and IPV4
* Also accounts for CIDR notation
* Examples:
* fe80::3ab1:dbff:fed1:c62d/64
* 2001:db8::
* 192.168.0.1
* 2001:db8::/116
* 192.168.0.0/32
* ::/0
*
* @param address
* @return
*/
public static boolean isValidIpString (String address) {
int slashIndex = address.lastIndexOf('/');
String ipAddressPart = address;
Integer subnetPart;
if (slashIndex != -1) {
ipAddressPart = address.substring(0, slashIndex);
try {
subnetPart = Integer.parseInt(address.substring(slashIndex + 1, address.length()));
} catch (NumberFormatException nfe) {
return false;
}
if (subnetPart < 0) {
return false;
}
if (subnetPart > 128) {
return false;
}
if (subnetPart > 32 && ipAddressPart.contains(".")) {
return false;
}
}
return InetAddresses.isInetAddress(ipAddressPart);
}
示例9: testValueOfInetAddressIPv6
import com.google.common.net.InetAddresses; //导入依赖的package包/类
/**
* Tests valueOf() converter for IPv6 InetAddress.
*/
@Test
public void testValueOfInetAddressIPv6() {
Ip6Address ipAddress;
InetAddress inetAddress;
inetAddress =
InetAddresses.forString("1111:2222:3333:4444:5555:6666:7777:8888");
ipAddress = Ip6Address.valueOf(inetAddress);
assertThat(ipAddress.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8888"));
inetAddress = InetAddresses.forString("::");
ipAddress = Ip6Address.valueOf(inetAddress);
assertThat(ipAddress.toString(), is("::"));
inetAddress =
InetAddresses.forString("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
ipAddress = Ip6Address.valueOf(inetAddress);
assertThat(ipAddress.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
}
示例10: testValueOfInetAddressIPv4
import com.google.common.net.InetAddresses; //导入依赖的package包/类
/**
* Tests valueOf() converter for IPv4 InetAddress.
*/
@Test
public void testValueOfInetAddressIPv4() {
IpAddress ipAddress;
InetAddress inetAddress;
inetAddress = InetAddresses.forString("1.2.3.4");
ipAddress = IpAddress.valueOf(inetAddress);
assertThat(ipAddress.toString(), is("1.2.3.4"));
inetAddress = InetAddresses.forString("0.0.0.0");
ipAddress = IpAddress.valueOf(inetAddress);
assertThat(ipAddress.toString(), is("0.0.0.0"));
inetAddress = InetAddresses.forString("255.255.255.255");
ipAddress = IpAddress.valueOf(inetAddress);
assertThat(ipAddress.toString(), is("255.255.255.255"));
}
示例11: testValueOfInetAddressIPv6
import com.google.common.net.InetAddresses; //导入依赖的package包/类
/**
* Tests valueOf() converter for IPv6 InetAddress.
*/
@Test
public void testValueOfInetAddressIPv6() {
IpAddress ipAddress;
InetAddress inetAddress;
inetAddress =
InetAddresses.forString("1111:2222:3333:4444:5555:6666:7777:8888");
ipAddress = IpAddress.valueOf(inetAddress);
assertThat(ipAddress.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8888"));
inetAddress = InetAddresses.forString("::");
ipAddress = IpAddress.valueOf(inetAddress);
assertThat(ipAddress.toString(), is("::"));
inetAddress =
InetAddresses.forString("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
ipAddress = IpAddress.valueOf(inetAddress);
assertThat(ipAddress.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
}
示例12: testValueOfInetAddressIPv4
import com.google.common.net.InetAddresses; //导入依赖的package包/类
/**
* Tests valueOf() converter for IPv4 InetAddress.
*/
@Test
public void testValueOfInetAddressIPv4() {
Ip4Address ipAddress;
InetAddress inetAddress;
inetAddress = InetAddresses.forString("1.2.3.4");
ipAddress = Ip4Address.valueOf(inetAddress);
assertThat(ipAddress.toString(), is("1.2.3.4"));
inetAddress = InetAddresses.forString("0.0.0.0");
ipAddress = Ip4Address.valueOf(inetAddress);
assertThat(ipAddress.toString(), is("0.0.0.0"));
inetAddress = InetAddresses.forString("255.255.255.255");
ipAddress = Ip4Address.valueOf(inetAddress);
assertThat(ipAddress.toString(), is("255.255.255.255"));
}
示例13: getValueFromrequestedObject
import com.google.common.net.InetAddresses; //导入依赖的package包/类
public String getValueFromrequestedObject(Object obj) {
if (obj instanceof BigInteger) {
return obj.toString();
} else if (obj instanceof Integer) {
return obj.toString();
} else if (obj instanceof Long) {
return obj.toString();
} else if (obj instanceof Date) {
Long tmp = ((Date) obj).getTime();
return tmp.toString();
} else if (obj instanceof InetAddress) {
Integer addr = InetAddresses.coerceToInteger((InetAddress) obj);
return addr.toString();
} else {
log.warn("input value has something problems");
return null;
}
}
示例14: makeHostResource
import com.google.common.net.InetAddresses; //导入依赖的package包/类
public static HostResource makeHostResource(
String fqhn, @Nullable String ip1, @Nullable String ip2) {
HostResource.Builder builder = new HostResource.Builder()
.setRepoId(generateNewContactHostRoid())
.setFullyQualifiedHostName(Idn.toASCII(fqhn))
.setCreationTimeForTest(DateTime.parse("2000-10-08T00:45:00Z"))
.setPersistedCurrentSponsorClientId("TheRegistrar");
if ((ip1 != null) || (ip2 != null)) {
ImmutableSet.Builder<InetAddress> ipBuilder = new ImmutableSet.Builder<>();
if (ip1 != null) {
ipBuilder.add(InetAddresses.forString(ip1));
}
if (ip2 != null) {
ipBuilder.add(InetAddresses.forString(ip2));
}
builder.setInetAddresses(ipBuilder.build());
}
return builder.build();
}
示例15: buildAlternativeNames
import com.google.common.net.InetAddresses; //导入依赖的package包/类
private GeneralNames buildAlternativeNames(CertificateGenerationRequestParameters params) {
String[] alternativeNamesList = params.getAlternativeNames();
if (alternativeNamesList == null){
return null;
}
GeneralNamesBuilder builder = new GeneralNamesBuilder();
for (String name :alternativeNamesList) {
if (InetAddresses.isInetAddress(name)) {
builder.addName(new GeneralName(GeneralName.iPAddress, name));
} else {
builder.addName(new GeneralName(GeneralName.dNSName, name));
}
}
return builder.build();
}