1 package be
.nikiroo
.utils
;
4 import java
.io
.FileInputStream
;
5 import java
.io
.FileNotFoundException
;
6 import java
.io
.IOException
;
7 import java
.io
.InputStream
;
12 * A generic cache system, with special support for {@link URL}s.
14 * This cache also manages timeout information.
20 private long tooOldChanging
;
21 private long tooOldStable
;
24 * Create a new {@link Cache} object.
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
33 * the number of hours after which a cached file that is thought
34 * to change rarely is considered too old (or -1 for
38 * in case of I/O error
40 public Cache(File dir
, int hoursChanging
, int hoursStable
)
43 this.tooOldChanging
= 1000 * 60 * 60 * hoursChanging
;
44 this.tooOldStable
= 1000 * 60 * 60 * hoursStable
;
46 if (dir
!= null && !dir
.exists()) {
50 if (dir
== null || !dir
.exists()) {
51 throw new IOException("Cannot create the cache directory: "
52 + (dir
== null ?
"null" : dir
.getAbsolutePath()));
57 * Check the resource to see if it is in the cache.
60 * the resource to check
62 * allow files even if they are considered too old
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
67 * @return TRUE if it is
70 public boolean check(URL url
, boolean allowTooOld
, boolean stable
) {
71 File file
= getCached(url
);
73 if (allowTooOld
|| !isOld(file
, stable
)) {
82 * Clean the cache (delete the cached items).
85 * only clean the files that are considered too old for a stable
88 * @return the number of cleaned items
90 public int clean(boolean onlyOld
) {
91 return clean(onlyOld
, dir
);
95 * Trace information (info/error) generated by this class.
97 * You can override it if you don't want the default sysout/syserr.
102 * TRUE for error messages, FALSE for information messages
104 protected void trace(String message
, boolean error
) {
106 System
.err
.println(message
);
108 System
.out
.println(message
);
113 * Clean the cache (delete the cached items) in the given cache directory.
116 * only clean the files that are considered too old for stable
119 * the cache directory to clean
121 * @return the number of cleaned items
123 private int clean(boolean onlyOld
, File cacheDir
) {
125 for (File file
: cacheDir
.listFiles()) {
126 if (file
.isDirectory()) {
127 num
+= clean(onlyOld
, file
);
129 if (!onlyOld
|| isOld(file
, true)) {
133 trace("Cannot delete temporary file: "
134 + file
.getAbsolutePath(), true);
144 * Open a resource from the cache if it exists.
149 * allow files even if they are considered too old
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
154 * @return the opened resource if found, NULL if not
156 * @throws IOException
157 * in case of I/O error
159 public InputStream
load(String uniqueID
, boolean allowTooOld
, boolean stable
) {
160 return load(getCached(uniqueID
), allowTooOld
, stable
);
164 * Open a resource from the cache if it exists.
167 * the resource to open
169 * allow files even if they are considered too old
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
174 * @return the opened resource if found, NULL if not
176 * @throws IOException
177 * in case of I/O error
179 public InputStream
load(URL url
, boolean allowTooOld
, boolean stable
)
181 return load(getCached(url
), allowTooOld
, stable
);
185 * Open a resource from the cache if it exists.
188 * the resource to open
190 * allow files even if they are considered too old
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
195 * @return the opened resource if found, NULL if not
197 * @throws IOException
198 * in case of I/O error
200 private InputStream
load(File cached
, boolean allowTooOld
, boolean stable
) {
201 if (cached
.exists() && (allowTooOld
|| !isOld(cached
, stable
))) {
203 return new MarkableFileInputStream(new FileInputStream(cached
));
204 } catch (FileNotFoundException e
) {
213 * Save the given resource to the cache.
218 * a unique ID used to locate the cached resource
220 * @return the resulting {@link File}
222 * @throws IOException
223 * in case of I/O error
225 public File
save(InputStream in
, String uniqueID
) throws IOException
{
226 File cached
= getCached(uniqueID
);
227 cached
.getParentFile().mkdirs();
228 return save(in
, cached
);
232 * Save the given resource to the cache.
237 * the {@link URL} used to locate the cached resource
239 * @throws IOException
240 * in case of I/O error
242 public File
save(InputStream in
, URL url
) throws IOException
{
243 File cached
= getCached(url
);
244 return save(in
, cached
);
248 * Save the given resource to the cache.
253 * the cached {@link File} to save to
255 * @throws IOException
256 * in case of I/O error
258 private File
save(InputStream in
, File cached
) throws IOException
{
259 IOUtils
.write(in
, cached
);
264 * Check if the {@link File} is too old according to
265 * {@link Cache#tooOldChanging}.
270 * TRUE to denote stable files, that are not supposed to change
273 * @return TRUE if it is
275 private boolean isOld(File file
, boolean stable
) {
276 long max
= tooOldChanging
;
285 long time
= new Date().getTime() - file
.lastModified();
287 trace("Timestamp in the future for file: " + file
.getAbsolutePath(),
291 return time
< 0 || time
> max
;
295 * Return the associated cache {@link File} from this {@link URL}.
300 * @return the cached {@link File} version of this {@link URL}
302 private File
getCached(URL url
) {
305 String name
= url
.getHost();
306 if (name
== null || name
.isEmpty()) {
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());
314 File subsubDir
= new File(dir
, allowedChars(url
.getHost()));
315 subdir
= new File(subsubDir
, "_" + allowedChars(url
.getPath()));
316 name
= allowedChars("_" + url
.getQuery());
319 File cacheFile
= new File(subdir
, name
);
326 * Get the basic cache resource file corresponding to this unique ID.
328 * Note that you may need to add a sub-directory in some cases.
333 * @return the cached version if present, NULL if not
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());
342 * Replace not allowed chars (in a {@link File}) by "_".
345 * the raw {@link String}
347 * @return the sanitised {@link String}
349 private String
allowedChars(String raw
) {
350 return raw
.replace('/', '_').replace(':', '_').replace("\\", "_");