@Test public void testIsValidHost() { assertTrue(Hosts.isValidHost("ni.hao")); assertFalse(Hosts.isValidHost("ni.ha~o")); assertFalse(Hosts.isValidHost(".ni.hao")); assertFalse(Hosts.isValidHost("ni.hao.")); assertFalse(Hosts.isValidHost("ni..hao.")); assertFalse(Hosts.isValidHost(".")); assertFalse(Hosts.isValidHost("")); assertFalse(Hosts.isValidHost(null)); }
public static boolean isValidHost(String host) { if (host == null) { return false; } if (host.length() > 253) { return false; } int labelLength = 0; for (int i = 0, n = host.length(); i < n; i++) { char ch = host.charAt(i); if (ch == '.') { if (labelLength < 1 || labelLength > 63) { return false; } labelLength = 0; } else { labelLength++; } if ((ch < 'a' || ch > 'z') && (ch < '0' || ch > '9') && ch != '-' && ch != '.') { return false; } } if (labelLength < 1 || labelLength > 63) { return false; } return true; }
Hosts { public static boolean isValidHost(String host) { if (host == null) { return false; } if (host.length() > 253) { return false; } int labelLength = 0; for (int i = 0, n = host.length(); i < n; i++) { char ch = host.charAt(i); if (ch == '.') { if (labelLength < 1 || labelLength > 63) { return false; } labelLength = 0; } else { labelLength++; } if ((ch < 'a' || ch > 'z') && (ch < '0' || ch > '9') && ch != '-' && ch != '.') { return false; } } if (labelLength < 1 || labelLength > 63) { return false; } return true; } }
Hosts { public static boolean isValidHost(String host) { if (host == null) { return false; } if (host.length() > 253) { return false; } int labelLength = 0; for (int i = 0, n = host.length(); i < n; i++) { char ch = host.charAt(i); if (ch == '.') { if (labelLength < 1 || labelLength > 63) { return false; } labelLength = 0; } else { labelLength++; } if ((ch < 'a' || ch > 'z') && (ch < '0' || ch > '9') && ch != '-' && ch != '.') { return false; } } if (labelLength < 1 || labelLength > 63) { return false; } return true; } Hosts(Context context, String name); }
Hosts { public static boolean isValidHost(String host) { if (host == null) { return false; } if (host.length() > 253) { return false; } int labelLength = 0; for (int i = 0, n = host.length(); i < n; i++) { char ch = host.charAt(i); if (ch == '.') { if (labelLength < 1 || labelLength > 63) { return false; } labelLength = 0; } else { labelLength++; } if ((ch < 'a' || ch > 'z') && (ch < '0' || ch > '9') && ch != '-' && ch != '.') { return false; } } if (labelLength < 1 || labelLength > 63) { return false; } return true; } Hosts(Context context, String name); @Nullable InetAddress get(String host); boolean put(String host, String ip); void delete(String host); List<Pair<String, String>> getAll(); @Nullable static InetAddress toInetAddress(String host, String ip); static boolean isValidHost(String host); static boolean isValidIp(String ip); }
Hosts { public static boolean isValidHost(String host) { if (host == null) { return false; } if (host.length() > 253) { return false; } int labelLength = 0; for (int i = 0, n = host.length(); i < n; i++) { char ch = host.charAt(i); if (ch == '.') { if (labelLength < 1 || labelLength > 63) { return false; } labelLength = 0; } else { labelLength++; } if ((ch < 'a' || ch > 'z') && (ch < '0' || ch > '9') && ch != '-' && ch != '.') { return false; } } if (labelLength < 1 || labelLength > 63) { return false; } return true; } Hosts(Context context, String name); @Nullable InetAddress get(String host); boolean put(String host, String ip); void delete(String host); List<Pair<String, String>> getAll(); @Nullable static InetAddress toInetAddress(String host, String ip); static boolean isValidHost(String host); static boolean isValidIp(String ip);  }