当前位置: 首页>>代码示例>>Java>>正文


Java IPAddressUtil类代码示例

本文整理汇总了Java中sun.net.util.IPAddressUtil的典型用法代码示例。如果您正苦于以下问题:Java IPAddressUtil类的具体用法?Java IPAddressUtil怎么用?Java IPAddressUtil使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


IPAddressUtil类属于sun.net.util包,在下文中一共展示了IPAddressUtil类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getByAddress

import sun.net.util.IPAddressUtil; //导入依赖的package包/类
/**
 * Creates an InetAddress based on the provided host name and IP address.
 * No name service is checked for the validity of the address.
 *
 * <p> The host name can either be a machine name, such as
 * "{@code java.sun.com}", or a textual representation of its IP
 * address.
 * <p> No validity checking is done on the host name either.
 *
 * <p> If addr specifies an IPv4 address an instance of Inet4Address
 * will be returned; otherwise, an instance of Inet6Address
 * will be returned.
 *
 * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array
 * must be 16 bytes long
 *
 * @param host the specified host
 * @param addr the raw IP address in network byte order
 * @return  an InetAddress object created from the raw IP address.
 * @exception  UnknownHostException  if IP address is of illegal length
 * @since 1.4
 */
public static InetAddress getByAddress(String host, byte[] addr)
    throws UnknownHostException {
    if (host != null && host.length() > 0 && host.charAt(0) == '[') {
        if (host.charAt(host.length()-1) == ']') {
            host = host.substring(1, host.length() -1);
        }
    }
    if (addr != null) {
        if (addr.length == Inet4Address.INADDRSZ) {
            return new Inet4Address(host, addr);
        } else if (addr.length == Inet6Address.INADDRSZ) {
            byte[] newAddr
                = IPAddressUtil.convertFromIPv4MappedAddress(addr);
            if (newAddr != null) {
                return new Inet4Address(host, newAddr);
            } else {
                return new Inet6Address(host, addr);
            }
        }
    }
    throw new UnknownHostException("addr is of illegal length");
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:45,代码来源:InetAddress.java

示例2: rawToSNIHostName

import sun.net.util.IPAddressUtil; //导入依赖的package包/类
/**
 * Converts string hostname to {@code SNIHostName}.
 * <P>
 * Note that to check whether a hostname is a valid domain name, we cannot
 * use the hostname resolved from name services.  For virtual hosting,
 * multiple hostnames may be bound to the same IP address, so the hostname
 * resolved from name services is not always reliable.
 *
 * @param  hostname
 *         the raw hostname
 * @return an instance of {@link SNIHostName}, or null if the hostname does
 *         not look like a FQDN
 */
private static SNIHostName rawToSNIHostName(String hostname) {
    SNIHostName sniHostName = null;
    if (hostname != null && hostname.indexOf('.') > 0 &&
            !hostname.endsWith(".") &&
            !IPAddressUtil.isIPv4LiteralAddress(hostname) &&
            !IPAddressUtil.isIPv6LiteralAddress(hostname)) {

        try {
            sniHostName = new SNIHostName(hostname);
        } catch (IllegalArgumentException iae) {
            // don't bother to handle illegal host_name
            if (Debug.isOn("ssl")) {
                System.out.println(Thread.currentThread().getName() +
                    ", \"" + hostname + "\" " +
                    "is not a legal HostName for  server name indication");
            }
        }
    }

    return sniHostName;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:35,代码来源:Utilities.java

示例3: evaluateVersion

import sun.net.util.IPAddressUtil; //导入依赖的package包/类
private boolean evaluateVersion(Map<String, String> params) {
    String left = leftSide.getStringValue(params);
    String right = rightSide.getStringValue(params);
    if (left == null || right == null) return false;
    String[] leftTokens = left.isEmpty() || IPAddressUtil.isIPv6LiteralAddress(left)
            ? new String[]{}
            : left.split("\\.");
    String[] rightTokens = right.isEmpty() || IPAddressUtil.isIPv6LiteralAddress(right)
            ? new String[]{}
            : right.split("\\.");
    int largest = Math.max(leftTokens.length, rightTokens.length);
    int[] leftNumbers = new int[largest];
    int[] rightNumbers = new int[largest];
    for (int i = 0; i < largest; i++) {
        leftNumbers[i] = i < leftTokens.length ? getIntValue(leftTokens[i]) : 0;
        rightNumbers[i] = i < rightTokens.length ? getIntValue(rightTokens[i]) : 0;
    }
    return evaluate(leftNumbers, rightNumbers);
}
 
开发者ID:Comcast,项目名称:redirector,代码行数:20,代码来源:RelationalExpression.java

示例4: addrToString

import sun.net.util.IPAddressUtil; //导入依赖的package包/类
private  String addrToString(byte addr[]) {
  String stringifiedAddress = null;

    if (addr.length == Inet4Address.INADDRSZ) {
        stringifiedAddress = Inet4Address.numericToTextFormat(addr);
    } else { // treat as an IPV6 jobby
        byte[] newAddr
            = IPAddressUtil.convertFromIPv4MappedAddress(addr);
        if (newAddr != null) {
           stringifiedAddress = Inet4Address.numericToTextFormat(addr);
        } else {
            stringifiedAddress = Inet6Address.numericToTextFormat(addr);
        }
    }
    return stringifiedAddress;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:InetAddress.java

示例5: getByAddress

import sun.net.util.IPAddressUtil; //导入依赖的package包/类
/**
    * Create an InetAddress based on the provided host name and IP address
    * No name service is checked for the validity of the address. 
    *
    * <p> The host name can either be a machine name, such as
    * "<code>java.sun.com</code>", or a textual representation of its IP
    * address.
    * <p> No validity checking is done on the host name either.
    *
    * <p> If addr specifies an IPv4 address an instance of Inet4Address 
    * will be returned; otherwise, an instance of Inet6Address 
    * will be returned.
    *
    * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array 
    * must be 16 bytes long
    *
    * @param host the specified host
    * @param addr the raw IP address in network byte order
    * @return  an InetAddress object created from the raw IP address.
    * @exception  UnknownHostException  if IP address is of illegal length
    * @since 1.4
    */
   public static InetAddress getByAddress(String host, byte[] addr) 
throws UnknownHostException {
if (host != null && host.length() > 0 && host.charAt(0) == '[') {
    if (host.charAt(host.length()-1) == ']') {
	host = host.substring(1, host.length() -1);
    }
}
if (addr != null) {
    if (addr.length == Inet4Address.INADDRSZ) {
	return new Inet4Address(host, addr);
    } else if (addr.length == Inet6Address.INADDRSZ) {
	byte[] newAddr 
	    = IPAddressUtil.convertFromIPv4MappedAddress(addr);
	if (newAddr != null) {
	    return new Inet4Address(host, newAddr);
	} else {
	    return new Inet6Address(host, addr); 
	}
    } 
} 
throw new UnknownHostException("addr is of illegal length");
   }
 
开发者ID:jgaltidor,项目名称:VarJ,代码行数:45,代码来源:InetAddress.java

示例6: toIPv6URLFormat

import sun.net.util.IPAddressUtil; //导入依赖的package包/类
public static String toIPv6URLFormat(final String host)
{
   try
   {
      if (host.startsWith("[") || host.startsWith(":"))
      {
         if (System.getProperty("java.net.preferIPv4Stack") == null)
         {
            throw new IllegalStateException("always provide java.net.preferIPv4Stack JVM property when using IPv6 address format");
         }
         if (System.getProperty("java.net.preferIPv6Addresses") == null)
         {
            throw new IllegalStateException("always provide java.net.preferIPv6Addresses JVM property when using IPv6 address format");
         }
      }
      final boolean isIPv6Address = InetAddress.getByName(host) instanceof Inet6Address;
      final boolean isIPv6Literal = isIPv6Address && IPAddressUtil.isIPv6LiteralAddress(host.replaceAll("^\\[(.*)\\]$","$1"));
      final boolean isIPv6LiteralFormattedForURI = isIPv6Literal && host.startsWith("[");
      return isIPv6Literal && !isIPv6LiteralFormattedForURI ? "[" + host + "]" : host;
   }
   catch (final UnknownHostException e)
   {
      throw new RuntimeException(e);
   }
}
 
开发者ID:jbossws,项目名称:jbossws-cxf,代码行数:26,代码来源:JBossWSTestHelper.java

示例7: getByAddress

import sun.net.util.IPAddressUtil; //导入依赖的package包/类
/**
 * Creates an InetAddress based on the provided host name and IP address.
 * No name service is checked for the validity of the address.
 *
 * <p> The host name can either be a machine name, such as
 * "<code>java.sun.com</code>", or a textual representation of its IP
 * address.
 * <p> No validity checking is done on the host name either.
 *
 * <p> If addr specifies an IPv4 address an instance of Inet4Address
 * will be returned; otherwise, an instance of Inet6Address
 * will be returned.
 *
 * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array
 * must be 16 bytes long
 *
 * @param host the specified host
 * @param addr the raw IP address in network byte order
 * @return  an InetAddress object created from the raw IP address.
 * @exception  UnknownHostException  if IP address is of illegal length
 * @since 1.4
 */
public static InetAddress getByAddress(String host, byte[] addr)
    throws UnknownHostException {
    if (host != null && host.length() > 0 && host.charAt(0) == '[') {
        if (host.charAt(host.length()-1) == ']') {
            host = host.substring(1, host.length() -1);
        }
    }
    if (addr != null) {
        if (addr.length == Inet4Address.INADDRSZ) {
            return new Inet4Address(host, addr);
        } else if (addr.length == Inet6Address.INADDRSZ) {
            byte[] newAddr
                = IPAddressUtil.convertFromIPv4MappedAddress(addr);
            if (newAddr != null) {
                return new Inet4Address(host, newAddr);
            } else {
                return new Inet6Address(host, addr);
            }
        }
    }
    throw new UnknownHostException("addr is of illegal length");
}
 
开发者ID:ZhaoX,项目名称:jdk-1.7-annotated,代码行数:45,代码来源:InetAddress.java

示例8: appendIfLiteralAddress

import sun.net.util.IPAddressUtil; //导入依赖的package包/类
private static void appendIfLiteralAddress(String addr, StringBuffer sb) {
    if (IPAddressUtil.isIPv4LiteralAddress(addr)) {
        sb.append("dns://" + addr + " ");
    } else {
        if (IPAddressUtil.isIPv6LiteralAddress(addr)) {
            sb.append("dns://[" + addr + "] ");
        }
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:10,代码来源:DNSNameService.java

示例9: evaluateIPv6

import sun.net.util.IPAddressUtil; //导入依赖的package包/类
private boolean evaluateIPv6(Map<String, String> params) {
    String left = leftSide.getStringValue(params);
    String right = rightSide.getStringValue(params);
    if (left == null || right == null) return false;
    return evaluate(
            IPAddressUtil.isIPv6LiteralAddress(left) ? IPAddressUtil.textToNumericFormatV6(left) : new byte[]{},
            IPAddressUtil.isIPv6LiteralAddress(right) ? IPAddressUtil.textToNumericFormatV6(right) : new byte[]{});
}
 
开发者ID:Comcast,项目名称:redirector,代码行数:9,代码来源:RelationalExpression.java

示例10: createAddressByteArray

import sun.net.util.IPAddressUtil; //导入依赖的package包/类
private byte [] createAddressByteArray(String addrStr) {
    byte[] addrArray;
    // check if IPV4 address - most likely
    addrArray = IPAddressUtil.textToNumericFormatV4(addrStr);
    if (addrArray == null) {
        addrArray = IPAddressUtil.textToNumericFormatV6(addrStr);
    }
    return addrArray;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:10,代码来源:InetAddress.java

示例11: getServerName

import sun.net.util.IPAddressUtil; //导入依赖的package包/类
/**
 * If the address was created with a domain name, then return
 * the domain name string. If created with a literal IP address
 * then return null. We do this to avoid doing a reverse lookup
 * Used to populate the TLS SNI parameter. So, SNI is only set
 * when a domain name was supplied.
 */
public static String getServerName(InetSocketAddress addr) {
    String host = addr.getHostString();
    if (IPAddressUtil.textToNumericFormatV4(host) != null)
        return null;
    if (IPAddressUtil.textToNumericFormatV6(host) != null)
        return null;
    return host;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:16,代码来源:Utils.java

示例12: unresolvedHostToNormalizedString

import sun.net.util.IPAddressUtil; //导入依赖的package包/类
/**
 * Returns an address in a normalized format for Akka.
 * When an IPv6 address is specified, it normalizes the IPv6 address to avoid
 * complications with the exact URL match policy of Akka.
 * @param host The hostname, IPv4 or IPv6 address
 * @return host which will be normalized if it is an IPv6 address
 */
public static String unresolvedHostToNormalizedString(String host) {
	// Return loopback interface address if host is null
	// This represents the behavior of {@code InetAddress.getByName } and RFC 3330
	if (host == null) {
		host = InetAddress.getLoopbackAddress().getHostAddress();
	} else {
		host = host.trim().toLowerCase();
	}

	// normalize and valid address
	if (IPAddressUtil.isIPv6LiteralAddress(host)) {
		byte[] ipV6Address = IPAddressUtil.textToNumericFormatV6(host);
		host = getIPv6UrlRepresentation(ipV6Address);
	} else if (!IPAddressUtil.isIPv4LiteralAddress(host)) {
		try {
			// We don't allow these in hostnames
			Preconditions.checkArgument(!host.startsWith("."));
			Preconditions.checkArgument(!host.endsWith("."));
			Preconditions.checkArgument(!host.contains(":"));
		} catch (Exception e) {
			throw new IllegalConfigurationException("The configured hostname is not valid", e);
		}
	}

	return host;
}
 
开发者ID:axbaretto,项目名称:flink,代码行数:34,代码来源:NetUtils.java

示例13: intOfIpV4

import sun.net.util.IPAddressUtil; //导入依赖的package包/类
public int intOfIpV4(String ip) {
	int result = 0;
	byte[] bytes = IPAddressUtil.textToNumericFormatV4(ip);
	if (bytes == null) {
		return result;
	}
	for (byte b : bytes) {
		result = result << 8 | (b & 0xFF);
	}
	return result;
}
 
开发者ID:bejondshao,项目名称:personal,代码行数:12,代码来源:IPConverter.java

示例14: testCmdRequestDecoderIPv6

import sun.net.util.IPAddressUtil; //导入依赖的package包/类
@Test
public void testCmdRequestDecoderIPv6() {
    String[] hosts = {SocksCommonUtils.ipv6toStr(IPAddressUtil.textToNumericFormatV6("::1"))};
    int[] ports = {1, 32769, 65535};
    for (SocksCmdType cmdType : SocksCmdType.values()) {
        for (String host : hosts) {
            for (int port : ports) {
                testSocksCmdRequestDecoderWithDifferentParams(cmdType, SocksAddressType.IPv6, host, port);
            }
        }
    }
}
 
开发者ID:wuyinxian124,项目名称:netty4.0.27Learn,代码行数:13,代码来源:SocksCmdRequestDecoderTest.java


注:本文中的sun.net.util.IPAddressUtil类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。