java.lang.Object
com.maxmind.db.Reader
- All Implemented Interfaces:
Closeable
,AutoCloseable
Instances of this class provide a reader for the MaxMind DB format. IP
addresses can be looked up using the
get
method.-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
The file mode to use when opening a MaxMind DB. -
Constructor Summary
ConstructorDescriptionConstructs a Reader for the MaxMind DB format, with no caching.Constructs a Reader for the MaxMind DB format, with the specified backing cache.Reader
(File database, Reader.FileMode fileMode) Constructs a Reader for the MaxMind DB format, with no caching.Reader
(File database, Reader.FileMode fileMode, NodeCache cache) Constructs a Reader for the MaxMind DB format, with the specified backing cache.Reader
(InputStream source) Constructs a Reader with no caching, as if in modeReader.FileMode.MEMORY
, without using aFile
instance.Reader
(InputStream source, NodeCache cache) Constructs a Reader with the specified backing cache, as if in modeReader.FileMode.MEMORY
, without using aFile
instance. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the database.<T> T
get
(InetAddress ipAddress, Class<T> cls) Looks upipAddress
in the MaxMind DB.<T> DatabaseRecord<T>
getRecord
(InetAddress ipAddress, Class<T> cls) Looks upipAddress
in the MaxMind DB.<T> Networks<T>
Creates a Networks iterator.<T> Networks<T>
Creates a Networks iterator and skips aliased networks.<T> Networks<T>
networksWithin
(Network network, boolean includeAliasedNetworks, Class<T> typeParameterClass) Returns an iterator within the specified network.
-
Constructor Details
-
Reader
Constructs a Reader for the MaxMind DB format, with no caching. The file passed to it must be a valid MaxMind DB file such as a GeoIP2 database file.- Parameters:
database
- the MaxMind DB file to use.- Throws:
IOException
- if there is an error opening or reading from the file.
-
Reader
Constructs a Reader for the MaxMind DB format, with the specified backing cache. The file passed to it must be a valid MaxMind DB file such as a GeoIP2 database file.- Parameters:
database
- the MaxMind DB file to use.cache
- backing cache instance- Throws:
IOException
- if there is an error opening or reading from the file.
-
Reader
Constructs a Reader with no caching, as if in modeReader.FileMode.MEMORY
, without using aFile
instance.- Parameters:
source
- the InputStream that contains the MaxMind DB file.- Throws:
IOException
- if there is an error reading from the Stream.
-
Reader
Constructs a Reader with the specified backing cache, as if in modeReader.FileMode.MEMORY
, without using aFile
instance.- Parameters:
source
- the InputStream that contains the MaxMind DB file.cache
- backing cache instance- Throws:
IOException
- if there is an error reading from the Stream.
-
Reader
Constructs a Reader for the MaxMind DB format, with no caching. The file passed to it must be a valid MaxMind DB file such as a GeoIP2 database file.- Parameters:
database
- the MaxMind DB file to use.fileMode
- the mode to open the file with.- Throws:
IOException
- if there is an error opening or reading from the file.
-
Reader
Constructs a Reader for the MaxMind DB format, with the specified backing cache. The file passed to it must be a valid MaxMind DB file such as a GeoIP2 database file.- Parameters:
database
- the MaxMind DB file to use.fileMode
- the mode to open the file with.cache
- backing cache instance- Throws:
IOException
- if there is an error opening or reading from the file.
-
-
Method Details
-
get
Looks upipAddress
in the MaxMind DB.- Type Parameters:
T
- the type to populate.- Parameters:
ipAddress
- the IP address to look up.cls
- the class of object to populate.- Returns:
- the object.
- Throws:
IOException
- if a file I/O error occurs.
-
getRecord
Looks upipAddress
in the MaxMind DB.- Type Parameters:
T
- the type to populate.- Parameters:
ipAddress
- the IP address to look up.cls
- the class of object to populate.- Returns:
- the record for the IP address. If there is no data for the
address, the non-null
DatabaseRecord
will still be returned. - Throws:
IOException
- if a file I/O error occurs.
-
networks
public <T> Networks<T> networks(Class<T> typeParameterClass) throws InvalidNetworkException, ClosedDatabaseException, InvalidDatabaseException Creates a Networks iterator and skips aliased networks. Please note that a MaxMind DB may map IPv4 networks into several locations in an IPv6 database. networks() iterates over the canonical locations and not the aliases. To include the aliases, you can set includeAliasedNetworks to true.- Type Parameters:
T
- Represents the data type(e.g., Map, HastMap, etc.).- Parameters:
typeParameterClass
- The type of data returned by the iterator.- Returns:
- Networks The Networks iterator.
- Throws:
InvalidNetworkException
- Exception for using an IPv6 network in ipv4-only database.ClosedDatabaseException
- Exception for a closed databased.InvalidDatabaseException
- Exception for an invalid database.
-
networks
public <T> Networks<T> networks(boolean includeAliasedNetworks, Class<T> typeParameterClass) throws InvalidNetworkException, ClosedDatabaseException, InvalidDatabaseException Creates a Networks iterator. Please note that a MaxMind DB may map IPv4 networks into several locations in an IPv6 database. This iterator will iterate over all of these locations separately. To set the iteration over the IPv4 networks once, use the includeAliasedNetworks option.- Type Parameters:
T
- Represents the data type(e.g., Map, HastMap, etc.).- Parameters:
includeAliasedNetworks
- Enable including aliased networks.- Returns:
- Networks The Networks iterator.
- Throws:
InvalidNetworkException
- Exception for using an IPv6 network in ipv4-only database.ClosedDatabaseException
- Exception for a closed databased.InvalidDatabaseException
- Exception for an invalid database.
-
networksWithin
public <T> Networks<T> networksWithin(Network network, boolean includeAliasedNetworks, Class<T> typeParameterClass) throws InvalidNetworkException, ClosedDatabaseException, InvalidDatabaseException Returns an iterator within the specified network. Please note that a MaxMind DB may map IPv4 networks into several locations in an IPv6 database. This iterator will iterate over all of these locations separately. To only iterate over the IPv4 networks once, use the includeAliasedNetworks option.- Type Parameters:
T
- Represents the data type(e.g., Map, HastMap, etc.).- Parameters:
network
- Specifies the network to be iterated.includeAliasedNetworks
- Boolean for including aliased networks.typeParameterClass
- The type of data returned by the iterator.- Returns:
- Networks
- Throws:
InvalidNetworkException
- Exception for using an IPv6 network in ipv4-only database.ClosedDatabaseException
- Exception for a closed databased.InvalidDatabaseException
- Exception for an invalid database.
-
getMetadata
- Returns:
- the metadata for the MaxMind DB file.
-
close
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.
-