com.maxmind.db

## Class Reader

- java.lang.Object
- - com.maxmind.db.Reader

- All Implemented Interfaces:
    Closeable, AutoCloseable

      public final class Reader
      extends Object
      implements Closeable

  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

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `Reader.FileMode` The file mode to use when opening a MaxMind DB.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `Reader(File database)` Constructs a Reader for the MaxMind DB format, with no caching.
    `Reader(File database, NodeCache cache)` 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 mode `Reader.FileMode.MEMORY`, without using a `File` instance.
    `Reader(InputStream source, NodeCache cache)` Constructs a Reader with the specified backing cache, as if in mode `Reader.FileMode.MEMORY`, without using a `File` instance.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close()` /**
    `void` | `countryToIP(String country, Writer out)` I2P - Write all IPv4 address ranges for the given country to out.
    `Object` | `get(InetAddress ipAddress)` Looks up the `address` in the MaxMind DB.
    `Metadata` | `getMetadata()`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### Reader

          public Reader(File database)
                 throws 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:
        `IOException` - if there is an error opening or reading from the file.

    - #### Reader

          public Reader(File database,
                        NodeCache cache)
                 throws 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:
        `IOException` - if there is an error opening or reading from the file.

    - #### Reader

          public Reader(File database,
                        Reader.FileMode fileMode)
                 throws 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:
        `IOException` - if there is an error opening or reading from the file.

    - #### Reader

          public Reader(File database,
                        Reader.FileMode fileMode,
                        NodeCache cache)
                 throws 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:
        `IOException` - if there is an error opening or reading from the file.

    - #### Reader

          public Reader(InputStream source)
                 throws IOException

      Constructs a Reader with no caching, as if in mode `Reader.FileMode.MEMORY`, without using a `File` instance.
      Parameters:
        `source` - the InputStream that contains the MaxMind DB file.
      Throws:
        `IOException` - if there is an error reading from the Stream.

    - #### Reader

          public Reader(InputStream source,
                        NodeCache cache)
                 throws IOException

      Constructs a Reader with the specified backing cache, as if in mode `Reader.FileMode.MEMORY`, without using a `File` 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.

  - ### 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's `MappedByteBuffer`. It will however set the reference to the buffer to `null`, allowing the garbage collector to collect it.

      Specified by:
        `close` in interface `Closeable`
      Specified by:
        `close` in interface `AutoCloseable`
      Throws:
        `IOException` - if an I/O error occurs.

    - #### 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 uppper-case
        `out` - caller must close
      Throws:
        `IOException`

    - #### get

          public Object get(InetAddress ipAddress)
                     throws IOException

      Looks up the `address` in 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.

    - #### getMetadata

          public Metadata getMetadata()

      Returns:
        the metadata for the MaxMind DB file.