public class DatabaseReader extends Object implements Closeable
The class DatabaseReader
provides a reader for the GeoIP2 database
format.
To use the database API, you must create a new DatabaseReader
using
the DatabaseReader.Builder
. You must provide the Builder
constructor either an InputStream
or File
for your GeoIP2
database. You may also specify the fileMode
and the locales
fallback order using the methods on the Builder
object. After you
have created the DatabaseReader
, you may then call the appropriate
method (e.g., city
) for your database, passing it the IP address
you want to look up.
If the lookup succeeds, the method call will return a response class for the GeoIP2 lookup. The class in turn contains multiple record classes, each of which represents part of the data returned by the database.
We recommend reusing the DatabaseReader
object rather than creating
a new one for each lookup. The creation of this object is relatively
expensive as it must read in metadata for the file. It is safe to share the
object across threads.
The database API supports pluggable caching (by default, no caching is
performed). A simple implementation is provided by
com.maxmind.db.CHMCache
. Using this cache, lookup performance is
significantly improved at the cost of a small (~2MB) memory overhead.
Modifier and Type | Class and Description |
---|---|
static class |
DatabaseReader.Builder
Constructs a Builder for the
DatabaseReader . |
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the database.
|
String |
country(String ipAddress) |
Metadata |
getMetadata() |
static void |
main(String[] args) |
public void close() throws IOException
Closes the database.
If you are using FileMode.MEMORY_MAPPED
, this will
not unmap the underlying file due to a limitation in Java's
MappedByteBuffer
. It will however set the reference to
the buffer to null
, allowing the garbage collector to
collect it.
close
in interface Closeable
close
in interface AutoCloseable
IOException
- if an I/O error occurs.public String country(String ipAddress) throws IOException
IOException
public Metadata getMetadata()