Package com.maxmind.db
Class Reader
- java.lang.Object
-
- com.maxmind.db.Reader
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public final class Reader extends java.lang.Object implements java.io.Closeable
Instances of this class provide a reader for the MaxMind DB format. IP addresses can be looked up using theget
method.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Reader.FileMode
The file mode to use when opening a MaxMind DB.
-
Constructor Summary
Constructors Constructor Description Reader(java.io.File database)
Constructs a Reader for the MaxMind DB format, with no caching.Reader(java.io.File database, NodeCache cache)
Constructs a Reader for the MaxMind DB format, with the specified backing cache.Reader(java.io.File database, Reader.FileMode fileMode)
Constructs a Reader for the MaxMind DB format, with no caching.Reader(java.io.File database, Reader.FileMode fileMode, NodeCache cache)
Constructs a Reader for the MaxMind DB format, with the specified backing cache.Reader(java.io.InputStream source)
Constructs a Reader with no caching, as if in modeReader.FileMode.MEMORY
, without using aFile
instance.Reader(java.io.InputStream source, NodeCache cache)
Constructs a Reader with the specified backing cache, as if in modeReader.FileMode.MEMORY
, without using aFile
instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes the database.<T> T
get(java.net.InetAddress ipAddress, java.lang.Class<T> cls)
Looks upipAddress
in the MaxMind DB.Metadata
getMetadata()
<T> DatabaseRecord<T>
getRecord(java.net.InetAddress ipAddress, java.lang.Class<T> cls)
Looks upipAddress
in the MaxMind DB.
-
-
-
Constructor Detail
-
Reader
public Reader(java.io.File database) throws java.io.IOException
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:
java.io.IOException
- if there is an error opening or reading from the file.
-
Reader
public Reader(java.io.File database, NodeCache cache) throws java.io.IOException
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:
java.io.IOException
- if there is an error opening or reading from the file.
-
Reader
public Reader(java.io.InputStream source) throws java.io.IOException
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:
java.io.IOException
- if there is an error reading from the Stream.
-
Reader
public Reader(java.io.InputStream source, NodeCache cache) throws java.io.IOException
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:
java.io.IOException
- if there is an error reading from the Stream.
-
Reader
public Reader(java.io.File database, Reader.FileMode fileMode) throws java.io.IOException
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:
java.io.IOException
- if there is an error opening or reading from the file.
-
Reader
public Reader(java.io.File database, Reader.FileMode fileMode, NodeCache cache) throws java.io.IOException
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:
java.io.IOException
- if there is an error opening or reading from the file.
-
-
Method Detail
-
get
public <T> T get(java.net.InetAddress ipAddress, java.lang.Class<T> cls) throws java.io.IOException
Looks upipAddress
in the MaxMind DB.- Parameters:
ipAddress
- the IP address to look up.cls
- the class of object to populate.- Returns:
- the object.
- Throws:
java.io.IOException
- if a file I/O error occurs.
-
getRecord
public <T> DatabaseRecord<T> getRecord(java.net.InetAddress ipAddress, java.lang.Class<T> cls) throws java.io.IOException
Looks upipAddress
in the MaxMind DB.- Parameters:
ipAddress
- the IP address to look up.- Returns:
- the record for the IP address. If there is no data for the
address, the non-null
DatabaseRecord
will still be returned. - Throws:
java.io.IOException
- if a file I/O error occurs.
-
getMetadata
public Metadata getMetadata()
- Returns:
- the metadata for the MaxMind DB file.
-
close
public void close() throws java.io.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 interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
- if an I/O error occurs.
-
-