New: Downloader, Cache
[nikiroo-utils.git] / src / be / nikiroo / utils / Cache.java
1 package be.nikiroo.utils;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.net.URL;
9 import java.util.Date;
10
11 /**
12 * A generic cache system, with special support for {@link URL}s.
13 * <p>
14 * This cache also manages timeout information.
15 *
16 * @author niki
17 */
18 public class Cache {
19 private File dir;
20 private long tooOldChanging;
21 private long tooOldStable;
22
23 /**
24 * Create a new {@link Cache} object.
25 *
26 * @param dir
27 * the directory to use as cache
28 * @param hoursChanging
29 * the number of hours after which a cached file that is thought
30 * to change ~often is considered too old (or -1 for
31 * "never too old")
32 * @param hoursStable
33 * the number of hours after which a cached file that is thought
34 * to change rarely is considered too old (or -1 for
35 * "never too old")
36 *
37 * @throws IOException
38 * in case of I/O error
39 */
40 public Cache(File dir, int hoursChanging, int hoursStable)
41 throws IOException {
42 this.dir = dir;
43 this.tooOldChanging = 1000 * 60 * 60 * hoursChanging;
44 this.tooOldStable = 1000 * 60 * 60 * hoursStable;
45
46 if (dir != null && !dir.exists()) {
47 dir.mkdirs();
48 }
49
50 if (dir == null || !dir.exists()) {
51 throw new IOException("Cannot create the cache directory: "
52 + (dir == null ? "null" : dir.getAbsolutePath()));
53 }
54 }
55
56 /**
57 * Check the resource to see if it is in the cache.
58 *
59 * @param url
60 * the resource to check
61 * @param allowTooOld
62 * allow files even if they are considered too old
63 * @param stable
64 * a stable file (that dones't change too often) -- parameter
65 * used to check if the file is too old to keep or not
66 *
67 * @return TRUE if it is
68 *
69 */
70 public boolean check(URL url, boolean allowTooOld, boolean stable) {
71 File file = getCached(url);
72 if (file.exists()) {
73 if (allowTooOld || !isOld(file, stable)) {
74 return true;
75 }
76 }
77
78 return false;
79 }
80
81 /**
82 * Clean the cache (delete the cached items).
83 *
84 * @param onlyOld
85 * only clean the files that are considered too old for a stable
86 * resource
87 *
88 * @return the number of cleaned items
89 */
90 public int clean(boolean onlyOld) {
91 return clean(onlyOld, dir);
92 }
93
94 /**
95 * Trace information (info/error) generated by this class.
96 * <p>
97 * You can override it if you don't want the default sysout/syserr.
98 *
99 * @param message
100 * the message
101 * @param error
102 * TRUE for error messages, FALSE for information messages
103 */
104 protected void trace(String message, boolean error) {
105 if (error) {
106 System.err.println(message);
107 } else {
108 System.out.println(message);
109 }
110 }
111
112 /**
113 * Clean the cache (delete the cached items) in the given cache directory.
114 *
115 * @param onlyOld
116 * only clean the files that are considered too old for stable
117 * resources
118 * @param cacheDir
119 * the cache directory to clean
120 *
121 * @return the number of cleaned items
122 */
123 private int clean(boolean onlyOld, File cacheDir) {
124 int num = 0;
125 for (File file : cacheDir.listFiles()) {
126 if (file.isDirectory()) {
127 num += clean(onlyOld, file);
128 } else {
129 if (!onlyOld || isOld(file, true)) {
130 if (file.delete()) {
131 num++;
132 } else {
133 trace("Cannot delete temporary file: "
134 + file.getAbsolutePath(), true);
135 }
136 }
137 }
138 }
139
140 return num;
141 }
142
143 /**
144 * Open a resource from the cache if it exists.
145 *
146 * @param uniqueID
147 * the unique ID
148 * @param allowTooOld
149 * allow files even if they are considered too old
150 * @param stable
151 * a stable file (that dones't change too often) -- parameter
152 * used to check if the file is too old to keep or not
153 *
154 * @return the opened resource if found, NULL if not
155 *
156 * @throws IOException
157 * in case of I/O error
158 */
159 public InputStream load(String uniqueID, boolean allowTooOld, boolean stable) {
160 return load(getCached(uniqueID), allowTooOld, stable);
161 }
162
163 /**
164 * Open a resource from the cache if it exists.
165 *
166 * @param url
167 * the resource to open
168 * @param allowTooOld
169 * allow files even if they are considered too old
170 * @param stable
171 * a stable file (that dones't change too often) -- parameter
172 * used to check if the file is too old to keep or not
173 *
174 * @return the opened resource if found, NULL if not
175 *
176 * @throws IOException
177 * in case of I/O error
178 */
179 public InputStream load(URL url, boolean allowTooOld, boolean stable)
180 throws IOException {
181 return load(getCached(url), allowTooOld, stable);
182 }
183
184 /**
185 * Open a resource from the cache if it exists.
186 *
187 * @param url
188 * the resource to open
189 * @param allowTooOld
190 * allow files even if they are considered too old
191 * @param stable
192 * a stable file (that dones't change too often) -- parameter
193 * used to check if the file is too old to keep or not
194 *
195 * @return the opened resource if found, NULL if not
196 *
197 * @throws IOException
198 * in case of I/O error
199 */
200 private InputStream load(File cached, boolean allowTooOld, boolean stable) {
201 if (cached.exists() && (allowTooOld || !isOld(cached, stable))) {
202 try {
203 return new MarkableFileInputStream(new FileInputStream(cached));
204 } catch (FileNotFoundException e) {
205 return null;
206 }
207 }
208
209 return null;
210 }
211
212 /**
213 * Save the given resource to the cache.
214 *
215 * @param in
216 * the input data
217 * @param uniqueID
218 * a unique ID used to locate the cached resource
219 *
220 * @return the resulting {@link File}
221 *
222 * @throws IOException
223 * in case of I/O error
224 */
225 public File save(InputStream in, String uniqueID) throws IOException {
226 File cached = getCached(uniqueID);
227 cached.getParentFile().mkdirs();
228 return save(in, cached);
229 }
230
231 /**
232 * Save the given resource to the cache.
233 *
234 * @param in
235 * the input data
236 * @param url
237 * the {@link URL} used to locate the cached resource
238 *
239 * @throws IOException
240 * in case of I/O error
241 */
242 public File save(InputStream in, URL url) throws IOException {
243 File cached = getCached(url);
244 return save(in, cached);
245 }
246
247 /**
248 * Save the given resource to the cache.
249 *
250 * @param in
251 * the input data
252 * @param cached
253 * the cached {@link File} to save to
254 *
255 * @throws IOException
256 * in case of I/O error
257 */
258 private File save(InputStream in, File cached) throws IOException {
259 IOUtils.write(in, cached);
260 return cached;
261 }
262
263 /**
264 * Check if the {@link File} is too old according to
265 * {@link Cache#tooOldChanging}.
266 *
267 * @param file
268 * the file to check
269 * @param stable
270 * TRUE to denote stable files, that are not supposed to change
271 * too often
272 *
273 * @return TRUE if it is
274 */
275 private boolean isOld(File file, boolean stable) {
276 long max = tooOldChanging;
277 if (stable) {
278 max = tooOldStable;
279 }
280
281 if (max < 0) {
282 return false;
283 }
284
285 long time = new Date().getTime() - file.lastModified();
286 if (time < 0) {
287 trace("Timestamp in the future for file: " + file.getAbsolutePath(),
288 true);
289 }
290
291 return time < 0 || time > max;
292 }
293
294 /**
295 * Return the associated cache {@link File} from this {@link URL}.
296 *
297 * @param url
298 * the {@link URL}
299 *
300 * @return the cached {@link File} version of this {@link URL}
301 */
302 private File getCached(URL url) {
303 File subdir;
304
305 String name = url.getHost();
306 if (name == null || name.isEmpty()) {
307 // File
308 File file = new File(url.getFile());
309 subdir = new File(file.getParent().replace("..", "__"));
310 subdir = new File(dir, allowedChars(subdir.getPath()));
311 name = allowedChars(url.getFile());
312 } else {
313 // URL
314 File subsubDir = new File(dir, allowedChars(url.getHost()));
315 subdir = new File(subsubDir, "_" + allowedChars(url.getPath()));
316 name = allowedChars("_" + url.getQuery());
317 }
318
319 File cacheFile = new File(subdir, name);
320 subdir.mkdirs();
321
322 return cacheFile;
323 }
324
325 /**
326 * Get the basic cache resource file corresponding to this unique ID.
327 * <p>
328 * Note that you may need to add a sub-directory in some cases.
329 *
330 * @param uniqueID
331 * the id
332 *
333 * @return the cached version if present, NULL if not
334 */
335 private File getCached(String uniqueID) {
336 File file = new File(dir, allowedChars(uniqueID));
337 File subdir = new File(file.getParentFile(), "_");
338 return new File(subdir, file.getName());
339 }
340
341 /**
342 * Replace not allowed chars (in a {@link File}) by "_".
343 *
344 * @param raw
345 * the raw {@link String}
346 *
347 * @return the sanitised {@link String}
348 */
349 private String allowedChars(String raw) {
350 return raw.replace('/', '_').replace(':', '_').replace("\\", "_");
351 }
352 }