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.
18 package org.altusmetrum.altosuilib_2;
21 import javax.imageio.ImageIO;
22 import java.awt.image.*;
27 class AltosCacheImage {
35 public void load_image() throws IOException {
36 BufferedImage bimg = ImageIO.read(file);
38 throw new IOException("Can't load image file");
39 Graphics2D g = image.createGraphics();
40 g.drawImage(bimg, 0, 0, null);
45 public Image validate() {
49 returnCode = image.validate(component.getGraphicsConfiguration());
51 returnCode = VolatileImage.IMAGE_INCOMPATIBLE;
52 if (returnCode == VolatileImage.IMAGE_RESTORED) {
55 } catch (IOException e) {
58 } else if (returnCode == VolatileImage.IMAGE_INCOMPATIBLE) {
59 image = component.createVolatileImage(width, height);
62 } catch (IOException e) {
73 public AltosCacheImage(Component component, File file, int w, int h) throws IOException {
74 this.component = component;
78 image = component.createVolatileImage(w, h);
83 public class AltosSiteMapCache {
84 static final long google_maps_ratelimit_ms = 1200;
85 // Google limits static map queries to 50 per minute per IP, so
86 // each query should take at least 1.2 seconds.
88 static final int success = 0;
89 static final int loading = 1;
90 static final int failed = 2;
91 static final int bad_request = 3;
92 static final int forbidden = 4;
94 public static boolean has_map(File file, String url) {
98 static long forbidden_time;
99 static boolean forbidden_set = false;
100 static final long forbidden_interval = 60l * 1000l * 1000l * 1000l;
102 public static synchronized int fetch_map(File file, String url) {
106 if (forbidden_set && (System.nanoTime() - forbidden_time) < forbidden_interval)
110 long startTime = System.nanoTime();
114 } catch (java.net.MalformedURLException e) {
119 URLConnection uc = null;
121 uc = u.openConnection();
122 String type = uc.getContentType();
123 int contentLength = uc.getContentLength();
124 if (uc instanceof HttpURLConnection) {
125 int response = ((HttpURLConnection) uc).getResponseCode();
127 case HttpURLConnection.HTTP_FORBIDDEN:
128 case HttpURLConnection.HTTP_PAYMENT_REQUIRED:
129 case HttpURLConnection.HTTP_UNAUTHORIZED:
130 forbidden_time = System.nanoTime();
131 forbidden_set = true;
135 InputStream in = new BufferedInputStream(uc.getInputStream());
138 data = new byte[contentLength];
139 while (offset < contentLength) {
140 bytesRead = in.read(data, offset, data.length - offset);
147 if (offset != contentLength)
150 } catch (IOException e) {
155 FileOutputStream out = new FileOutputStream(file);
159 } catch (FileNotFoundException e) {
161 } catch (IOException e) {
167 long duration_ms = (System.nanoTime() - startTime) / 1000000;
168 if (duration_ms < google_maps_ratelimit_ms) {
170 Thread.sleep(google_maps_ratelimit_ms - duration_ms);
171 } catch (InterruptedException e) {
172 Thread.currentThread().interrupt();
179 static final int cache_size = 12;
181 static AltosCacheImage[] images;
185 public static Image get_image(Component component, File file, int width, int height) {
188 AltosCacheImage image;
190 images = new AltosCacheImage[cache_size];
191 for (int i = 0; i < cache_size; i++) {
198 if (image.component == component && file.equals(image.file)) {
200 return image.validate();
202 if (image.used < age) {
209 image = new AltosCacheImage(component, file, width, height);
211 if (images[oldest] != null)
212 images[oldest].flush();
213 images[oldest] = image;
214 return image.validate();
215 } catch (IOException e) {