java.lang.Object
com.maxmind.db.Reader
- All Implemented Interfaces:
Closeable, AutoCloseable
Nested Class Summary
Modifier and TypeClassDescriptionstatic enumThe file mode to use when opening a MaxMind DB.private classWalks the search tree writing the IPv4 ranges of one country.Field Summary
FieldsModifier and TypeFieldDescriptionprivate final AtomicReference<BufferHolder> Holder for the database data, for reloads.private final NodeCacheCache of the tree nodes read.private static final intSize of the separator before the data section, in bytes.private final intIndex of the IPv4 subtree's start node.private final MetadataMetadata read from the database.private static final byte[]Marker preceding the database metadata.Constructor Summary
ModifierConstructorDescriptionprivateReader(BufferHolder bufferHolder, String name, NodeCache cache) Opens a reader on an already loaded database.Constructs 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 aFileinstance.Reader(InputStream source, NodeCache cache) Constructs a Reader with the specified backing cache, as if in modeReader.FileMode.MEMORY, without using aFileinstance.Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the database.voidcountryToIP(String country, Writer out) I2P - Write all IPv4 address ranges for the given country to out.private intfindAddressInTree(ByteBuffer buffer, InetAddress address) Finds the node or data pointer for an address.private intfindIpV4StartNode(ByteBuffer buffer) Finds the start node of the IPv4 subtree.private intfindMetadataStart(ByteBuffer buffer, String databaseName) Finds the start of the database metadata in the buffer.get(InetAddress ipAddress) Looks up theaddressin the MaxMind DB.private BufferHolderReturns the holder for the database data.Returns the metadata for the MaxMind DB file.private intreadNode(ByteBuffer buffer, int nodeNumber, int index) Reads one node from the tree.private ObjectresolveDataPointer(ByteBuffer buffer, int pointer) Resolves a data pointer to its value.private intstartNode(int bitLength) Returns the first node to search for an address family.
Field Details
DATA_SECTION_SEPARATOR_SIZE
private static final int DATA_SECTION_SEPARATOR_SIZESize of the separator before the data section, in bytes.- See Also:
METADATA_START_MARKER
private static final byte[] METADATA_START_MARKERMarker preceding the database metadata.ipV4Start
private final int ipV4StartIndex of the IPv4 subtree's start node.metadata
Metadata read from the database.bufferHolderReference
Holder for the database data, for reloads.cache
Cache of the tree nodes read.
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 aFileinstance.- 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 aFileinstance.- 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.
Reader
Opens a reader on an already loaded database.- Parameters:
bufferHolder- the holder for the database dataname- the database name for the error messagescache- the node cache to use- Throws:
IOException- if the metadata cannot be read
Method Details
get
Looks up theaddressin the MaxMind DB.- Parameters:
ipAddress- the IP address to look up.- Returns:
- the record for the IP address.
- Throws:
IOException- if a file I/O error occurs.
countryToIP
I2P - Write all IPv4 address ranges for the given country to out.- Parameters:
country- the ISO 3166-1 alpha-2 code, upper caseout- caller must close- Throws:
IOException- if the database cannot be read or the output cannot be written
getBufferHolder
Returns the holder for the database data.- Returns:
- the current holder
- Throws:
ClosedDatabaseException- if the database is closed
findAddressInTree
private int findAddressInTree(ByteBuffer buffer, InetAddress address) throws InvalidDatabaseException Finds the node or data pointer for an address.- Parameters:
buffer- the buffer holding the database dataaddress- the address to locate- Returns:
- the record number, or -1 if not found
- Throws:
InvalidDatabaseException- if the tree is corrupt
startNode
private int startNode(int bitLength) Returns the first node to search for an address family.- Parameters:
bitLength- the address family size, in bits- Returns:
- the start node number
findIpV4StartNode
Finds the start node of the IPv4 subtree.- Parameters:
buffer- the buffer holding the database data- Returns:
- the start node number, 0 if the tree is smaller
- Throws:
InvalidDatabaseException- if the tree is corrupt
readNode
Reads one node from the tree.- Parameters:
buffer- the buffer holding the database datanodeNumber- the node to readindex- the index of the node's data- Returns:
- the node value
- Throws:
InvalidDatabaseException- if the tree is corrupt
resolveDataPointer
Resolves a data pointer to its value.- Parameters:
buffer- the buffer holding the database datapointer- the pointer to resolve- Returns:
- the decoded value
- Throws:
IOException- if the data cannot be read
findMetadataStart
private int findMetadataStart(ByteBuffer buffer, String databaseName) throws InvalidDatabaseException Finds the start of the database metadata in the buffer.- Parameters:
buffer- the buffer holding the database datadatabaseName- the database name for the error message- Returns:
- the metadata offset
- Throws:
InvalidDatabaseException- if the marker is missing
getMetadata
Returns the metadata for the MaxMind DB file.- 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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if an I/O error occurs.