@Test public void testLRUFileCacheKeyCheck() throws IOException { LRUFileCache cache = new LRUFileCache(dir, 100, 9999, 1); byte[] buf = new byte[30]; cache.putFile("10623fe9-1646-40f0-83d6-bda1ba43d305@Llxny6HoJ1_C8CJGJ1rLpRnpWJri4qAS", new ByteArrayInputStream(buf)); try { cache.putFile("..", new ByteArrayInputStream(buf)); } catch (IllegalArgumentException e) { assertTrue(e.getMessage(), e.getMessage().contains("Invalid key")); } try { cache.putFile("../myfile", new ByteArrayInputStream(buf)); } catch (IllegalArgumentException e) { assertTrue(e.getMessage(), e.getMessage().contains("Invalid key")); } try { cache.putFile("foo/bar", new ByteArrayInputStream(buf)); } catch (IllegalArgumentException e) { assertTrue(e.getMessage(), e.getMessage().contains("Invalid key")); } }
@Override public File putFile(String key, InputStream in) throws IOException { File tmp; try { checkKey(key); Path path = dir.resolve(key); if (Files.exists(path)) { recordAccess(path); return path.toFile(); } tmp = getTempFile(); try (FileOutputStream out = new FileOutputStream(tmp)) { IOUtils.copy(in, out); } } finally { in.close(); } return putFile(key, tmp); }
LRUFileCache implements FileCache { @Override public File putFile(String key, InputStream in) throws IOException { File tmp; try { checkKey(key); Path path = dir.resolve(key); if (Files.exists(path)) { recordAccess(path); return path.toFile(); } tmp = getTempFile(); try (FileOutputStream out = new FileOutputStream(tmp)) { IOUtils.copy(in, out); } } finally { in.close(); } return putFile(key, tmp); } }
LRUFileCache implements FileCache { @Override public File putFile(String key, InputStream in) throws IOException { File tmp; try { checkKey(key); Path path = dir.resolve(key); if (Files.exists(path)) { recordAccess(path); return path.toFile(); } tmp = getTempFile(); try (FileOutputStream out = new FileOutputStream(tmp)) { IOUtils.copy(in, out); } } finally { in.close(); } return putFile(key, tmp); } LRUFileCache(File dir, long maxSize, long maxCount, long minAge); }
LRUFileCache implements FileCache { @Override public File putFile(String key, InputStream in) throws IOException { File tmp; try { checkKey(key); Path path = dir.resolve(key); if (Files.exists(path)) { recordAccess(path); return path.toFile(); } tmp = getTempFile(); try (FileOutputStream out = new FileOutputStream(tmp)) { IOUtils.copy(in, out); } } finally { in.close(); } return putFile(key, tmp); } LRUFileCache(File dir, long maxSize, long maxCount, long minAge); void setClearOldEntriesIntervalMillis(long millis); @Override long getSize(); @Override int getNumberOfItems(); @Override void clear(); @Override File getTempFile(); @Override File putFile(String key, InputStream in); @Override File putFile(String key, File file); @Override File getFile(String key); }
LRUFileCache implements FileCache { @Override public File putFile(String key, InputStream in) throws IOException { File tmp; try { checkKey(key); Path path = dir.resolve(key); if (Files.exists(path)) { recordAccess(path); return path.toFile(); } tmp = getTempFile(); try (FileOutputStream out = new FileOutputStream(tmp)) { IOUtils.copy(in, out); } } finally { in.close(); } return putFile(key, tmp); } LRUFileCache(File dir, long maxSize, long maxCount, long minAge); void setClearOldEntriesIntervalMillis(long millis); @Override long getSize(); @Override int getNumberOfItems(); @Override void clear(); @Override File getTempFile(); @Override File putFile(String key, InputStream in); @Override File putFile(String key, File file); @Override File getFile(String key); static final Pattern SIMPLE_ASCII; static final long CLEAR_OLD_ENTRIES_INTERVAL_MILLIS_DEFAULT; }