2 * Copyright © 2010 Anthony Towns <aj@erisian.com.au>
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.
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.
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.
21 import javax.imageio.ImageIO;
22 import java.awt.image.*;
25 import java.net.URLConnection;
27 public class AltosSiteMapCache extends JLabel {
28 public static boolean fetchMap(File file, String url) {
33 } catch (java.net.MalformedURLException e) {
39 URLConnection uc = u.openConnection();
40 int contentLength = uc.getContentLength();
41 InputStream in = new BufferedInputStream(uc.getInputStream());
44 data = new byte[contentLength];
45 while (offset < contentLength) {
46 bytesRead = in.read(data, offset, data.length - offset);
53 if (offset != contentLength) {
56 } catch (IOException e) {
61 FileOutputStream out = new FileOutputStream(file);
65 } catch (FileNotFoundException e) {
67 } catch (IOException e) {
76 public static ImageIcon fetchAndLoadMap(File pngfile, String url) {
77 if (!pngfile.exists()) {
78 if (!fetchMap(pngfile, url)) {
82 return loadMap(pngfile, url);
85 public static ImageIcon loadMap(File pngfile, String url) {
86 if (!pngfile.exists()) {
93 img = ImageIO.read(pngfile);
95 System.out.printf("# Can't read pngfile %s\n", pngfile);
98 return new ImageIcon(img);
99 } catch (IOException e) {
100 System.out.printf("# IO error trying to load %s\n", pngfile);