@Test public void testCreateByteArrayFromIpAddressString() { for (Entry<String, String> e : validIpV4Hosts.entrySet()) { assertHexDumpEquals(e.getValue(), NetUtil.createByteArrayFromIpAddressString(e.getKey())); } for (Entry<String, String> e : invalidIpV4Hosts.entrySet()) { assertHexDumpEquals(e.getValue(), NetUtil.createByteArrayFromIpAddressString(e.getKey())); } for (Entry<String, String> e : validIpV6Hosts.entrySet()) { assertHexDumpEquals(e.getValue(), NetUtil.createByteArrayFromIpAddressString(e.getKey())); } for (Entry<String, String> e : invalidIpV6Hosts.entrySet()) { assertHexDumpEquals(e.getValue(), NetUtil.createByteArrayFromIpAddressString(e.getKey())); } }
public static byte[] createByteArrayFromIpAddressString(String ipAddressString) { if (isValidIpV4Address(ipAddressString)) { StringTokenizer tokenizer = new StringTokenizer(ipAddressString, "."); String token; int tempInt; byte[] byteAddress = new byte[IPV4_BYTE_COUNT]; for (int i = 0; i < IPV4_BYTE_COUNT; i ++) { token = tokenizer.nextToken(); tempInt = Integer.parseInt(token); byteAddress[i] = (byte) tempInt; } return byteAddress; } if (isValidIpV6Address(ipAddressString)) { if (ipAddressString.charAt(0) == '[') { ipAddressString = ipAddressString.substring(1, ipAddressString.length() - 1); } int percentPos = ipAddressString.indexOf('%'); if (percentPos >= 0) { ipAddressString = ipAddressString.substring(0, percentPos); } StringTokenizer tokenizer = new StringTokenizer(ipAddressString, ":.", true); ArrayList<String> hexStrings = new ArrayList<String>(); ArrayList<String> decStrings = new ArrayList<String>(); String token = ""; String prevToken = ""; int doubleColonIndex = -1; while (tokenizer.hasMoreTokens()) { prevToken = token; token = tokenizer.nextToken(); if (":".equals(token)) { if (":".equals(prevToken)) { doubleColonIndex = hexStrings.size(); } else if (!prevToken.isEmpty()) { hexStrings.add(prevToken); } } else if (".".equals(token)) { decStrings.add(prevToken); } } if (":".equals(prevToken)) { if (":".equals(token)) { doubleColonIndex = hexStrings.size(); } else { hexStrings.add(token); } } else if (".".equals(prevToken)) { decStrings.add(token); } int hexStringsLength = 8; if (!decStrings.isEmpty()) { hexStringsLength -= 2; } if (doubleColonIndex != -1) { int numberToInsert = hexStringsLength - hexStrings.size(); for (int i = 0; i < numberToInsert; i ++) { hexStrings.add(doubleColonIndex, "0"); } } byte[] ipByteArray = new byte[IPV6_BYTE_COUNT]; for (int i = 0; i < hexStrings.size(); i ++) { convertToBytes(hexStrings.get(i), ipByteArray, i << 1); } for (int i = 0; i < decStrings.size(); i ++) { ipByteArray[i + 12] = (byte) (Integer.parseInt(decStrings.get(i)) & 255); } return ipByteArray; } return null; }
NetUtil { public static byte[] createByteArrayFromIpAddressString(String ipAddressString) { if (isValidIpV4Address(ipAddressString)) { StringTokenizer tokenizer = new StringTokenizer(ipAddressString, "."); String token; int tempInt; byte[] byteAddress = new byte[IPV4_BYTE_COUNT]; for (int i = 0; i < IPV4_BYTE_COUNT; i ++) { token = tokenizer.nextToken(); tempInt = Integer.parseInt(token); byteAddress[i] = (byte) tempInt; } return byteAddress; } if (isValidIpV6Address(ipAddressString)) { if (ipAddressString.charAt(0) == '[') { ipAddressString = ipAddressString.substring(1, ipAddressString.length() - 1); } int percentPos = ipAddressString.indexOf('%'); if (percentPos >= 0) { ipAddressString = ipAddressString.substring(0, percentPos); } StringTokenizer tokenizer = new StringTokenizer(ipAddressString, ":.", true); ArrayList<String> hexStrings = new ArrayList<String>(); ArrayList<String> decStrings = new ArrayList<String>(); String token = ""; String prevToken = ""; int doubleColonIndex = -1; while (tokenizer.hasMoreTokens()) { prevToken = token; token = tokenizer.nextToken(); if (":".equals(token)) { if (":".equals(prevToken)) { doubleColonIndex = hexStrings.size(); } else if (!prevToken.isEmpty()) { hexStrings.add(prevToken); } } else if (".".equals(token)) { decStrings.add(prevToken); } } if (":".equals(prevToken)) { if (":".equals(token)) { doubleColonIndex = hexStrings.size(); } else { hexStrings.add(token); } } else if (".".equals(prevToken)) { decStrings.add(token); } int hexStringsLength = 8; if (!decStrings.isEmpty()) { hexStringsLength -= 2; } if (doubleColonIndex != -1) { int numberToInsert = hexStringsLength - hexStrings.size(); for (int i = 0; i < numberToInsert; i ++) { hexStrings.add(doubleColonIndex, "0"); } } byte[] ipByteArray = new byte[IPV6_BYTE_COUNT]; for (int i = 0; i < hexStrings.size(); i ++) { convertToBytes(hexStrings.get(i), ipByteArray, i << 1); } for (int i = 0; i < decStrings.size(); i ++) { ipByteArray[i + 12] = (byte) (Integer.parseInt(decStrings.get(i)) & 255); } return ipByteArray; } return null; } }
NetUtil { public static byte[] createByteArrayFromIpAddressString(String ipAddressString) { if (isValidIpV4Address(ipAddressString)) { StringTokenizer tokenizer = new StringTokenizer(ipAddressString, "."); String token; int tempInt; byte[] byteAddress = new byte[IPV4_BYTE_COUNT]; for (int i = 0; i < IPV4_BYTE_COUNT; i ++) { token = tokenizer.nextToken(); tempInt = Integer.parseInt(token); byteAddress[i] = (byte) tempInt; } return byteAddress; } if (isValidIpV6Address(ipAddressString)) { if (ipAddressString.charAt(0) == '[') { ipAddressString = ipAddressString.substring(1, ipAddressString.length() - 1); } int percentPos = ipAddressString.indexOf('%'); if (percentPos >= 0) { ipAddressString = ipAddressString.substring(0, percentPos); } StringTokenizer tokenizer = new StringTokenizer(ipAddressString, ":.", true); ArrayList<String> hexStrings = new ArrayList<String>(); ArrayList<String> decStrings = new ArrayList<String>(); String token = ""; String prevToken = ""; int doubleColonIndex = -1; while (tokenizer.hasMoreTokens()) { prevToken = token; token = tokenizer.nextToken(); if (":".equals(token)) { if (":".equals(prevToken)) { doubleColonIndex = hexStrings.size(); } else if (!prevToken.isEmpty()) { hexStrings.add(prevToken); } } else if (".".equals(token)) { decStrings.add(prevToken); } } if (":".equals(prevToken)) { if (":".equals(token)) { doubleColonIndex = hexStrings.size(); } else { hexStrings.add(token); } } else if (".".equals(prevToken)) { decStrings.add(token); } int hexStringsLength = 8; if (!decStrings.isEmpty()) { hexStringsLength -= 2; } if (doubleColonIndex != -1) { int numberToInsert = hexStringsLength - hexStrings.size(); for (int i = 0; i < numberToInsert; i ++) { hexStrings.add(doubleColonIndex, "0"); } } byte[] ipByteArray = new byte[IPV6_BYTE_COUNT]; for (int i = 0; i < hexStrings.size(); i ++) { convertToBytes(hexStrings.get(i), ipByteArray, i << 1); } for (int i = 0; i < decStrings.size(); i ++) { ipByteArray[i + 12] = (byte) (Integer.parseInt(decStrings.get(i)) & 255); } return ipByteArray; } return null; } private  NetUtil(); }
NetUtil { public static byte[] createByteArrayFromIpAddressString(String ipAddressString) { if (isValidIpV4Address(ipAddressString)) { StringTokenizer tokenizer = new StringTokenizer(ipAddressString, "."); String token; int tempInt; byte[] byteAddress = new byte[IPV4_BYTE_COUNT]; for (int i = 0; i < IPV4_BYTE_COUNT; i ++) { token = tokenizer.nextToken(); tempInt = Integer.parseInt(token); byteAddress[i] = (byte) tempInt; } return byteAddress; } if (isValidIpV6Address(ipAddressString)) { if (ipAddressString.charAt(0) == '[') { ipAddressString = ipAddressString.substring(1, ipAddressString.length() - 1); } int percentPos = ipAddressString.indexOf('%'); if (percentPos >= 0) { ipAddressString = ipAddressString.substring(0, percentPos); } StringTokenizer tokenizer = new StringTokenizer(ipAddressString, ":.", true); ArrayList<String> hexStrings = new ArrayList<String>(); ArrayList<String> decStrings = new ArrayList<String>(); String token = ""; String prevToken = ""; int doubleColonIndex = -1; while (tokenizer.hasMoreTokens()) { prevToken = token; token = tokenizer.nextToken(); if (":".equals(token)) { if (":".equals(prevToken)) { doubleColonIndex = hexStrings.size(); } else if (!prevToken.isEmpty()) { hexStrings.add(prevToken); } } else if (".".equals(token)) { decStrings.add(prevToken); } } if (":".equals(prevToken)) { if (":".equals(token)) { doubleColonIndex = hexStrings.size(); } else { hexStrings.add(token); } } else if (".".equals(prevToken)) { decStrings.add(token); } int hexStringsLength = 8; if (!decStrings.isEmpty()) { hexStringsLength -= 2; } if (doubleColonIndex != -1) { int numberToInsert = hexStringsLength - hexStrings.size(); for (int i = 0; i < numberToInsert; i ++) { hexStrings.add(doubleColonIndex, "0"); } } byte[] ipByteArray = new byte[IPV6_BYTE_COUNT]; for (int i = 0; i < hexStrings.size(); i ++) { convertToBytes(hexStrings.get(i), ipByteArray, i << 1); } for (int i = 0; i < decStrings.size(); i ++) { ipByteArray[i + 12] = (byte) (Integer.parseInt(decStrings.get(i)) & 255); } return ipByteArray; } return null; } private  NetUtil(); static boolean isIpV4StackPreferred(); static byte[] createByteArrayFromIpAddressString(String ipAddressString); static boolean isValidIpV6Address(String ipAddress); @Deprecated static boolean isValidIp4Word(String word); static boolean isValidIpV4Address(String value); static Inet6Address getByName(CharSequence ip); static Inet6Address getByName(CharSequence ip, boolean ipv4Mapped); static String toAddressString(InetAddress ip); static String toAddressString(InetAddress ip, boolean ipv4Mapped); }
NetUtil { public static byte[] createByteArrayFromIpAddressString(String ipAddressString) { if (isValidIpV4Address(ipAddressString)) { StringTokenizer tokenizer = new StringTokenizer(ipAddressString, "."); String token; int tempInt; byte[] byteAddress = new byte[IPV4_BYTE_COUNT]; for (int i = 0; i < IPV4_BYTE_COUNT; i ++) { token = tokenizer.nextToken(); tempInt = Integer.parseInt(token); byteAddress[i] = (byte) tempInt; } return byteAddress; } if (isValidIpV6Address(ipAddressString)) { if (ipAddressString.charAt(0) == '[') { ipAddressString = ipAddressString.substring(1, ipAddressString.length() - 1); } int percentPos = ipAddressString.indexOf('%'); if (percentPos >= 0) { ipAddressString = ipAddressString.substring(0, percentPos); } StringTokenizer tokenizer = new StringTokenizer(ipAddressString, ":.", true); ArrayList<String> hexStrings = new ArrayList<String>(); ArrayList<String> decStrings = new ArrayList<String>(); String token = ""; String prevToken = ""; int doubleColonIndex = -1; while (tokenizer.hasMoreTokens()) { prevToken = token; token = tokenizer.nextToken(); if (":".equals(token)) { if (":".equals(prevToken)) { doubleColonIndex = hexStrings.size(); } else if (!prevToken.isEmpty()) { hexStrings.add(prevToken); } } else if (".".equals(token)) { decStrings.add(prevToken); } } if (":".equals(prevToken)) { if (":".equals(token)) { doubleColonIndex = hexStrings.size(); } else { hexStrings.add(token); } } else if (".".equals(prevToken)) { decStrings.add(token); } int hexStringsLength = 8; if (!decStrings.isEmpty()) { hexStringsLength -= 2; } if (doubleColonIndex != -1) { int numberToInsert = hexStringsLength - hexStrings.size(); for (int i = 0; i < numberToInsert; i ++) { hexStrings.add(doubleColonIndex, "0"); } } byte[] ipByteArray = new byte[IPV6_BYTE_COUNT]; for (int i = 0; i < hexStrings.size(); i ++) { convertToBytes(hexStrings.get(i), ipByteArray, i << 1); } for (int i = 0; i < decStrings.size(); i ++) { ipByteArray[i + 12] = (byte) (Integer.parseInt(decStrings.get(i)) & 255); } return ipByteArray; } return null; } private  NetUtil(); static boolean isIpV4StackPreferred(); static byte[] createByteArrayFromIpAddressString(String ipAddressString); static boolean isValidIpV6Address(String ipAddress); @Deprecated static boolean isValidIp4Word(String word); static boolean isValidIpV4Address(String value); static Inet6Address getByName(CharSequence ip); static Inet6Address getByName(CharSequence ip, boolean ipv4Mapped); static String toAddressString(InetAddress ip); static String toAddressString(InetAddress ip, boolean ipv4Mapped); static final Inet4Address LOCALHOST4; static final Inet6Address LOCALHOST6; static final InetAddress LOCALHOST; static final NetworkInterface LOOPBACK_IF; static final int SOMAXCONN; }