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;
  }
@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));
  }