public class DatabaseReader extends Object implements DatabaseProvider, 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 one of
the appropriate methods, e.g., city
or tryCity
, for your
database. These methods take the IP address to be looked up. The methods
with the "try" prefix return an Optional
object, which will be
empty if the value is not present in the database. The method without the
prefix will throw an AddressNotFoundException
if the address is
not in the database. If you are looking up many IPs that are not contained
in the database, the "try" method will be slightly faster as they do not
need to construct and throw an exception. These methods otherwise behave
the same.
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 |
---|---|
AnonymousIpResponse |
anonymousIp(InetAddress ipAddress)
Look up an IP address in a GeoIP2 Anonymous IP.
|
AsnResponse |
asn(InetAddress ipAddress)
Look up an IP address in a GeoLite2 ASN database.
|
CityResponse |
city(InetAddress ipAddress) |
void |
close()
Closes the database.
|
ConnectionTypeResponse |
connectionType(InetAddress ipAddress)
Look up an IP address in a GeoIP2 Connection Type database.
|
CountryResponse |
country(InetAddress ipAddress) |
DomainResponse |
domain(InetAddress ipAddress)
Look up an IP address in a GeoIP2 Domain database.
|
EnterpriseResponse |
enterprise(InetAddress ipAddress)
Look up an IP address in a GeoIP2 Enterprise database.
|
Metadata |
getMetadata() |
IspResponse |
isp(InetAddress ipAddress)
Look up an IP address in a GeoIP2 ISP database.
|
Optional<AnonymousIpResponse> |
tryAnonymousIp(InetAddress ipAddress)
Look up an IP address in a GeoIP2 Anonymous IP.
|
Optional<AsnResponse> |
tryAsn(InetAddress ipAddress)
Look up an IP address in a GeoLite2 ASN database.
|
Optional<CityResponse> |
tryCity(InetAddress ipAddress) |
Optional<ConnectionTypeResponse> |
tryConnectionType(InetAddress ipAddress)
Look up an IP address in a GeoIP2 Connection Type database.
|
Optional<CountryResponse> |
tryCountry(InetAddress ipAddress) |
Optional<DomainResponse> |
tryDomain(InetAddress ipAddress)
Look up an IP address in a GeoIP2 Domain database.
|
Optional<EnterpriseResponse> |
tryEnterprise(InetAddress ipAddress)
Look up an IP address in a GeoIP2 Enterprise database.
|
Optional<IspResponse> |
tryIsp(InetAddress ipAddress)
Look up an IP address in a GeoIP2 ISP database.
|
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 CountryResponse country(InetAddress ipAddress) throws IOException, GeoIp2Exception
country
in interface GeoIp2Provider
ipAddress
- IPv4 or IPv6 address to lookup.IOException
- if there is an IO errorGeoIp2Exception
- if there is an error looking up the IPpublic Optional<CountryResponse> tryCountry(InetAddress ipAddress) throws IOException, GeoIp2Exception
tryCountry
in interface DatabaseProvider
ipAddress
- IPv4 or IPv6 address to lookup.IOException
- if there is an IO errorGeoIp2Exception
- if there is an error looking up the IPpublic CityResponse city(InetAddress ipAddress) throws IOException, GeoIp2Exception
city
in interface GeoIp2Provider
ipAddress
- IPv4 or IPv6 address to lookup.IOException
- if there is an IO errorGeoIp2Exception
- if there is an error looking up the IPpublic Optional<CityResponse> tryCity(InetAddress ipAddress) throws IOException, GeoIp2Exception
tryCity
in interface DatabaseProvider
ipAddress
- IPv4 or IPv6 address to lookup.IOException
- if there is an IO errorGeoIp2Exception
- if there is an error looking up the IPpublic AnonymousIpResponse anonymousIp(InetAddress ipAddress) throws IOException, GeoIp2Exception
anonymousIp
in interface DatabaseProvider
ipAddress
- IPv4 or IPv6 address to lookup.GeoIp2Exception
- if there is an error looking up the IPIOException
- if there is an IO errorpublic Optional<AnonymousIpResponse> tryAnonymousIp(InetAddress ipAddress) throws IOException, GeoIp2Exception
DatabaseProvider
tryAnonymousIp
in interface DatabaseProvider
ipAddress
- IPv4 or IPv6 address to lookup.IOException
- if there is an IO errorGeoIp2Exception
- if there is an error looking up the IPpublic AsnResponse asn(InetAddress ipAddress) throws IOException, GeoIp2Exception
asn
in interface DatabaseProvider
ipAddress
- IPv4 or IPv6 address to lookup.GeoIp2Exception
- if there is an error looking up the IPIOException
- if there is an IO errorpublic Optional<AsnResponse> tryAsn(InetAddress ipAddress) throws IOException, GeoIp2Exception
DatabaseProvider
tryAsn
in interface DatabaseProvider
ipAddress
- IPv4 or IPv6 address to lookup.IOException
- if there is an IO errorGeoIp2Exception
- if there is an error looking up the IPpublic ConnectionTypeResponse connectionType(InetAddress ipAddress) throws IOException, GeoIp2Exception
connectionType
in interface DatabaseProvider
ipAddress
- IPv4 or IPv6 address to lookup.GeoIp2Exception
- if there is an error looking up the IPIOException
- if there is an IO errorpublic Optional<ConnectionTypeResponse> tryConnectionType(InetAddress ipAddress) throws IOException, GeoIp2Exception
DatabaseProvider
tryConnectionType
in interface DatabaseProvider
ipAddress
- IPv4 or IPv6 address to lookup.IOException
- if there is an IO errorGeoIp2Exception
- if there is an error looking up the IPpublic DomainResponse domain(InetAddress ipAddress) throws IOException, GeoIp2Exception
domain
in interface DatabaseProvider
ipAddress
- IPv4 or IPv6 address to lookup.GeoIp2Exception
- if there is an error looking up the IPIOException
- if there is an IO errorpublic Optional<DomainResponse> tryDomain(InetAddress ipAddress) throws IOException, GeoIp2Exception
DatabaseProvider
tryDomain
in interface DatabaseProvider
ipAddress
- IPv4 or IPv6 address to lookup.IOException
- if there is an IO errorGeoIp2Exception
- if there is an error looking up the IPpublic EnterpriseResponse enterprise(InetAddress ipAddress) throws IOException, GeoIp2Exception
enterprise
in interface DatabaseProvider
ipAddress
- IPv4 or IPv6 address to lookup.GeoIp2Exception
- if there is an error looking up the IPIOException
- if there is an IO errorpublic Optional<EnterpriseResponse> tryEnterprise(InetAddress ipAddress) throws IOException, GeoIp2Exception
DatabaseProvider
tryEnterprise
in interface DatabaseProvider
ipAddress
- IPv4 or IPv6 address to lookup.IOException
- if there is an IO errorGeoIp2Exception
- if there is an error looking up the IPpublic IspResponse isp(InetAddress ipAddress) throws IOException, GeoIp2Exception
isp
in interface DatabaseProvider
ipAddress
- IPv4 or IPv6 address to lookup.GeoIp2Exception
- if there is an error looking up the IPIOException
- if there is an IO errorpublic Optional<IspResponse> tryIsp(InetAddress ipAddress) throws IOException, GeoIp2Exception
DatabaseProvider
tryIsp
in interface DatabaseProvider
ipAddress
- IPv4 or IPv6 address to lookup or empty if the IP address is not in the DB.IOException
- if there is an IO errorGeoIp2Exception
- if there is an error looking up the IPpublic Metadata getMetadata()
Copyright © 2020 MaxMind, Inc.. All rights reserved.