File: TileDownloader.java

package info (click to toggle)
gpsprune 26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,824 kB
  • sloc: java: 52,154; sh: 25; makefile: 21; python: 15
file content (56 lines) | stat: -rw-r--r-- 1,358 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package tim.prune.gui.map.tile;

import tim.prune.GpsPrune;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * Single worker instance processing definitions from the coordinator
 */
public class TileDownloader extends TileWorker
{
	public TileDownloader(Coordinator inParent) {
		super(inParent);
	}

	@Override
	protected TileBytes processTile(TileDef def)
	{
		URL tileUrl = def.getUrl();
		if (tileUrl == null) {
			return null;
		}
		InputStream in = null;
		TileBytes result = null;
		try
		{
			URLConnection conn = tileUrl.openConnection();
			conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER);
			in = conn.getInputStream();
			result = new TileBytes();
			int bytesRead = 0;
			byte[] buffer = new byte[4096];
			while (bytesRead >= 0)
			{
				bytesRead = in.read(buffer, 0, buffer.length);
				result.addBytes(buffer, bytesRead);
			}

			// TODO: if this worked, maybe we just came back online?
		}
		catch (IOException e)
		{
			System.err.println("IOE: " + e.getClass().getName() + " - " + e.getMessage());
			// TODO: if this didn't work, maybe we are now offline, or maybe we just need to block this URL to avoid retries?
		}
		finally {
			try {
				in.close();
			} catch (IOException | NullPointerException ignored) {}
		}
		return result;
	}
}