Class DatabaseReader
- java.lang.Object
-
- com.maxmind.geoip2.DatabaseReader
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
public class DatabaseReader extends Object implements Closeable
Modified and simplified for I2PThe class
DatabaseReader
provides a reader for the GeoIP2 database format.Usage
To use the database API, you must create a new
DatabaseReader
using theDatabaseReader.Builder
. You must provide theBuilder
constructor either anInputStream
orFile
for your GeoIP2 database. You may also specify thefileMode
and thelocales
fallback order using the methods on theBuilder
object. After you have created theDatabaseReader
, 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.Caching
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.- Since:
- 0.9.38
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DatabaseReader.Builder
Constructs a Builder for theDatabaseReader
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes the database.String
country(String ipAddress)
void
countryToIP(String country, Writer out)
I2P - Write all IPv4 address ranges for the given country to out.Metadata
getMetadata()
static void
main(String[] args)
-
-
-
Method Detail
-
close
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'sMappedByteBuffer
. It will however set the reference to the buffer tonull
, allowing the garbage collector to collect it.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
- if an I/O error occurs.
-
country
public String country(String ipAddress) throws IOException
- Throws:
IOException
-
countryToIP
public void countryToIP(String country, Writer out) throws IOException
I2P - Write all IPv4 address ranges for the given country to out.- Parameters:
country
- two-letter case-insensitiveout
- caller must close- Throws:
IOException
- Since:
- 0.9.48
-
getMetadata
public Metadata getMetadata()
- Returns:
- the metadata for the open MaxMind DB file.
-
-