Class HostsFileParser

java.lang.Object
org.xbill.DNS.hosts.HostsFileParser

public final class HostsFileParser extends Object
Parses and caches the system's local hosts database, otherwise known as /etc/hosts. The cache is cleared when the file is modified.
Since:
3.4
  • Constructor Details

    • HostsFileParser

      public HostsFileParser()
      Creates a new instance based on the current OS's default. Unix and alike (or rather everything else than Windows) use /etc/hosts, while on Windows %SystemRoot%\System32\drivers\etc\hosts is used. The cache is cleared when the file has changed.
    • HostsFileParser

      public HostsFileParser(Path path)
      Creates an instance with a custom hosts database path. The cache is cleared when the file has changed.
      Parameters:
      path - The path to the hosts database.
    • HostsFileParser

      public HostsFileParser(Path path, boolean clearCacheOnChange)
      Creates an instance with a custom hosts database path.
      Parameters:
      path - The path to the hosts database.
      clearCacheOnChange - set to true to clear the cache when the hosts file changes.
  • Method Details

    • getAddressForHost

      public Optional<InetAddress> getAddressForHost(Name name, int type) throws IOException
      Performs on-demand parsing and caching of the local hosts database.
      Parameters:
      name - the hostname to search for.
      type - Record type to search for, see Type.
      Returns:
      The first address found for the requested hostname.
      Throws:
      IOException - When the parsing fails.
      IllegalArgumentException - when type is not Type.A orType.AAAA.