Skip to content

Commit 3812071

Browse files
committed
Misc cleanup
1 parent aecef59 commit 3812071

3 files changed

Lines changed: 13 additions & 23 deletions

File tree

src/main/java/com/maxmind/maxminddb/Decoder.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ private Result decodeMap(int size, int offset) throws IOException {
309309
}
310310

311311
return new Result(map, offset);
312-
313312
}
314313

315314
private int[] sizeFromCtrlByte(int ctrlByte, int offset) {

src/main/java/com/maxmind/maxminddb/MaxMindDbReader.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public enum FileMode {
4343
/**
4444
* Constructs a Reader for the MaxMind DB format. The file passed to it must
4545
* be a valid MaxMind DB file such as a GeoIP2 database file.
46-
*
46+
*
4747
* @param database
4848
* the MaxMind DB file to use.
4949
* @throws IOException
@@ -56,7 +56,7 @@ public MaxMindDbReader(File database) throws IOException {
5656
/**
5757
* Constructs a Reader for the MaxMind DB format. The file passed to it must
5858
* be a valid MaxMind DB file such as a GeoIP2 database file.
59-
*
59+
*
6060
* @param database
6161
* the MaxMind DB file to use.
6262
* @param fileMode
@@ -66,14 +66,7 @@ public MaxMindDbReader(File database) throws IOException {
6666
*/
6767
public MaxMindDbReader(File database, FileMode fileMode) throws IOException {
6868
this.threadBuffer = new ThreadBuffer(database, fileMode);
69-
int start = this.findMetadataStart();
70-
71-
if (start < 0) {
72-
throw new InvalidDatabaseException(
73-
"Could not find a MaxMind DB metadata marker in this file ("
74-
+ database.getName()
75-
+ "). Is this a valid MaxMind DB file?");
76-
}
69+
int start = this.findMetadataStart(database.getName());
7770

7871
Decoder metadataDecoder = new Decoder(this.threadBuffer, 0);
7972
this.metadata = new Metadata(metadataDecoder.decode(start).getNode());
@@ -87,7 +80,7 @@ public MaxMindDbReader(File database, FileMode fileMode) throws IOException {
8780

8881
/**
8982
* Looks up the <code>address</code> in the MaxMind DB.
90-
*
83+
*
9184
* @param ipAddress
9285
* the IP address to look up.
9386
* @return the record for the IP address.
@@ -99,7 +92,6 @@ public JsonNode get(InetAddress ipAddress) throws IOException {
9992
if (pointer == 0) {
10093
return null;
10194
}
102-
this.threadBuffer.get().position(pointer);
10395
return this.resolveDataPointer(pointer);
10496
}
10597

@@ -162,7 +154,6 @@ private int readNode(int nodeNumber, int index)
162154
throws InvalidDatabaseException {
163155
ByteBuffer buffer = this.threadBuffer.get();
164156
int baseOffset = nodeNumber * this.metadata.nodeByteSize;
165-
buffer.position(baseOffset);
166157

167158
switch (this.metadata.recordSize) {
168159
case 24:
@@ -206,12 +197,12 @@ private JsonNode resolveDataPointer(int pointer) throws IOException {
206197
/*
207198
* Apparently searching a file for a sequence is not a solved problem in
208199
* Java. This searches from the end of the file for metadata start.
209-
*
200+
*
210201
* This is an extremely naive but reasonably readable implementation. There
211202
* are much faster algorithms (e.g., Boyer-Moore) for this if speed is ever
212203
* an issue, but I suspect it won't be.
213204
*/
214-
private int findMetadataStart() {
205+
private int findMetadataStart(String databaseName) throws InvalidDatabaseException {
215206
ByteBuffer buffer = this.threadBuffer.get();
216207
int fileSize = buffer.capacity();
217208

@@ -225,7 +216,10 @@ private int findMetadataStart() {
225216
}
226217
return fileSize - i;
227218
}
228-
return -1;
219+
throw new InvalidDatabaseException(
220+
"Could not find a MaxMind DB metadata marker in this file ("
221+
+ databaseName
222+
+ "). Is this a valid MaxMind DB file?");
229223
}
230224

231225
Metadata getMetadata() {
@@ -234,12 +228,12 @@ Metadata getMetadata() {
234228

235229
/**
236230
* Closes the MaxMind DB and returns resources to the system.
237-
*
231+
*
238232
* @throws IOException
239233
* if an I/O error occurs.
240234
*/
241235
@Override
242236
public void close() throws IOException {
243237
this.threadBuffer.close();
244238
}
245-
}
239+
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class ReaderTest {
1919

2020
@Test
2121
public void test() throws InvalidDatabaseException, IOException {
22-
for (long recordSize : new long[] { 24 }) { // 24, 28, 32 }) {
22+
for (long recordSize : new long[] { 24, 28, 32 }) {
2323
for (int ipVersion : new int[] { 4, 6 }) {
2424
String fileName = "test-data/Test-IPv" + ipVersion + "-"
2525
+ recordSize + ".mmdb";
@@ -31,7 +31,6 @@ public void test() throws InvalidDatabaseException, IOException {
3131
} else {
3232
this.testIpV6(reader, fileName);
3333
}
34-
3534
}
3635
}
3736
}
@@ -53,7 +52,6 @@ private void testMetadata(MaxMindDbReader reader, int ipVersion, long recordSize
5352

5453
assertEquals(description, metadata.description);
5554
assertEquals(recordSize, metadata.recordSize);
56-
5755
}
5856

5957
private void testIpV4(MaxMindDbReader reader, String fileName)
@@ -128,6 +126,5 @@ private void testIpV6(MaxMindDbReader reader, String fileName)
128126
for (String ip : new String[] { "1.1.1.33", "255.254.253.123", "89fa::" }) {
129127
assertNull(reader.get(InetAddress.getByName(ip)));
130128
}
131-
132129
}
133130
}

0 commit comments

Comments
 (0)