]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/gui/map/DiskTileCacher.java
Version 13, August 2011
[GpsPrune.git] / tim / prune / gui / map / DiskTileCacher.java
index dfc4f927aed2aee0baef04623c37831e824a5156..84f54def64ec34b4a004c75dae4342f1db083cb0 100644 (file)
@@ -68,28 +68,29 @@ 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 void saveTile(URL inUrl, String inBasePath, String inTilePath, ImageObserver inObserver)
+       public static boolean saveTile(URL inUrl, String inBasePath, String inTilePath, ImageObserver inObserver)
        {
-               if (inBasePath == null || inTilePath == null) {return;}
+               if (inBasePath == null || inTilePath == null) {return false;}
                // save file if possible
                File basePath = new File(inBasePath);
                if (!basePath.exists() || !basePath.isDirectory() || !basePath.canWrite()) {
-                       //System.err.println("Can't write");
                        // Can't write to base path
-                       return;
+                       return false;
                }
                File tileFile = new File(basePath, inTilePath);
                // Check if this file is already being loaded
-               if (!isBeingLoaded(tileFile))
+               if (isBeingLoaded(tileFile)) {return true;}
+
+               File dir = tileFile.getParentFile();
+               // Start a new thread to load the image if necessary
+               if ((dir.exists() || dir.mkdirs()) && dir.canWrite())
                {
-                       File dir = tileFile.getParentFile();
-                       // Start a new thread to load the image if necessary
-                       if (dir.exists() || dir.mkdirs())
-                       {
-                               new DiskTileCacher(inUrl, tileFile, inObserver);
-                       }
+                       new DiskTileCacher(inUrl, tileFile, inObserver);
+                       return true;
                }
+               return false; // couldn't write the file
        }
 
        /**
@@ -120,7 +121,8 @@ public class DiskTileCacher implements Runnable
                        }
                        catch (Exception e) {return;}
                }
-               try {
+               try
+               {
                        // Open streams from URL and to file
                        out = new FileOutputStream(tempFile);
                        in = _url.openStream();
@@ -131,16 +133,18 @@ public class DiskTileCacher implements Runnable
                        }
                        finished = true;
                } catch (IOException e) {}
-               finally {
-                       try {
-                               in.close();
-                               out.close();
-                               if (!finished) {tempFile.delete();}
+               finally
+               {
+                       // clean up files
+                       try {in.close();} catch (Exception e) {} // ignore
+                       try {out.close();} catch (Exception e) {} // ignore
+                       if (!finished) {
+                               tempFile.delete();
                        }
-                       catch (Exception e) {} // ignore
                }
                // Move temp file to desired file location
-               if (!tempFile.renameTo(_file)) {
+               if (!tempFile.renameTo(_file))
+               {
                        // File couldn't be moved - delete both to be sure
                        tempFile.delete();
                        _file.delete();