1 package net.suberic.util.cache; 2 import java.io.*; 3 4 10 public class FileSizedCacheEntry extends SizedCacheEntry { 11 12 protected Object cachedValue; 15 16 protected long lastAccessedTime; 18 19 protected long size = 0; 21 22 26 public FileSizedCacheEntry(Object value, boolean create, String filename) { 27 File f = new File(filename); 28 try { 29 saveValue(f, value); 30 } catch (Exception e) { 31 } 32 cachedValue = f; 33 34 touchEntry(); 35 } 36 37 40 public Object getCachedValue() { 41 Object o = null; 42 try { 43 o = loadValue(); 44 } catch (IOException ioe) { 45 } 46 47 return o; 48 } 49 50 55 public Object loadValue() throws IOException { 56 File f = getCacheFile(); 57 ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f)); 58 size = ois.available(); 59 Object returnValue = null; 60 try { 61 returnValue = ois.readObject(); 62 } catch (ClassNotFoundException cnfe) { 63 throw new IOException("Class not found: " + cnfe); 64 } 65 ois.close(); 66 return returnValue; 67 } 68 69 74 public void saveValue(File f, Object value) throws IOException { 75 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f)); 76 oos.writeObject(value); 77 oos.close(); 78 } 79 80 87 public boolean removeFromCache() { 88 if (cachedValue instanceof File) { 89 ((File) cachedValue).delete(); 90 return true; 91 } else 92 return false; 93 } 94 95 98 public void touchEntry() { 99 lastAccessedTime = System.currentTimeMillis(); 100 getCacheFile().setLastModified(lastAccessedTime); 101 } 102 103 106 public long getSize() { 107 return size; 108 } 109 110 113 public long getLastAccessedTime() { 114 return lastAccessedTime; 115 } 116 117 120 public File getCacheFile() { 121 return (File) cachedValue; 122 } 123 124 127 public boolean equals(Object o) { 128 if (o != null) { 129 Object testValue = null; 130 if (o instanceof SizedCacheEntry) { 131 testValue = ((SizedCacheEntry)o).getCachedValue(); 132 } else { 133 testValue = o; 134 } 135 return o.equals(cachedValue); 136 } 137 138 return (cachedValue == null); 139 } 140 } 141 142 | Popular Tags |