]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/gui/map/TileDownloader.java
Moved source into separate src directory due to popular request
[GpsPrune.git] / src / tim / prune / gui / map / TileDownloader.java
1 package tim.prune.gui.map;
2
3 import java.awt.Image;
4 import java.awt.Toolkit;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.net.URL;
8 import java.net.URLConnection;
9 import java.util.HashSet;
10
11 import tim.prune.GpsPrune;
12
13 /**
14  * Class to asynchronously download a tile from a url
15  * and populate an Image object with the contents
16  */
17 public class TileDownloader implements Runnable
18 {
19         private MapTileManager _manager = null;
20         private URL _url = null;
21         private int _layer = 0;
22         private int _x = 0, _y = 0;
23         private int _zoom = 0;
24         /** Hashset of all blocked / 404 tiles to avoid requesting them again */
25         private static final HashSet<String> BLOCKED_URLS = new HashSet<String>();
26         /** Hashset of all currently loading tiles to avoid requesting them again */
27         private static final HashSet<String> LOADING_URLS = new HashSet<String>();
28
29
30         /**
31          * Constructor (private)
32          * @param inManager parent manager for callback
33          * @param inUrl URL to load
34          * @param inLayer layer index from 0
35          * @param inX x coordinate of tile
36          * @param inY y coordinate of tile
37          * @param inZoom zoom level
38          */
39         private TileDownloader(MapTileManager inManager, URL inUrl, int inLayer, int inX, int inY, int inZoom)
40         {
41                 _manager = inManager;
42                 _url = inUrl;
43                 _layer = inLayer;
44                 _x = inX; _y = inY;
45                 _zoom = inZoom;
46         }
47
48         /**
49          * Trigger a download in a new thread
50          * @param inManager manager to callback when image is loaded
51          * @param inUrl URL to load
52          * @param inLayer layer index from 0
53          * @param inX x coordinate of tile
54          * @param inY y coordinate of tile
55          * @param inZoom current zoom level
56          */
57         public static synchronized void triggerLoad(MapTileManager inManager, URL inUrl, int inLayer,
58                 int inX, int inY, int inZoom)
59         {
60                 if (inManager != null && inUrl != null)
61                 {
62                         String url = inUrl.toString();
63                         // System.out.println("Trigger load: " + url);
64                         if (!BLOCKED_URLS.contains(url) && !LOADING_URLS.contains(url))
65                         {
66                                 // System.out.println("Not blocked: " + url);
67                                 LOADING_URLS.add(url);
68                                 new Thread(new TileDownloader(inManager, inUrl, inLayer, inX, inY, inZoom)).start();
69                         }
70                 }
71         }
72
73         /**
74          * Run method, called in separate thread
75          */
76         public void run()
77         {
78                 InputStream in = null;
79                 try
80                 {
81                         // System.out.println("TD Running thread to get: " + _url.toString());
82                         // Set http user agent on connection
83                         URLConnection conn = _url.openConnection();
84                         conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER);
85                         in = conn.getInputStream();
86                         int len = conn.getContentLength();
87                         if (len > 0)
88                         {
89                                 byte[] data = new byte[len];
90                                 int totalRead = 0;
91                                 while (totalRead < len)
92                                 {
93                                         int numRead = in.read(data, totalRead, len-totalRead);
94                                         totalRead += numRead;
95                                 }
96                                 Image tile = Toolkit.getDefaultToolkit().createImage(data);
97                                 in.close();
98
99                                 // Pass back to manager so it can be stored in its memory cache
100                                 _manager.notifyImageLoaded(tile, _layer, _x, _y, _zoom);
101                         }
102                 }
103                 catch (IOException e)
104                 {
105                         System.err.println("IOE: " + e.getClass().getName() + " - " + e.getMessage());
106                         synchronized(this.getClass())
107                         {
108                                 BLOCKED_URLS.add(_url.toString());
109                         }
110                         try {in.close();} catch (Exception e2) {}
111                 }
112                 LOADING_URLS.remove(_url.toString());
113         }
114 }