[1610] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.io;
|
---|
| 3 |
|
---|
| 4 | import java.awt.image.BufferedImage;
|
---|
| 5 | import java.io.File;
|
---|
| 6 | import java.io.RandomAccessFile;
|
---|
| 7 | import java.math.BigInteger;
|
---|
[7082] | 8 | import java.nio.charset.StandardCharsets;
|
---|
[1610] | 9 | import java.security.MessageDigest;
|
---|
| 10 | import java.util.Iterator;
|
---|
| 11 | import java.util.Set;
|
---|
| 12 | import java.util.TreeMap;
|
---|
| 13 |
|
---|
| 14 | import javax.imageio.ImageIO;
|
---|
| 15 |
|
---|
| 16 | import org.openstreetmap.josm.Main;
|
---|
[7132] | 17 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[1610] | 18 |
|
---|
| 19 | /**
|
---|
| 20 | * Use this class if you want to cache a lot of files that shouldn't be kept in memory. You can
|
---|
| 21 | * specify how much data should be stored and after which date the files should be expired.
|
---|
| 22 | * This works on a last-access basis, so files get deleted after they haven't been used for x days.
|
---|
| 23 | * You can turn this off by calling setUpdateModTime(false). Files get deleted on a first-in-first-out
|
---|
| 24 | * basis.
|
---|
| 25 | * @author xeen
|
---|
| 26 | *
|
---|
| 27 | */
|
---|
| 28 | public class CacheFiles {
|
---|
| 29 | /**
|
---|
| 30 | * Common expirey dates
|
---|
| 31 | */
|
---|
[6889] | 32 | public static final int EXPIRE_NEVER = -1;
|
---|
| 33 | public static final int EXPIRE_DAILY = 60 * 60 * 24;
|
---|
| 34 | public static final int EXPIRE_WEEKLY = EXPIRE_DAILY * 7;
|
---|
| 35 | public static final int EXPIRE_MONTHLY = EXPIRE_WEEKLY * 4;
|
---|
[1610] | 36 |
|
---|
[6889] | 37 | private final File dir;
|
---|
| 38 | private final String ident;
|
---|
| 39 | private final boolean enabled;
|
---|
[1610] | 40 |
|
---|
| 41 | private long expire; // in seconds
|
---|
| 42 | private long maxsize; // in megabytes
|
---|
| 43 | private boolean updateModTime = true;
|
---|
| 44 |
|
---|
| 45 | // If the cache is full, we don't want to delete just one file
|
---|
[2990] | 46 | private static final int CLEANUP_TRESHOLD = 20;
|
---|
[1610] | 47 | // We don't want to clean after every file-write
|
---|
[2990] | 48 | private static final int CLEANUP_INTERVAL = 5;
|
---|
[1610] | 49 | // Stores how many files have been written
|
---|
| 50 | private int writes = 0;
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
| 53 | * Creates a new cache class. The ident will be used to store the files on disk and to save
|
---|
[5881] | 54 | * expire/space settings. Set plugin state to <code>true</code>.
|
---|
| 55 | * @param ident cache identifier
|
---|
[1610] | 56 | */
|
---|
| 57 | public CacheFiles(String ident) {
|
---|
[2606] | 58 | this(ident, true);
|
---|
| 59 | }
|
---|
[2711] | 60 |
|
---|
[5881] | 61 | /**
|
---|
| 62 | * Creates a new cache class. The ident will be used to store the files on disk and to save
|
---|
| 63 | * expire/space settings.
|
---|
| 64 | * @param ident cache identifier
|
---|
| 65 | * @param isPlugin Whether this is a plugin or not (changes cache path)
|
---|
| 66 | */
|
---|
[2606] | 67 | public CacheFiles(String ident, boolean isPlugin) {
|
---|
[6070] | 68 | String pref = isPlugin ?
|
---|
[4818] | 69 | Main.pref.getPluginsDirectory().getPath() + File.separator + "cache" :
|
---|
| 70 | Main.pref.getCacheDirectory().getPath();
|
---|
[2711] | 71 |
|
---|
[2016] | 72 | boolean dir_writeable;
|
---|
| 73 | this.ident = ident;
|
---|
[4818] | 74 | String cacheDir = Main.pref.get("cache." + ident + "." + "path", pref + File.separator + ident + File.separator);
|
---|
[2016] | 75 | this.dir = new File(cacheDir);
|
---|
| 76 | try {
|
---|
| 77 | this.dir.mkdirs();
|
---|
| 78 | dir_writeable = true;
|
---|
[1610] | 79 | } catch(Exception e) {
|
---|
[2016] | 80 | // We have no access to this directory, so don't do anything
|
---|
| 81 | dir_writeable = false;
|
---|
[1610] | 82 | }
|
---|
| 83 | this.enabled = dir_writeable;
|
---|
| 84 | this.expire = Main.pref.getLong("cache." + ident + "." + "expire", EXPIRE_DAILY);
|
---|
[2016] | 85 | if(this.expire < 0) {
|
---|
[1610] | 86 | this.expire = CacheFiles.EXPIRE_NEVER;
|
---|
[2016] | 87 | }
|
---|
[1610] | 88 | this.maxsize = Main.pref.getLong("cache." + ident + "." + "maxsize", 50);
|
---|
[2016] | 89 | if(this.maxsize < 0) {
|
---|
[1610] | 90 | this.maxsize = -1;
|
---|
[2016] | 91 | }
|
---|
[1610] | 92 | }
|
---|
| 93 |
|
---|
| 94 | /**
|
---|
| 95 | * Loads the data for the given ident as an byte array. Returns null if data not available.
|
---|
[5881] | 96 | * @param ident cache identifier
|
---|
| 97 | * @return stored data
|
---|
[1610] | 98 | */
|
---|
| 99 | public byte[] getData(String ident) {
|
---|
| 100 | if(!enabled) return null;
|
---|
| 101 | try {
|
---|
| 102 | File data = getPath(ident);
|
---|
| 103 | if(!data.exists())
|
---|
| 104 | return null;
|
---|
| 105 |
|
---|
| 106 | if(isExpired(data)) {
|
---|
| 107 | data.delete();
|
---|
| 108 | return null;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | // Update last mod time so we don't expire recently used data
|
---|
[2016] | 112 | if(updateModTime) {
|
---|
[6102] | 113 | data.setLastModified(System.currentTimeMillis());
|
---|
[2016] | 114 | }
|
---|
[1610] | 115 |
|
---|
| 116 | byte[] bytes = new byte[(int) data.length()];
|
---|
[7033] | 117 | try (RandomAccessFile raf = new RandomAccessFile(data, "r")) {
|
---|
| 118 | raf.readFully(bytes);
|
---|
| 119 | }
|
---|
[1610] | 120 | return bytes;
|
---|
[6248] | 121 | } catch (Exception e) {
|
---|
| 122 | Main.warn(e);
|
---|
[1610] | 123 | }
|
---|
| 124 | return null;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | /**
|
---|
| 128 | * Writes an byte-array to disk
|
---|
[5881] | 129 | * @param ident cache identifier
|
---|
| 130 | * @param data data to store
|
---|
[1610] | 131 | */
|
---|
| 132 | public void saveData(String ident, byte[] data) {
|
---|
| 133 | if(!enabled) return;
|
---|
| 134 | try {
|
---|
| 135 | File f = getPath(ident);
|
---|
[6248] | 136 | if (f.exists()) {
|
---|
[1610] | 137 | f.delete();
|
---|
[2016] | 138 | }
|
---|
[1610] | 139 | // rws also updates the file meta-data, i.e. last mod time
|
---|
[7033] | 140 | try (RandomAccessFile raf = new RandomAccessFile(f, "rws")) {
|
---|
[6248] | 141 | raf.write(data);
|
---|
| 142 | }
|
---|
| 143 | } catch (Exception e) {
|
---|
| 144 | Main.warn(e);
|
---|
[1610] | 145 | }
|
---|
| 146 |
|
---|
| 147 | writes++;
|
---|
| 148 | checkCleanUp();
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | /**
|
---|
| 152 | * Loads the data for the given ident as an image. If no image is found, null is returned
|
---|
[5881] | 153 | * @param ident cache identifier
|
---|
[1610] | 154 | * @return BufferedImage or null
|
---|
| 155 | */
|
---|
| 156 | public BufferedImage getImg(String ident) {
|
---|
| 157 | if(!enabled) return null;
|
---|
| 158 | try {
|
---|
| 159 | File img = getPath(ident, "png");
|
---|
| 160 | if(!img.exists())
|
---|
| 161 | return null;
|
---|
| 162 |
|
---|
| 163 | if(isExpired(img)) {
|
---|
| 164 | img.delete();
|
---|
| 165 | return null;
|
---|
| 166 | }
|
---|
| 167 | // Update last mod time so we don't expire recently used images
|
---|
[2016] | 168 | if(updateModTime) {
|
---|
[6102] | 169 | img.setLastModified(System.currentTimeMillis());
|
---|
[2016] | 170 | }
|
---|
[7132] | 171 | return ImageProvider.read(img, false, false);
|
---|
[6248] | 172 | } catch (Exception e) {
|
---|
| 173 | Main.warn(e);
|
---|
[1610] | 174 | }
|
---|
| 175 | return null;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | /**
|
---|
| 179 | * Saves a given image and ident to the cache
|
---|
[5881] | 180 | * @param ident cache identifier
|
---|
| 181 | * @param image imaga data for storage
|
---|
[1610] | 182 | */
|
---|
| 183 | public void saveImg(String ident, BufferedImage image) {
|
---|
[6248] | 184 | if (!enabled) return;
|
---|
[1610] | 185 | try {
|
---|
| 186 | ImageIO.write(image, "png", getPath(ident, "png"));
|
---|
[6248] | 187 | } catch (Exception e) {
|
---|
| 188 | Main.warn(e);
|
---|
[1610] | 189 | }
|
---|
| 190 |
|
---|
| 191 | writes++;
|
---|
| 192 | checkCleanUp();
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | /**
|
---|
| 196 | * Sets the amount of time data is stored before it gets expired
|
---|
| 197 | * @param amount of time in seconds
|
---|
| 198 | * @param force will also write it to the preferences
|
---|
| 199 | */
|
---|
| 200 | public void setExpire(int amount, boolean force) {
|
---|
| 201 | String key = "cache." + ident + "." + "expire";
|
---|
[4932] | 202 | if(!Main.pref.get(key).isEmpty() && !force)
|
---|
[2016] | 203 | return;
|
---|
| 204 |
|
---|
| 205 | this.expire = amount > 0 ? amount : EXPIRE_NEVER;
|
---|
| 206 | Main.pref.putLong(key, this.expire);
|
---|
[1610] | 207 | }
|
---|
| 208 |
|
---|
| 209 | /**
|
---|
| 210 | * Sets the amount of data stored in the cache
|
---|
| 211 | * @param amount in Megabytes
|
---|
| 212 | * @param force will also write it to the preferences
|
---|
| 213 | */
|
---|
| 214 | public void setMaxSize(int amount, boolean force) {
|
---|
[2016] | 215 | String key = "cache." + ident + "." + "maxsize";
|
---|
[4932] | 216 | if(!Main.pref.get(key).isEmpty() && !force)
|
---|
[2016] | 217 | return;
|
---|
| 218 |
|
---|
[1610] | 219 | this.maxsize = amount > 0 ? amount : -1;
|
---|
[2016] | 220 | Main.pref.putLong(key, this.maxsize);
|
---|
[1610] | 221 | }
|
---|
| 222 |
|
---|
| 223 | /**
|
---|
[5881] | 224 | * Call this with <code>true</code> to update the last modification time when a file it is read.
|
---|
| 225 | * Call this with <code>false</code> to not update the last modification time when a file is read.
|
---|
| 226 | * @param to update state
|
---|
[1610] | 227 | */
|
---|
| 228 | public void setUpdateModTime(boolean to) {
|
---|
| 229 | updateModTime = to;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | /**
|
---|
| 233 | * Checks if a clean up is needed and will do so if necessary
|
---|
| 234 | */
|
---|
| 235 | public void checkCleanUp() {
|
---|
[2990] | 236 | if(this.writes > CLEANUP_INTERVAL) {
|
---|
[1610] | 237 | cleanUp();
|
---|
[2016] | 238 | }
|
---|
[1610] | 239 | }
|
---|
| 240 |
|
---|
| 241 | /**
|
---|
| 242 | * Performs a default clean up with the set values (deletes oldest files first)
|
---|
| 243 | */
|
---|
| 244 | public void cleanUp() {
|
---|
| 245 | if(!this.enabled || maxsize == -1) return;
|
---|
| 246 |
|
---|
[7005] | 247 | TreeMap<Long, File> modtime = new TreeMap<>();
|
---|
[1610] | 248 | long dirsize = 0;
|
---|
| 249 |
|
---|
| 250 | for(File f : dir.listFiles()) {
|
---|
[2016] | 251 | if(isExpired(f)) {
|
---|
[1610] | 252 | f.delete();
|
---|
[2016] | 253 | } else {
|
---|
[1610] | 254 | dirsize += f.length();
|
---|
| 255 | modtime.put(f.lastModified(), f);
|
---|
| 256 | }
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | if(dirsize < maxsize*1000*1000) return;
|
---|
| 260 |
|
---|
| 261 | Set<Long> keySet = modtime.keySet();
|
---|
| 262 | Iterator<Long> it = keySet.iterator();
|
---|
| 263 | int i=0;
|
---|
| 264 | while (it.hasNext()) {
|
---|
| 265 | i++;
|
---|
| 266 | modtime.get(it.next()).delete();
|
---|
| 267 |
|
---|
| 268 | // Delete a couple of files, then check again
|
---|
[2990] | 269 | if(i % CLEANUP_TRESHOLD == 0 && getDirSize() < maxsize)
|
---|
[1610] | 270 | return;
|
---|
| 271 | }
|
---|
| 272 | writes = 0;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
[6889] | 275 | public static final int CLEAN_ALL = 0;
|
---|
| 276 | public static final int CLEAN_SMALL_FILES = 1;
|
---|
| 277 | public static final int CLEAN_BY_DATE = 2;
|
---|
| 278 |
|
---|
[1610] | 279 | /**
|
---|
| 280 | * Performs a non-default, specified clean up
|
---|
| 281 | * @param type any of the CLEAN_XX constants.
|
---|
| 282 | * @param size for CLEAN_SMALL_FILES: deletes all files smaller than (size) bytes
|
---|
| 283 | */
|
---|
| 284 | public void customCleanUp(int type, int size) {
|
---|
| 285 | switch(type) {
|
---|
[2016] | 286 | case CLEAN_ALL:
|
---|
| 287 | for(File f : dir.listFiles()) {
|
---|
| 288 | f.delete();
|
---|
| 289 | }
|
---|
| 290 | break;
|
---|
| 291 | case CLEAN_SMALL_FILES:
|
---|
| 292 | for(File f: dir.listFiles())
|
---|
| 293 | if(f.length() < size) {
|
---|
[1610] | 294 | f.delete();
|
---|
[2016] | 295 | }
|
---|
| 296 | break;
|
---|
| 297 | case CLEAN_BY_DATE:
|
---|
| 298 | cleanUp();
|
---|
| 299 | break;
|
---|
[1610] | 300 | }
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | /**
|
---|
| 304 | * Calculates the size of the directory
|
---|
| 305 | * @return long Size of directory in bytes
|
---|
| 306 | */
|
---|
| 307 | private long getDirSize() {
|
---|
| 308 | if(!enabled) return -1;
|
---|
| 309 | long dirsize = 0;
|
---|
| 310 |
|
---|
[2016] | 311 | for(File f : this.dir.listFiles()) {
|
---|
[1610] | 312 | dirsize += f.length();
|
---|
[2016] | 313 | }
|
---|
[1610] | 314 | return dirsize;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | /**
|
---|
| 318 | * Returns a short and unique file name for a given long identifier
|
---|
| 319 | * @return String short filename
|
---|
| 320 | */
|
---|
| 321 | private static String getUniqueFilename(String ident) {
|
---|
| 322 | try {
|
---|
| 323 | MessageDigest md = MessageDigest.getInstance("MD5");
|
---|
[7082] | 324 | BigInteger number = new BigInteger(1, md.digest(ident.getBytes(StandardCharsets.UTF_8)));
|
---|
[1610] | 325 | return number.toString(16);
|
---|
| 326 | } catch(Exception e) {
|
---|
| 327 | // Fall back. Remove unsuitable characters and some random ones to shrink down path length.
|
---|
| 328 | // Limit it to 70 characters, that leaves about 190 for the path on Windows/NTFS
|
---|
| 329 | ident = ident.replaceAll("[^a-zA-Z0-9]", "");
|
---|
| 330 | ident = ident.replaceAll("[acegikmoqsuwy]", "");
|
---|
| 331 | return ident.substring(ident.length() - 70);
|
---|
| 332 | }
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | /**
|
---|
| 336 | * Gets file path for ident with customizable file-ending
|
---|
[5881] | 337 | * @param ident cache identifier
|
---|
| 338 | * @param ending file extension
|
---|
| 339 | * @return file structure
|
---|
[1610] | 340 | */
|
---|
| 341 | private File getPath(String ident, String ending) {
|
---|
| 342 | return new File(dir, getUniqueFilename(ident) + "." + ending);
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | /**
|
---|
| 346 | * Gets file path for ident
|
---|
[5881] | 347 | * @param ident cache identifier
|
---|
| 348 | * @return file structure
|
---|
[1610] | 349 | */
|
---|
| 350 | private File getPath(String ident) {
|
---|
| 351 | return new File(dir, getUniqueFilename(ident));
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | /**
|
---|
| 355 | * Checks whether a given file is expired
|
---|
[5881] | 356 | * @param file file description structure
|
---|
| 357 | * @return expired state
|
---|
[1610] | 358 | */
|
---|
| 359 | private boolean isExpired(File file) {
|
---|
| 360 | if(CacheFiles.EXPIRE_NEVER == this.expire)
|
---|
| 361 | return false;
|
---|
[6102] | 362 | return (file.lastModified() < (System.currentTimeMillis() - expire*1000));
|
---|
[1610] | 363 | }
|
---|
| 364 | }
|
---|