Skip to content

Commit

Permalink
Merge pull request #784 from mapsforge/mapsforge
Browse files Browse the repository at this point in the history
Mapsforge: map stream support
  • Loading branch information
devemux86 authored Sep 24, 2020
2 parents 5c3a728 + 6116ebb commit e3b4ff5
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 127 deletions.
1 change: 1 addition & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## New since 0.14.0

- Mapsforge: map stream support [#784](https://github.com/mapsforge/vtm/pull/784)
- Render theme from Android content providers [#783](https://github.com/mapsforge/vtm/pull/783)
- Many other minor improvements and bug fixes
- [Solved issues](https://github.com/mapsforge/vtm/issues?q=is%3Aclosed+milestone%3A0.15.0)
Expand Down
27 changes: 16 additions & 11 deletions vtm/src/org/oscim/tiling/source/mapfile/IndexCache.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
* Copyright 2017-2020 devemux86
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
Expand All @@ -20,7 +21,8 @@
import org.oscim.utils.LRUCache;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Collections;
import java.util.Map;
import java.util.logging.Level;
Expand All @@ -44,15 +46,15 @@ class IndexCache {
* SubFileParameter.BYTES_PER_INDEX_ENTRY;

private final Map<IndexCacheEntryKey, byte[]> map;
private final RandomAccessFile randomAccessFile;
private final FileChannel fileChannel;

/**
* @param randomAccessFile the map file from which the index should be read and cached.
* @param capacity the maximum number of entries in the cache.
* @param inputChannel the map file from which the index should be read and cached.
* @param capacity the maximum number of entries in the cache.
* @throws IllegalArgumentException if the capacity is negative.
*/
IndexCache(RandomAccessFile randomAccessFile, int capacity) {
this.randomAccessFile = randomAccessFile;
IndexCache(FileChannel inputChannel, int capacity) {
this.fileChannel = inputChannel;
this.map = Collections.synchronizedMap(new LRUCache<IndexCacheEntryKey, byte[]>(capacity));
}

Expand Down Expand Up @@ -97,11 +99,14 @@ synchronized long getIndexEntry(SubFileParameter subFileParameter, long blockNum
int remainingIndexSize = (int) (subFileParameter.indexEndAddress - indexBlockPosition);
int indexBlockSize = Math.min(SIZE_OF_INDEX_BLOCK, remainingIndexSize);
indexBlock = new byte[indexBlockSize];

this.randomAccessFile.seek(indexBlockPosition);
if (this.randomAccessFile.read(indexBlock, 0, indexBlockSize) != indexBlockSize) {
LOG.warning("reading the current index block has failed");
return -1;
ByteBuffer indexBlockWrapper = ByteBuffer.wrap(indexBlock, 0, indexBlockSize);

synchronized (this.fileChannel) {
this.fileChannel.position(indexBlockPosition);
if (this.fileChannel.read(indexBlockWrapper) != indexBlockSize) {
LOG.warning("reading the current index block has failed");
return -1;
}
}

// put the index block in the map
Expand Down
Loading

0 comments on commit e3b4ff5

Please sign in to comment.