當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。