Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright (C) 2009 The Guava Authors
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
A syntactically valid host specifier, suitable for use in a URI. This may be either a numeric IP address in IPv4 or IPv6 notation, or a domain name.

Because this class is intended to represent host specifiers which can reasonably be used in a URI, the domain name case is further restricted to include only those domain names which end in a recognized public suffix; see InternetDomainName.isPublicSuffix() for details.

Note that no network lookups are performed by any HostSpecifier methods. No attempt is made to verify that a provided specifier corresponds to a real or accessible host. Only syntactic and pattern-based checks are performed.

If you know that a given string represents a numeric IP address, use InetAddresses to obtain and manipulate a instance from it rather than using this class. Similarly, if you know that a given string represents a domain name, use InternetDomainName rather than this class.

Craig Berry
 public final class HostSpecifier {
   private final String canonicalForm;
   private HostSpecifier(String canonicalForm) {
     this. = canonicalForm;

Returns a HostSpecifier built from the provided specifier, which is already known to be valid. If the specifier might be invalid, use from(java.lang.String) instead.

The specifier must be in one of these formats:

  • A domain name, like
  • A IPv4 address string, like
  • An IPv6 address string with or without brackets, like [2001:db8::1] or 2001:db8::1

java.lang.IllegalArgumentException if the specifier is not valid.
   public static HostSpecifier fromValid(String specifier) {
     // Verify that no port was specified, and strip optional brackets from
     // IPv6 literals.
     final HostAndPort parsedHost = HostAndPort.fromString(specifier);
     final String host = parsedHost.getHostText();
     // Try to interpret the specifier as an IP address.  Note we build
     // the address rather than using the .is* methods because we want to
     // use InetAddresses.toUriString to convert the result to a string in
     // canonical form.
     InetAddress addr = null;
     try {
       addr = InetAddresses.forString(host);
     } catch (IllegalArgumentException e) {
       // It is not an IPv4 or IPv6 literal
     if (addr != null) {
       return new HostSpecifier(InetAddresses.toUriString(addr));
     // It is not any kind of IP address; must be a domain name or invalid.
     // TODO(user): different versions of this for different factories?
    final InternetDomainName domain = InternetDomainName.from(host);
    if (domain.hasPublicSuffix()) {
      return new HostSpecifier(;
    throw new IllegalArgumentException(
        "Domain name does not have a recognized public suffix: " + host);

Attempts to return a HostSpecifier for the given string, throwing an exception if parsing fails. Always use this method in preference to fromValid(java.lang.String) for a specifier that is not already known to be valid.

java.text.ParseException if the specifier is not valid.
  public static HostSpecifier from(String specifier)
      throws ParseException {
    try {
      return fromValid(specifier);
    } catch (IllegalArgumentException e) {
      // Since the IAE can originate at several different points inside
      // fromValid(), we implement this method in terms of that one rather
      // than the reverse.
      ParseException parseException =
          new ParseException("Invalid host specifier: " + specifier, 0);
      throw parseException;

Determines whether specifier represents a valid HostSpecifier as described in the documentation for fromValid(java.lang.String).
  public static boolean isValid(String specifier) {
    try {
      return true;
    } catch (IllegalArgumentException e) {
      return false;
  public boolean equals(@Nullable Object other) {
    if (this == other) {
      return true;
    if (other instanceof HostSpecifier) {
      final HostSpecifier that = (HostSpecifierother;
      return this..equals(that.canonicalForm);
    return false;
  public int hashCode() {
    return .hashCode();

Returns a string representation of the host specifier suitable for inclusion in a URI. If the host specifier is a domain name, the string will be normalized to all lower case. If the specifier was an IPv6 address without brackets, brackets are added so that the result will be usable in the host part of a URI.
  public String toString() {
    return ;
New to GrepCode? Check out our FAQ X