Java Code Examples for com.maxmind.db.Reader.FileMode#MEMORY

The following examples show how to use com.maxmind.db.Reader.FileMode#MEMORY . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BufferHolder.java    From MaxMind-DB-Reader-java with Apache License 2.0 6 votes vote down vote up
BufferHolder(File database, FileMode mode) throws IOException {
    try (
            final RandomAccessFile file = new RandomAccessFile(database, "r");
            final FileChannel channel = file.getChannel()
    ) {
        if (mode == FileMode.MEMORY) {
            final ByteBuffer buf = ByteBuffer.wrap(new byte[(int) channel.size()]);
            if (channel.read(buf) != buf.capacity()) {
                throw new IOException("Unable to read "
                        + database.getName()
                        + " into memory. Unexpected end of stream.");
            }
            this.buffer = buf.asReadOnlyBuffer();
        } else {
            this.buffer = channel.map(MapMode.READ_ONLY, 0, channel.size()).asReadOnlyBuffer();
        }
    }
}
 
Example 2
Source File: PointerTest.java    From MaxMind-DB-Reader-java with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("static-method")
@Test
public void testWithPointers() throws IOException {
    File file = ReaderTest.getFile("maps-with-pointers.raw");
    BufferHolder ptf = new BufferHolder(file, FileMode.MEMORY);
    Decoder decoder = new Decoder(NoCache.getInstance(), ptf.get(), 0);

    ObjectMapper om = new ObjectMapper();

    ObjectNode map = om.createObjectNode();
    map.put("long_key", "long_value1");
    assertEquals(map, decoder.decode(0));

    map = om.createObjectNode();
    map.put("long_key", "long_value2");
    assertEquals(map, decoder.decode(22));

    map = om.createObjectNode();
    map.put("long_key2", "long_value1");
    assertEquals(map, decoder.decode(37));

    map = om.createObjectNode();
    map.put("long_key2", "long_value2");
    assertEquals(map, decoder.decode(50));

    map = om.createObjectNode();
    map.put("long_key", "long_value1");
    assertEquals(map, decoder.decode(55));

    map = om.createObjectNode();
    map.put("long_key2", "long_value2");
    assertEquals(map, decoder.decode(57));
}
 
Example 3
Source File: GeoIpService.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
private void startReading() throws IOException {
    databaseReader = new Reader(dataFile.toFile(), FileMode.MEMORY, new CHMCache());
    logger.info(LICENSE);

    // clear downloading flag, because we now have working reader instance
    downloading = false;
}
 
Example 4
Source File: DatabaseReader.java    From GeoIP2-java with Apache License 2.0 5 votes vote down vote up
/**
 * @param val The file mode used to open the GeoIP2 database
 * @return Builder object
 * @throws java.lang.IllegalArgumentException if you initialized the Builder with a URL, which uses
 *                                            {@link FileMode#MEMORY}, but you provided a different
 *                                            FileMode to this method.
 */
public Builder fileMode(FileMode val) {
    if (this.stream != null && FileMode.MEMORY != val) {
        throw new IllegalArgumentException(
                "Only FileMode.MEMORY is supported when using an InputStream.");
    }
    this.mode = val;
    return this;
}