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