X-Git-Url: http://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2Fgui%2Fmap%2FDiskTileCacher.java;h=fd2042a7714acfe2ca1f5b47693ce9df06b0a79f;hb=79f3ca2954d7feb6052c21b3aa974c89b2b101d6;hp=350b3b4b1cdb1f2bbc53ff9f98accadbb1d0bbe5;hpb=4d5796d02a15808311c09448d79e6e7d1de9d636;p=GpsPrune.git diff --git a/tim/prune/gui/map/DiskTileCacher.java b/tim/prune/gui/map/DiskTileCacher.java index 350b3b4..fd2042a 100644 --- a/tim/prune/gui/map/DiskTileCacher.java +++ b/tim/prune/gui/map/DiskTileCacher.java @@ -40,17 +40,15 @@ public class DiskTileCacher implements Runnable _url = inUrl; _file = inFile; _observer = inObserver; - new Thread(this).start(); } /** * Get the specified tile from the disk cache * @param inBasePath base path to whole disk cache * @param inTilePath relative path to requested tile - * @param inCheckAge true to check age of file, false to ignore * @return tile image if available, or null if not there */ - public static Image getTile(String inBasePath, String inTilePath, boolean inCheckAge) + public static MapTile getTile(String inBasePath, String inTilePath) { if (inBasePath == null) {return null;} File tileFile = new File(inBasePath, inTilePath); @@ -58,15 +56,17 @@ public class DiskTileCacher implements Runnable if (tileFile.exists() && tileFile.canRead() && tileFile.length() > 0) { long fileStamp = tileFile.lastModified(); - if (!inCheckAge || ((System.currentTimeMillis()-fileStamp) < CACHE_TIME_LIMIT)) + boolean isExpired = ((System.currentTimeMillis()-fileStamp) > CACHE_TIME_LIMIT); + try { - try { - image = Toolkit.getDefaultToolkit().createImage(tileFile.getAbsolutePath()); - } - catch (Exception e) {} + image = Toolkit.getDefaultToolkit().createImage(tileFile.getAbsolutePath()); + return new MapTile(image, isExpired); + } + catch (Exception e) { + System.err.println("createImage: " + e.getClass().getName() + " _ " + e.getMessage()); } } - return image; + return null; } /** @@ -75,31 +75,28 @@ public class DiskTileCacher implements Runnable * @param inBasePath base path to disk cache * @param inTilePath relative path to this tile * @param inObserver observer to inform when load complete - * @return true if successful, false for failure */ - public static boolean saveTile(URL inUrl, String inBasePath, String inTilePath, ImageObserver inObserver) + public static void saveTile(URL inUrl, String inBasePath, String inTilePath, ImageObserver inObserver) { - if (inBasePath == null || inTilePath == null) {return false;} + if (inBasePath == null || inTilePath == null) {return;} // save file if possible File basePath = new File(inBasePath); if (!basePath.exists() || !basePath.isDirectory() || !basePath.canWrite()) { // Can't write to base path - return false; + return; } File tileFile = new File(basePath, inTilePath); // Check if this file is already being loaded - if (isBeingLoaded(tileFile)) {return true;} + if (isBeingLoaded(tileFile)) {return;} // Check if it has already failed - if (BLOCKED_URLS.contains(inUrl.toString())) {return true;} + if (BLOCKED_URLS.contains(inUrl.toString())) {return;} File dir = tileFile.getParentFile(); // Start a new thread to load the image if necessary if ((dir.exists() || dir.mkdirs()) && dir.canWrite()) { - new DiskTileCacher(inUrl, tileFile, inObserver); - return true; + new Thread(new DiskTileCacher(inUrl, tileFile, inObserver)).start(); } - return false; // couldn't write the file } /** @@ -110,7 +107,12 @@ public class DiskTileCacher implements Runnable private static boolean isBeingLoaded(File inFile) { File tempFile = new File(inFile.getAbsolutePath() + ".temp"); - return tempFile.exists(); + if (!tempFile.exists()) { + return false; + } + // File exists, so check if it was created recently + final long fileAge = System.currentTimeMillis() - tempFile.lastModified(); + return fileAge < 1000000L; // overwrite if the temp file is still there after 1000s } /** @@ -125,7 +127,7 @@ public class DiskTileCacher implements Runnable // Use a synchronized block across all threads to make sure this url is only fetched once synchronized (DiskTileCacher.class) { - if (tempFile.exists()) {return;} + if (tempFile.exists()) {tempFile.delete();} try { if (!tempFile.createNewFile()) {return;} } @@ -135,6 +137,7 @@ public class DiskTileCacher implements Runnable { // Open streams from URL and to file out = new FileOutputStream(tempFile); + //System.out.println("Opening URL: " + _url.toString()); // Set http user agent on connection URLConnection conn = _url.openConnection(); conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER); @@ -159,7 +162,7 @@ public class DiskTileCacher implements Runnable } } // Move temp file to desired file location - if (!tempFile.renameTo(_file)) + if (tempFile.exists() && !tempFile.renameTo(_file)) { // File couldn't be moved - delete both to be sure tempFile.delete();