Browse Source

Image caching

master
Chris Smith 15 years ago
parent
commit
08f16e5331

+ 11
- 3
src/uk/co/md87/evetool/ImageManager.java View File

@@ -25,7 +25,6 @@ package uk.co.md87.evetool;
25 25
 import java.awt.Image;
26 26
 import java.io.File;
27 27
 import java.io.IOException;
28
-import java.net.URL;
29 28
 import java.util.ArrayList;
30 29
 import java.util.List;
31 30
 import java.util.concurrent.atomic.AtomicInteger;
@@ -34,6 +33,7 @@ import java.util.logging.Logger;
34 33
 
35 34
 import javax.imageio.ImageIO;
36 35
 
36
+import uk.co.md87.evetool.api.io.Downloader;
37 37
 import uk.co.md87.evetool.api.io.QueueSizeListener;
38 38
 
39 39
 /**
@@ -45,6 +45,8 @@ public class ImageManager {
45 45
 
46 46
     protected static final List<String> REQUESTS = new ArrayList<String>();
47 47
 
48
+    protected static final String ILLEGAL_CHARS = "[\\\\\"/:\\*\\?\"<>\\|]";
49
+
48 50
     private static final List<QueueSizeListener> listeners = new ArrayList<QueueSizeListener>();
49 51
 
50 52
     private static final AtomicInteger queueSize = new AtomicInteger(0);
@@ -63,6 +65,7 @@ public class ImageManager {
63 65
 
64 66
     public Image getImage(final ImageType type, final Object ... arguments) throws IOException {
65 67
         final String url = type.getUrl(arguments);
68
+        final String cacheUrl = url.replaceAll(ILLEGAL_CHARS, "_");
66 69
 
67 70
         synchronized (REQUESTS) {
68 71
             fireQueueSizeChange(queueSize.incrementAndGet());
@@ -79,8 +82,13 @@ public class ImageManager {
79 82
         }
80 83
 
81 84
         try {
82
-            final Image res = ImageIO.read(new URL(url));
83
-            return res;
85
+            final File file = new File(cacheDir, cacheUrl);
86
+            
87
+            if (!file.exists()) {
88
+                Downloader.downloadPage(url, file);
89
+            }
90
+
91
+            return ImageIO.read(file);
84 92
         } finally {
85 93
             synchronized (REQUESTS) {
86 94
                 fireQueueSizeChange(queueSize.decrementAndGet());

+ 32
- 0
src/uk/co/md87/evetool/api/io/Downloader.java View File

@@ -24,7 +24,10 @@ package uk.co.md87.evetool.api.io;
24 24
 
25 25
 import java.io.BufferedReader;
26 26
 import java.io.DataOutputStream;
27
+import java.io.File;
28
+import java.io.FileOutputStream;
27 29
 import java.io.IOException;
30
+import java.io.InputStream;
28 31
 import java.io.InputStreamReader;
29 32
 import java.io.UnsupportedEncodingException;
30 33
 import java.net.URL;
@@ -113,6 +116,35 @@ public final class Downloader {
113 116
 
114 117
         return data.length() == 0 ? "" : data.substring(1);
115 118
     }
119
+
120
+    /**
121
+     * Downloads the specified page to disk.
122
+     *
123
+     * @param url The URL to retrieve
124
+     * @param file The file to save the page to
125
+     * @throws java.io.IOException If there's an I/O error while downloading
126
+     */
127
+    public static void downloadPage(final String url, final File file) throws IOException {
128
+
129
+        final URLConnection urlConn = getConnection(url, "");
130
+
131
+        final FileOutputStream output = new FileOutputStream(file);
132
+        final InputStream input = urlConn.getInputStream();
133
+
134
+        final byte[] buffer = new byte[512];
135
+        int count;
136
+
137
+        do {
138
+            count = input.read(buffer);
139
+
140
+            if (count > 0) {
141
+                output.write(buffer, 0, count);
142
+            }
143
+        } while (count > 0);
144
+
145
+        input.close();
146
+        output.close();
147
+    }
116 148
         
117 149
     /**
118 150
      * Creates an URL connection for the specified URL and data.

Loading…
Cancel
Save