KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > util > cache > FileSizedCacheEntry


1 package net.suberic.util.cache;
2 import java.io.*;
3
4 /**
5  * This represents a cached object. It stores the object itself, plus
6  * the size and last accessed time.
7  *
8  * This implementation uses files to store the information in the cache.
9  */

10 public class FileSizedCacheEntry extends SizedCacheEntry {
11
12   // the stored object itself. note that this could be a reference to
13
// another object, or a source from which the object may be accessed.
14
protected Object JavaDoc cachedValue;
15
16   // the last accessed time
17
protected long lastAccessedTime;
18
19   // the size that the cachedValue occupies in the cache.
20
protected long size = 0;
21
22   /**
23    * Creates a new FileSizedCacheEntry containing value which has been most
24    * recently accessed now.
25    */

26   public FileSizedCacheEntry(Object JavaDoc value, boolean create, String JavaDoc filename) {
27     File f = new File(filename);
28     try {
29       saveValue(f, value);
30     } catch (Exception JavaDoc e) {
31     }
32     cachedValue = f;
33
34     touchEntry();
35   }
36
37   /**
38    * This gets the cached value. Implementations may vary for this.
39    */

40   public Object JavaDoc getCachedValue() {
41     Object JavaDoc o = null;
42     try {
43       o = loadValue();
44     } catch (IOException ioe) {
45     }
46
47     return o;
48   }
49
50   /**
51    * Loads the given value from the source file.
52    *
53    * Note that this should also set the size.
54    */

55   public Object JavaDoc loadValue() throws IOException {
56     File f = getCacheFile();
57     ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
58     size = ois.available();
59     Object JavaDoc returnValue = null;
60     try {
61       returnValue = ois.readObject();
62     } catch (ClassNotFoundException JavaDoc cnfe) {
63       throw new IOException("Class not found: " + cnfe);
64     }
65     ois.close();
66     return returnValue;
67   }
68
69   /**
70    * Saves the given value to the source file.
71    *
72    * Note that this should also set the size.
73    */

74   public void saveValue(File f, Object JavaDoc value) throws IOException {
75     ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
76     oos.writeObject(value);
77     oos.close();
78   }
79
80   /**
81    * Deletes this entry. Should be called in order to clean up entries
82    * for which simple removal from memory is insufficient.
83    *
84    * The default implementation does nothing; subclasses should override
85    * this method if cleanup is required.
86    */

87   public boolean removeFromCache() {
88     if (cachedValue instanceof File) {
89       ((File) cachedValue).delete();
90       return true;
91     } else
92       return false;
93   }
94
95   /**
96    * Touches the SizedCacheEntry, making its last accessed time now.
97    */

98   public void touchEntry() {
99     lastAccessedTime = System.currentTimeMillis();
100     getCacheFile().setLastModified(lastAccessedTime);
101   }
102
103   /**
104    * Gets the size of this SizedCacheEntry.
105    */

106   public long getSize() {
107     return size;
108   }
109
110   /**
111    * Gets the last accessed time for this entry.
112    */

113   public long getLastAccessedTime() {
114     return lastAccessedTime;
115   }
116
117   /**
118    * Gets the File object for the cache.
119    */

120   public File getCacheFile() {
121     return (File) cachedValue;
122   }
123
124   /**
125    * Compares the underlying value for equality.
126    */

127   public boolean equals(Object JavaDoc o) {
128     if (o != null) {
129       Object JavaDoc 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