public final class HostsFileParser
extends java.lang.Object
/etc/hosts
. The
cache is cleared when the file is modified.Modifier and Type | Class and Description |
---|---|
private static class |
HostsFileParser.LineData |
Modifier and Type | Field and Description |
---|---|
private boolean |
clearCacheOnChange |
private java.util.Map<java.lang.String,java.net.InetAddress> |
hostsCache |
private boolean |
isEntireFileParsed |
private java.time.Instant |
lastFileReadTime |
private static int |
MAX_FULL_CACHE_FILE_SIZE_BYTES |
private java.nio.file.Path |
path |
Constructor and Description |
---|
HostsFileParser()
Creates a new instance based on the current OS's default.
|
HostsFileParser(java.nio.file.Path path)
Creates an instance with a custom hosts database path.
|
HostsFileParser(java.nio.file.Path path,
boolean clearCacheOnChange)
Creates an instance with a custom hosts database path.
|
Modifier and Type | Method and Description |
---|---|
(package private) int |
cacheSize() |
java.util.Optional<java.net.InetAddress> |
getAddressForHost(Name name,
int type)
Performs on-demand parsing and caching of the local hosts database.
|
private java.lang.String[] |
getLineTokens(java.lang.String line) |
private java.lang.String |
key(Name name,
int type) |
private void |
parseEntireHostsFile() |
private HostsFileParser.LineData |
parseLine(int lineNumber,
java.lang.String line) |
private Name |
safeName(java.lang.String name,
int lineNumber) |
private void |
searchHostsFileForEntry(Name name,
int type) |
private void |
validateCache() |
private static final int MAX_FULL_CACHE_FILE_SIZE_BYTES
private final java.util.Map<java.lang.String,java.net.InetAddress> hostsCache
private final java.nio.file.Path path
private final boolean clearCacheOnChange
private java.time.Instant lastFileReadTime
private boolean isEntireFileParsed
public HostsFileParser()
/etc/hosts
, while on Windows %SystemRoot%\System32\drivers\etc\hosts
is used. The cache is cleared when the file has
changed.public HostsFileParser(java.nio.file.Path path)
path
- The path to the hosts database.public HostsFileParser(java.nio.file.Path path, boolean clearCacheOnChange)
path
- The path to the hosts database.clearCacheOnChange
- set to true to clear the cache when the hosts file changes.public java.util.Optional<java.net.InetAddress> getAddressForHost(Name name, int type) throws java.io.IOException
private void parseEntireHostsFile() throws java.io.IOException
java.io.IOException
private void searchHostsFileForEntry(Name name, int type) throws java.io.IOException
java.io.IOException
private HostsFileParser.LineData parseLine(int lineNumber, java.lang.String line)
private Name safeName(java.lang.String name, int lineNumber)
private java.lang.String[] getLineTokens(java.lang.String line)
private void validateCache() throws java.io.IOException
java.io.IOException
private java.lang.String key(Name name, int type)
int cacheSize()