altoslib: create eeprom download thread before telling monitor about it
[fw/altos] / altosui / AltosSiteMapCache.java
1 /*
2  * Copyright © 2010 Anthony Towns <aj@erisian.com.au>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package altosui;
19
20 import javax.swing.*;
21 import javax.imageio.ImageIO;
22 import java.awt.image.*;
23 import java.io.*;
24 import java.net.URL;
25 import java.net.URLConnection;
26
27 public class AltosSiteMapCache extends JLabel {
28         public static boolean fetchMap(File file, String url) {
29                 URL u;
30
31                 try {
32                         u = new URL(url);
33                 } catch (java.net.MalformedURLException e) {
34                         return false;
35                 }
36
37                 byte[] data;
38                 try {
39                         URLConnection uc = u.openConnection();
40                         int contentLength = uc.getContentLength();
41                         InputStream in = new BufferedInputStream(uc.getInputStream());
42                         int bytesRead = 0;
43                         int offset = 0;
44                         data = new byte[contentLength];
45                         while (offset < contentLength) {
46                                 bytesRead = in.read(data, offset, data.length - offset);
47                                 if (bytesRead == -1)
48                                         break;
49                                 offset += bytesRead;
50                         }
51                         in.close();
52
53                         if (offset != contentLength) {
54                                 return false;
55                         }
56                 } catch (IOException e) {
57                         return false;
58                 }
59
60                 try {
61                         FileOutputStream out = new FileOutputStream(file);
62                         out.write(data);
63                         out.flush();
64                         out.close();
65                 } catch (FileNotFoundException e) {
66                         return false;
67                 } catch (IOException e) {
68                         if (file.exists()) {
69                                 file.delete();
70                         }
71                         return false;
72                 }
73                 return true;
74         }
75
76         public static ImageIcon fetchAndLoadMap(File pngfile, String url) {
77                 if (!pngfile.exists()) {
78                         if (!fetchMap(pngfile, url)) {
79                                 return null;
80                         }
81                 }
82                 return loadMap(pngfile, url);
83         }
84
85         public static ImageIcon loadMap(File pngfile, String url) {
86                 if (!pngfile.exists()) {
87                         return null;
88                 }
89
90                 try {
91                         BufferedImage   img;
92
93                         img = ImageIO.read(pngfile);
94                         if (img == null) {
95                                 System.out.printf("# Can't read pngfile %s\n", pngfile);
96                                 return null;
97                         }
98                         return new ImageIcon(img);
99                 } catch (IOException e) {
100                         System.out.printf("# IO error trying to load %s\n", pngfile);
101                         return null;
102                 }
103         }
104 }