1 package tim.prune.gui.map;
4 import java.awt.Toolkit;
5 import java.awt.image.ImageObserver;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.io.InputStream;
11 import java.net.URLConnection;
12 import java.util.HashSet;
14 import tim.prune.GpsPrune;
17 * Class to control the reading and saving of map tiles
20 public class DiskTileCacher implements Runnable
22 /** URL to get image from */
23 private URL _url = null;
24 /** File to save image to */
25 private File _file = null;
26 /** Observer to be notified */
27 private ImageObserver _observer = null;
28 /** Time limit to cache images for */
29 private static final long CACHE_TIME_LIMIT = 20 * 24 * 60 * 60 * 1000; // 20 days in ms
30 /** Hashset of all blocked / 404 tiles to avoid requesting them again */
31 private static final HashSet<String> BLOCKED_URLS = new HashSet<String>();
35 * @param inUrl URL to get
36 * @param inFile file to save to
38 private DiskTileCacher(URL inUrl, File inFile, ImageObserver inObserver)
42 _observer = inObserver;
46 * Get the specified tile from the disk cache
47 * @param inBasePath base path to whole disk cache
48 * @param inTilePath relative path to requested tile
49 * @param inCheckAge true to check age of file, false to ignore
50 * @return tile image if available, or null if not there
52 public static Image getTile(String inBasePath, String inTilePath, boolean inCheckAge)
54 if (inBasePath == null) {return null;}
55 File tileFile = new File(inBasePath, inTilePath);
57 if (tileFile.exists() && tileFile.canRead() && tileFile.length() > 0)
59 long fileStamp = tileFile.lastModified();
60 if (!inCheckAge || ((System.currentTimeMillis()-fileStamp) < CACHE_TIME_LIMIT))
63 image = Toolkit.getDefaultToolkit().createImage(tileFile.getAbsolutePath());
66 System.err.println("createImage: " + e.getClass().getName() + " _ " + e.getMessage());
74 * Save the specified image tile to disk
75 * @param inUrl url to get image from
76 * @param inBasePath base path to disk cache
77 * @param inTilePath relative path to this tile
78 * @param inObserver observer to inform when load complete
79 * @return true if successful, false for failure
81 public static boolean saveTile(URL inUrl, String inBasePath, String inTilePath, ImageObserver inObserver)
83 if (inBasePath == null || inTilePath == null) {return false;}
84 // save file if possible
85 File basePath = new File(inBasePath);
86 if (!basePath.exists() || !basePath.isDirectory() || !basePath.canWrite()) {
87 // Can't write to base path
90 File tileFile = new File(basePath, inTilePath);
91 // Check if this file is already being loaded
92 if (isBeingLoaded(tileFile)) {return true;}
93 // Check if it has already failed
94 if (BLOCKED_URLS.contains(inUrl.toString())) {return true;}
96 File dir = tileFile.getParentFile();
97 // Start a new thread to load the image if necessary
98 if ((dir.exists() || dir.mkdirs()) && dir.canWrite())
100 new Thread(new DiskTileCacher(inUrl, tileFile, inObserver)).start();
103 return false; // couldn't write the file
107 * Check whether the given tile is already being loaded
108 * @param inFile desired file
109 * @return true if temporary file with this name exists
111 private static boolean isBeingLoaded(File inFile)
113 File tempFile = new File(inFile.getAbsolutePath() + ".temp");
114 if (!tempFile.exists()) {
117 // File exists, so check if it was created recently
118 final long fileAge = System.currentTimeMillis() - tempFile.lastModified();
119 return fileAge < 1000000L; // overwrite if the temp file is still there after 1000s
123 * Run method for loading URL asynchronously and saving to file
127 boolean finished = false;
128 InputStream in = null;
129 FileOutputStream out = null;
130 File tempFile = new File(_file.getAbsolutePath() + ".temp");
131 // Use a synchronized block across all threads to make sure this url is only fetched once
132 synchronized (DiskTileCacher.class)
134 if (tempFile.exists()) {tempFile.delete();}
136 if (!tempFile.createNewFile()) {return;}
138 catch (Exception e) {return;}
142 // Open streams from URL and to file
143 out = new FileOutputStream(tempFile);
144 // Set http user agent on connection
145 URLConnection conn = _url.openConnection();
146 conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER);
147 in = conn.getInputStream();
149 // Loop over each byte in the stream (maybe buffering is more efficient?)
150 while ((d = in.read()) >= 0) {
154 } catch (IOException e) {
155 System.err.println("ioe: " + e.getClass().getName() + " - " + e.getMessage());
156 BLOCKED_URLS.add(_url.toString());
161 try {in.close();} catch (Exception e) {} // ignore
162 try {out.close();} catch (Exception e) {} // ignore
167 // Move temp file to desired file location
168 if (!tempFile.renameTo(_file))
170 // File couldn't be moved - delete both to be sure
174 // Tell parent that load is finished (parameters ignored)
175 _observer.imageUpdate(null, ImageObserver.ALLBITS, 0, 0, 0, 0);