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;
24 import java.net.URLConnection;
26 public class AltosSiteMapCache extends JLabel {
27 public static boolean fetchMap(File file, String url) {
32 } catch (java.net.MalformedURLException e) {
38 URLConnection uc = u.openConnection();
39 int contentLength = uc.getContentLength();
40 InputStream in = new BufferedInputStream(uc.getInputStream());
43 data = new byte[contentLength];
44 while (offset < contentLength) {
45 bytesRead = in.read(data, offset, data.length - offset);
52 if (offset != contentLength) {
55 } catch (IOException e) {
60 FileOutputStream out = new FileOutputStream(file);
64 } catch (FileNotFoundException e) {
66 } catch (IOException e) {
75 public static ImageIcon fetchAndLoadMap(File pngfile, String url) {
76 if (!pngfile.exists()) {
77 if (!fetchMap(pngfile, url)) {
81 return loadMap(pngfile, url);
84 public static ImageIcon loadMap(File pngfile, String url) {
85 if (!pngfile.exists()) {
90 return new ImageIcon(ImageIO.read(pngfile));
91 } catch (IOException e) {
92 System.out.printf("# IO error trying to load %s\n", pngfile);