Package org.xbill.DNS.hosts
Class HostsFileParser
java.lang.Object
org.xbill.DNS.hosts.HostsFileParser
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
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance based on the current OS's default.HostsFileParser(Path path) Creates an instance with a custom hosts database path.HostsFileParser(Path path, boolean clearCacheOnChange) Creates an instance with a custom hosts database path. -
Method Summary
Modifier and TypeMethodDescription(package private) intprivate voidgetAddressForHost(Name name, int type) Performs on-demand parsing and caching of the local hosts database.private String[]getLineTokens(String line) private Stringprivate voidprivate HostsFileParser.LineDataparseLine(int lineNumber, String line, AtomicInteger addressFailures, AtomicInteger nameFailures) private voidprivate NamesafeName(String name, int lineNumber, AtomicInteger nameFailures) private voidsearchHostsFileForEntry(Name name, int type) (package private) voidprivate void
-
Field Details
-
maxFullCacheFileSizeBytes
private final int maxFullCacheFileSizeBytes -
fileChangeCheckInterval
-
path
-
clearCacheOnChange
private final boolean clearCacheOnChange -
clock
-
hostsCache
-
lastFileModificationCheckTime
-
lastFileReadTime
-
isEntireFileParsed
private boolean isEntireFileParsed -
hostsFileWarningLogged
private boolean hostsFileWarningLogged -
hostsFileSizeBytes
private long hostsFileSizeBytes
-
-
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\hostsis used. The cache is cleared when the file has changed. -
HostsFileParser
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
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
Performs on-demand parsing and caching of the local hosts database.- Parameters:
name- the hostname to search for.type- Record type to search for, seeType.- Returns:
- The first address found for the requested hostname.
- Throws:
IOException- When the parsing fails.IllegalArgumentException- whentypeis notType.AorType.AAAA.
-
parseEntireHostsFile
- Throws:
IOException
-
searchHostsFileForEntry
- Throws:
IOException
-
parseLine
private HostsFileParser.LineData parseLine(int lineNumber, String line, AtomicInteger addressFailures, AtomicInteger nameFailures) -
safeName
-
getLineTokens
-
validateCache
- Throws:
IOException
-
readHostsFile
- Throws:
IOException
-
createOrClearCache
private void createOrClearCache() -
key
-
cacheSize
int cacheSize() -
setClock
-