altos: Add initial micropeak implementation
[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.io.*;
23 import java.net.URL;
24 import java.net.URLConnection;
25
26 public class AltosSiteMapCache extends JLabel {
27         public static boolean fetchMap(File file, String url) {
28                 URL u;
29
30                 try {
31                         u = new URL(url);
32                 } catch (java.net.MalformedURLException e) {
33                         return false;
34                 }
35
36                 byte[] data;
37                 try {
38                         URLConnection uc = u.openConnection();
39                         int contentLength = uc.getContentLength();
40                         InputStream in = new BufferedInputStream(uc.getInputStream());
41                         int bytesRead = 0;
42                         int offset = 0;
43                         data = new byte[contentLength];
44                         while (offset < contentLength) {
45                                 bytesRead = in.read(data, offset, data.length - offset);
46                                 if (bytesRead == -1)
47                                         break;
48                                 offset += bytesRead;
49                         }
50                         in.close();
51
52                         if (offset != contentLength) {
53                                 return false;
54                         }
55                 } catch (IOException e) {
56                         return false;
57                 }
58
59                 try {
60                         FileOutputStream out = new FileOutputStream(file);
61                         out.write(data);
62                         out.flush();
63                         out.close();
64                 } catch (FileNotFoundException e) {
65                         return false;
66                 } catch (IOException e) {
67                         if (file.exists()) {
68                                 file.delete();
69                         }
70                         return false;
71                 }
72                 return true;
73         }
74
75         public static ImageIcon fetchAndLoadMap(File pngfile, String url) {
76                 if (!pngfile.exists()) {
77                         if (!fetchMap(pngfile, url)) {
78                                 return null;
79                         }
80                 }
81                 return loadMap(pngfile, url);
82         }
83
84         public static ImageIcon loadMap(File pngfile, String url) {
85                 if (!pngfile.exists()) {
86                         return null;
87                 }
88
89                 try {
90                         return new ImageIcon(ImageIO.read(pngfile));
91                 } catch (IOException e) {
92                         System.out.printf("# IO error trying to load %s\n", pngfile);
93                         return null;
94                 }
95         }
96 }