Skip to content

Commit 7866b3c

Browse files
author
Matthew L Daniel
committed
Teach Reader to load from a URL
Updated the test to exercise both the File and the URL flavors.
1 parent c041609 commit 7866b3c

2 files changed

Lines changed: 98 additions & 8 deletions

File tree

src/main/java/com/maxmind/db/Reader.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.File;
55
import java.io.IOException;
66
import java.net.InetAddress;
7+
import java.net.URL;
78
import java.nio.ByteBuffer;
89

910
import com.fasterxml.jackson.databind.JsonNode;
@@ -52,6 +53,17 @@ public Reader(File database) throws IOException {
5253
this(database, FileMode.MEMORY_MAPPED);
5354
}
5455

56+
/**
57+
* Constructs a Reader as if in mode {@link FileMode#MEMORY}, without using
58+
* a <code>File</code> instance.
59+
* @param source the URL that points to the MaxMind DB file.
60+
* @throws IOException
61+
* if there is an error opening or reading from the file.
62+
*/
63+
public Reader(URL source) throws IOException {
64+
this(ThreadBuffer.newInstance(source), source.getFile());
65+
}
66+
5567
/**
5668
* Constructs a Reader for the MaxMind DB format. The file passed to it must
5769
* be a valid MaxMind DB file such as a GeoIP2 database file.
@@ -64,8 +76,12 @@ public Reader(File database) throws IOException {
6476
* if there is an error opening or reading from the file.
6577
*/
6678
public Reader(File database, FileMode fileMode) throws IOException {
67-
this.threadBuffer = new ThreadBuffer(database, fileMode);
68-
int start = this.findMetadataStart(database.getName());
79+
this(new ThreadBuffer(database, fileMode), database.getName());
80+
}
81+
82+
private Reader(ThreadBuffer buffer, String name) throws IOException {
83+
this.threadBuffer = buffer;
84+
int start = this.findMetadataStart(name);
6985

7086
Decoder metadataDecoder = new Decoder(this.threadBuffer, start);
7187
this.metadata = new Metadata(metadataDecoder.decode(start).getNode());

src/test/java/com/maxmind/db/ReaderTest.java

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.math.BigInteger;
1212
import java.net.InetAddress;
1313
import java.net.URI;
14+
import java.net.URL;
1415
import java.net.URISyntaxException;
1516
import java.net.UnknownHostException;
1617
import java.util.HashMap;
@@ -52,12 +53,23 @@ public void test() throws InvalidDatabaseException, IOException,
5253
}
5354

5455
@Test
55-
public void testNoIpV4SearchTree() throws IOException, URISyntaxException {
56+
public void testNoIpV4SearchTreeFile() throws IOException, URISyntaxException {
5657
URI file = ReaderTest.class.getResource(
5758
"/maxmind-db/test-data/MaxMind-DB-no-ipv4-search-tree.mmdb")
5859
.toURI();
5960

6061
Reader reader = new Reader(new File(file));
62+
testNoIpV4SearchTree(reader);
63+
}
64+
@Test
65+
public void testNoIpV4SearchTreeURL() throws IOException, URISyntaxException {
66+
URL resource = ReaderTest.class.getResource(
67+
"/maxmind-db/test-data/MaxMind-DB-no-ipv4-search-tree.mmdb");
68+
Reader reader = new Reader(resource);
69+
testNoIpV4SearchTree(reader);
70+
}
71+
private void testNoIpV4SearchTree(Reader reader) throws IOException, URISyntaxException {
72+
6173

6274
assertEquals("::/64", reader.get(InetAddress.getByName("1.1.1.1"))
6375
.textValue());
@@ -66,11 +78,22 @@ public void testNoIpV4SearchTree() throws IOException, URISyntaxException {
6678
}
6779

6880
@Test
69-
public void testDecodingTypes() throws URISyntaxException, IOException {
81+
public void testDecodingTypesFile() throws URISyntaxException, IOException {
7082
URI file = ReaderTest.class.getResource(
7183
"/maxmind-db/test-data/MaxMind-DB-test-decoder.mmdb").toURI();
7284

7385
Reader reader = new Reader(new File(file));
86+
testDecodingTypes(reader);
87+
}
88+
@Test
89+
public void testDecodingTypesURL() throws URISyntaxException, IOException {
90+
URL resource = ReaderTest.class.getResource(
91+
"/maxmind-db/test-data/MaxMind-DB-test-decoder.mmdb");
92+
93+
Reader reader = new Reader(resource);
94+
testDecodingTypes(reader);
95+
}
96+
private void testDecodingTypes(Reader reader) throws URISyntaxException, IOException {
7497
JsonNode record = reader.get(InetAddress.getByName("::1.1.1.0"));
7598

7699
assertEquals(true, record.get("boolean").booleanValue());
@@ -114,11 +137,22 @@ public void testDecodingTypes() throws URISyntaxException, IOException {
114137
}
115138

116139
@Test
117-
public void testZeros() throws URISyntaxException, IOException {
140+
public void testZerosFile() throws URISyntaxException, IOException {
118141
URI file = ReaderTest.class.getResource(
119142
"/maxmind-db/test-data/MaxMind-DB-test-decoder.mmdb").toURI();
120143

121144
Reader reader = new Reader(new File(file));
145+
testZeros(reader);
146+
}
147+
@Test
148+
public void testZerosURL() throws URISyntaxException, IOException {
149+
URL resource = ReaderTest.class.getResource(
150+
"/maxmind-db/test-data/MaxMind-DB-test-decoder.mmdb");
151+
152+
Reader reader = new Reader(resource);
153+
testZeros(reader);
154+
}
155+
private void testZeros(Reader reader) throws URISyntaxException, IOException {
122156
JsonNode record = reader.get(InetAddress.getByName("::"));
123157

124158
assertEquals(false, record.get("boolean").booleanValue());
@@ -146,13 +180,25 @@ public void testZeros() throws URISyntaxException, IOException {
146180
public ExpectedException thrown = ExpectedException.none();
147181

148182
@Test
149-
public void testBrokenDatabase() throws URISyntaxException, IOException {
183+
public void testBrokenDatabaseFile() throws URISyntaxException, IOException {
150184
URI file = ReaderTest.class
151185
.getResource(
152186
"/maxmind-db/test-data/GeoIP2-City-Test-Broken-Double-Format.mmdb")
153187
.toURI();
154188

155189
Reader reader = new Reader(new File(file));
190+
testBrokenDatabase(reader);
191+
}
192+
@Test
193+
public void testBrokenDatabaseURL() throws URISyntaxException, IOException {
194+
URL resource = ReaderTest.class
195+
.getResource(
196+
"/maxmind-db/test-data/GeoIP2-City-Test-Broken-Double-Format.mmdb");
197+
198+
Reader reader = new Reader(resource);
199+
testBrokenDatabase(reader);
200+
}
201+
private void testBrokenDatabase(Reader reader) throws URISyntaxException, IOException {
156202

157203
this.thrown.expect(InvalidDatabaseException.class);
158204
this.thrown
@@ -162,14 +208,28 @@ public void testBrokenDatabase() throws URISyntaxException, IOException {
162208
}
163209

164210
@Test
165-
public void testBrokenSearchTreePointer() throws URISyntaxException,
211+
public void testBrokenSearchTreePointerFile() throws URISyntaxException,
166212
IOException {
167213
URI file = ReaderTest.class
168214
.getResource(
169215
"/maxmind-db/test-data/MaxMind-DB-test-broken-pointers-24.mmdb")
170216
.toURI();
171217

172218
Reader reader = new Reader(new File(file));
219+
testBrokenSearchTreePointer(reader);
220+
}
221+
@Test
222+
public void testBrokenSearchTreePointerURL() throws URISyntaxException,
223+
IOException {
224+
URL resource = ReaderTest.class
225+
.getResource(
226+
"/maxmind-db/test-data/MaxMind-DB-test-broken-pointers-24.mmdb");
227+
228+
Reader reader = new Reader(resource);
229+
testBrokenSearchTreePointer(reader);
230+
}
231+
private void testBrokenSearchTreePointer(Reader reader) throws URISyntaxException,
232+
IOException {
173233

174234
this.thrown.expect(InvalidDatabaseException.class);
175235
this.thrown
@@ -179,14 +239,28 @@ public void testBrokenSearchTreePointer() throws URISyntaxException,
179239
}
180240

181241
@Test
182-
public void testBrokenDataPointer() throws UnknownHostException,
242+
public void testBrokenDataPointerFile() throws UnknownHostException,
183243
IOException, URISyntaxException {
184244
URI file = ReaderTest.class
185245
.getResource(
186246
"/maxmind-db/test-data/MaxMind-DB-test-broken-pointers-24.mmdb")
187247
.toURI();
188248

189249
Reader reader = new Reader(new File(file));
250+
testBrokenDataPointer(reader);
251+
}
252+
@Test
253+
public void testBrokenDataPointerURL() throws UnknownHostException,
254+
IOException, URISyntaxException {
255+
URL resource = ReaderTest.class
256+
.getResource(
257+
"/maxmind-db/test-data/MaxMind-DB-test-broken-pointers-24.mmdb");
258+
259+
Reader reader = new Reader(resource);
260+
testBrokenDataPointer(reader);
261+
}
262+
private void testBrokenDataPointer(Reader reader) throws UnknownHostException,
263+
IOException, URISyntaxException {
190264

191265
this.thrown.expect(InvalidDatabaseException.class);
192266
this.thrown

0 commit comments

Comments
 (0)