brooklyn.util.net.Networking.isValidIp4()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(2.6k)|赞(0)|评价(0)|浏览(103)

本文整理了Java中brooklyn.util.net.Networking.isValidIp4()方法的一些代码示例,展示了Networking.isValidIp4()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Networking.isValidIp4()方法的具体详情如下:
包路径:brooklyn.util.net.Networking
类名称:Networking
方法名:isValidIp4

Networking.isValidIp4介绍

[英]checks whether given string matches a valid numeric IP (v4) address, e.g. 127.0.0.1, but not localhost or 1.2.3.256
[中]检查给定字符串是否匹配有效的数字IP(v4)地址,例如127.0.0.1,但不匹配localhost或1.2.3.256

代码示例

代码示例来源:origin: io.brooklyn/brooklyn-utils-common

/**
 * Gets an InetAddress using the given hostname or IP. If it is an IPv4 address, then this is equivalent
 * to {@link getInetAddressWithFixedName(byte[])}. If it is a hostname, then this hostname will be used
 * in the returned InetAddress.
 */
public static InetAddress getInetAddressWithFixedName(String hostnameOrIp) {
  try {
    if (isValidIp4(hostnameOrIp)) {
      byte[] ip = new byte[4];
      String[] parts = hostnameOrIp.split("\\.");
      assert parts.length == 4 : "val="+hostnameOrIp+"; split="+Arrays.toString(parts)+"; length="+parts.length;
      for (int i = 0; i < parts.length; i++) {
        ip[i] = (byte)Integer.parseInt(parts[i]);
      }
      return InetAddress.getByAddress(hostnameOrIp, ip);
    } else {
      return InetAddress.getByName(hostnameOrIp);
    }
  } catch (UnknownHostException e) {
    throw Throwables.propagate(e);
  }
}

代码示例来源:origin: io.brooklyn/brooklyn-locations-jclouds

@Override
public String getSubnetIp() {
  Optional<String> privateAddress = getPrivateAddress();
  if (privateAddress.isPresent()) {
    return privateAddress.get();
  }
  
  String hostname = jcloudsParent.getPublicHostname(node, getRawLocalConfigBag());
  if (hostname != null && !Networking.isValidIp4(hostname)) {
    try {
      return InetAddress.getByName(hostname).getHostAddress();
    } catch (UnknownHostException e) {
      LOG.debug("Cannot resolve IP for hostname {} of machine {} (so returning hostname): {}", new Object[] {hostname, this, e});
    }
  }
  return hostname;
}

代码示例来源:origin: io.brooklyn/brooklyn-locations-jclouds

/** returns the hostname (or sometimes IP) for use by peers in the same subnet,
 * defaulting to public hostname if nothing special
 * <p>
 * for use e.g. in clouds like amazon where other machines
 * in the same subnet need to use a different IP
 */
@Override
public String getSubnetHostname() {
  String publicHostname = jcloudsParent.getPublicHostname(node, getRawLocalConfigBag());
  
  if ("aws-ec2".equals(jcloudsParent.getProvider())) {
    // prefer hostname over IP for aws (resolves to private ip in subnet, and to public from outside)
    if (!Networking.isValidIp4(publicHostname)) {
      return publicHostname; // assume it's a hostname; could check for ip6!
    }
  }
  Optional<String> privateAddress = getPrivateAddress();
  return privateAddress.isPresent() ? privateAddress.get() : publicHostname;
}

相关文章