/** debug */ static void make(DiskCache2 dc, String filename) throws IOException { File want = dc.getCacheFile(filename); System.out.println("make=" + want.getPath() + "; exists = " + want.exists()); if (!want.exists()) want.createNewFile(); System.out.println(" canRead= " + want.canRead() + " canWrite = " + canWrite(want) + " lastMod = " + new Date(want.lastModified())); System.out.println(" original=" + EscapeStrings.urlDecode(filename)); }
/** * Get a File in the cache, corresponding to the fileLocation. * File may or may not exist. * If fileLocation has "/" in it, and cachePathPolicy == NestedDirectory, the * nested directories will be created. * * @param fileLocation logical file location * @return physical File in the cache. */ public File getCacheFile(String fileLocation) { if (!alwaysUseCache) { File f = new File(fileLocation); if (canWrite(f)) return f; } File f = new File(makeCachePath(fileLocation)); //if (f.exists()) // f.setLastModified( System.currentTimeMillis()); if (cachePathPolicy == CachePathPolicy.NestedDirectory) { File dir = f.getParentFile(); dir.mkdirs(); } return f; }
/** * Get a File in the cache, corresponding to the fileLocation. * File may or may not exist. * If fileLocation has "/" in it, and cachePathPolicy == NestedDirectory, the * nested directories will be created. * * @param fileLocation logical file location * @return physical File in the cache. */ public File getCacheFile(String fileLocation) { if (neverUseCache) return null; if (!alwaysUseCache) { File f = new File(fileLocation); if (canWrite(f)) return f; } File f = new File(makeCachePath(fileLocation)); //if (f.exists()) // f.setLastModified( System.currentTimeMillis()); if (cachePathPolicy == CachePathPolicy.NestedDirectory) { File dir = f.getParentFile(); boolean ret = dir.mkdirs(); if (!ret) cacheLog.warn("Error creating dir: " + dir); } return f; }
/** * Get the named File. * If exists or isWritable, return it. * Otherwise get corresponding file in the cache directory. * * If fileLocation has "/" in it, and cachePathPolicy == NestedDirectory, the * nested directories will be created. * * @param fileLocation logical file location * @return physical File as named, or in the cache. */ public File getFile(String fileLocation) { if (!alwaysUseCache) { File f = new File(fileLocation); if (f.exists()) return f; if (canWrite(f)) return f; } if (neverUseCache) { throw new IllegalStateException("neverUseCache=true, but directory is not writeable ="+fileLocation); } File f = new File(makeCachePath(fileLocation)); if (cachePathPolicy == CachePathPolicy.NestedDirectory) { File dir = f.getParentFile(); if (!dir.exists() && !dir.mkdirs()) cacheLog.warn("Cant create directories for file "+dir.getPath()); } return f; }
/** * Get the named File. * If exists or isWritable, return it. * Otherwise get corresponding file in the cache directory. * * If fileLocation has "/" in it, and cachePathPolicy == NestedDirectory, the * nested directories will be created. * * @param fileLocation logical file location * @return physical File as named, or in the cache. */ public File getFile(String fileLocation) { if (!alwaysUseCache) { File f = new File(fileLocation); if (f.exists()) return f; if (canWrite(f)) return f; } if (neverUseCache) { throw new IllegalStateException("neverUseCache=true, but file does not exist and directory is not writeable ="+fileLocation); } File f = new File(makeCachePath(fileLocation)); if (cachePathPolicy == CachePathPolicy.NestedDirectory) { File dir = f.getParentFile(); if (!dir.exists() && !dir.mkdirs()) cacheLog.warn("Cant create directories for file "+dir.getPath()); } return f; }
/** * Get the named File. * If exists or isWritable, return it. * Otherwise get corresponding file in the cache directory. * * If fileLocation has "/" in it, and cachePathPolicy == NestedDirectory, the * nested directories will be created. * * @param fileLocation logical file location * @return physical File as named, or in the cache. */ public File getFile(String fileLocation) { if (!alwaysUseCache) { File f = new File(fileLocation); if (f.exists()) return f; if (canWrite(f)) return f; } if (neverUseCache) { throw new IllegalStateException("neverUseCache=true, but file does not exist and directory is not writeable ="+fileLocation); } File f = new File(makeCachePath(fileLocation)); if (cachePathPolicy == CachePathPolicy.NestedDirectory) { File dir = f.getParentFile(); if (!dir.exists() && !dir.mkdirs()) cacheLog.warn("Cant create directories for file "+dir.getPath()); } return f; }
/** * Get a File in the cache, corresponding to the fileLocation. * File may or may not exist. * If fileLocation has "/" in it, and cachePathPolicy == NestedDirectory, the * nested directories will be created. * * @param fileLocation logical file location * @return physical File in the cache. */ public File getCacheFile(String fileLocation) { if (neverUseCache) return null; if (!alwaysUseCache) { File f = new File(fileLocation); if (canWrite(f)) return f; } File f = new File(makeCachePath(fileLocation)); //if (f.exists()) // f.setLastModified( System.currentTimeMillis()); if (cachePathPolicy == CachePathPolicy.NestedDirectory) { File dir = f.getParentFile(); if (!dir.exists()) { boolean ret = dir.mkdirs(); if (!ret) cacheLog.warn("Error creating dir: " + dir); } } return f; }
@Test public void canWriteFile() throws IOException { File file = Files.createTempFile(nearest, "temp", null).toFile(); try { Assert.assertTrue(file.exists()); Assert.assertTrue(DiskCache2.canWrite(file)); } finally { file.delete(); } }
@Test public void cantWriteNonExistentFileWithNonExistentParent() { File file = Paths.get(nearest.toString(), "A", "B", "C", "non-existent-file.txt").toFile(); Assert.assertFalse(file.exists()); Assert.assertFalse(file.getParentFile().exists()); Assert.assertFalse(DiskCache2.canWrite(file)); } }
@Test public void canWriteNonExistentFileWithExistentParent() { File file = Paths.get(nearest.toString(), "non-existent-file.txt").toFile(); Assert.assertFalse(file.exists()); Assert.assertTrue(DiskCache2.canWrite(file)); }
@Test public void cantWriteFile() throws IOException { File file = Files.createTempFile(nearest, "temp", null).toFile(); try { Assert.assertTrue(file.exists()); Assert.assertTrue(file.setWritable(false)); Assert.assertFalse(DiskCache2.canWrite(file)); } finally { file.delete(); } }
@Test public void canWriteDir() { File file = nearest.toFile(); Assert.assertTrue(file.exists()); Assert.assertTrue(DiskCache2.canWrite(file)); }