@@ -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+ }
0 commit comments