Skip to content

Commit 036a16f

Browse files
Support custom hosts file size (#349)
1 parent a6371af commit 036a16f

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

README.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ Do NOT use it.
103103
|true
104104
|false
105105

106+
.2+|dnsjava.hostsfile.max_size_bytes
107+
3+|Set the size of the hosts file to be loaded at a time, in bytes.
108+
|Integer
109+
|16384
110+
|1000000
111+
106112
.2+|dnsjava.nio.selector_timeout
107113
3+|Set selector timeout in milliseconds. Default/Max 1000, Min 1.
108114
|Integer

src/main/java/org/xbill/DNS/hosts/HostsFileParser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
*/
3030
@Slf4j
3131
public final class HostsFileParser {
32-
private static final int MAX_FULL_CACHE_FILE_SIZE_BYTES = 16384;
32+
private final int MAX_FULL_CACHE_FILE_SIZE_BYTES = Integer.parseInt(
33+
System.getProperty("dnsjava.hostsfile.max_size_bytes", "16384"));
3334

3435
private final Map<String, InetAddress> hostsCache = new HashMap<>();
3536
private final Path path;

src/test/java/org/xbill/DNS/hosts/HostsFileParserTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,19 @@ void testBigFileIsNotCompletelyCachedA() throws IOException {
171171
assertEquals(1, hostsFileParser.cacheSize());
172172
}
173173

174+
@Test
175+
void testBigFileCompletelyCachedA() throws IOException {
176+
try {
177+
System.setProperty("dnsjava.hostsfile.max_size_bytes", 1024 * 1024 * 1024 + "");
178+
HostsFileParser hostsFileParser = generateLargeHostsFile("testBigFileCompletelyCachedA");
179+
hostsFileParser.getAddressForHost(Name.fromConstantString("localhost-10."), Type.A)
180+
.orElseThrow(() -> new IllegalStateException("Host entry not found"));
181+
assertEquals(1280, hostsFileParser.cacheSize());
182+
} finally {
183+
System.clearProperty("dnsjava.hostsfile.max_size_bytes");
184+
}
185+
}
186+
174187
@Test
175188
void testBigFileIsNotCompletelyCachedAAAA() throws IOException {
176189
HostsFileParser hostsFileParser =

0 commit comments

Comments
 (0)