KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > io > StreamCache


1 package org.columba.core.io;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.FileNotFoundException JavaDoc;
6 import java.io.FileOutputStream JavaDoc;
7 import java.io.IOException JavaDoc;
8 import java.io.InputStream JavaDoc;
9 import java.io.ObjectInputStream JavaDoc;
10 import java.io.ObjectOutputStream JavaDoc;
11 import java.io.Serializable JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collections JavaDoc;
14 import java.util.Comparator JavaDoc;
15 import java.util.Date JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 public class StreamCache {
20
21     public static final long DEFAULT_SIZE = 10 * 1024 * 1024;// Byte
22

23     public static final String JavaDoc FIFO_FILE = ".fifo";
24
25     private File JavaDoc dir;
26
27     private List JavaDoc<CacheEntry> fifo;
28
29     private long actSize;
30
31     private long maxSize;
32
33     public StreamCache(File JavaDoc directory) {
34         this(directory, DEFAULT_SIZE);
35     }
36
37     public StreamCache(File JavaDoc directory, long maxSize) {
38         dir = directory;
39         if (!dir.exists()) {
40             if (!dir.mkdirs()) {
41                 throw new RuntimeException JavaDoc(dir.toString()
42                         + " could not be created!");
43             }
44         } else {
45             // try to restore from previous session
46
try {
47                 restore();
48             } catch (IOException JavaDoc e) {
49                 // Never mind
50
}
51         }
52         actSize = 0;
53
54         // If the fifo could not be restored initialize it
55
if (fifo == null) {
56             fifo = new ArrayList JavaDoc<CacheEntry>(100);
57         }
58
59         this.maxSize = maxSize;
60     }
61
62     public InputStream JavaDoc passiveAdd(Object JavaDoc key, InputStream JavaDoc in)
63             throws IOException JavaDoc {
64         File JavaDoc streamFile = new File JavaDoc(dir.getAbsoluteFile() + File.separator
65                 + key.toString() + ".cache");
66         return new StreamCacheCopyStream(in, key, streamFile, this);
67     }
68
69     public void activeAdd(Object JavaDoc key, InputStream JavaDoc in) throws IOException JavaDoc {
70         File JavaDoc streamFile = new File JavaDoc(dir.getAbsoluteFile() + File.separator
71                 + key.toString() + ".cache");
72         StreamUtils.streamCopy(in, new FileOutputStream JavaDoc(streamFile));
73         add(key, streamFile);
74     }
75
76     void add(Object JavaDoc key, File JavaDoc out) {
77         fifo.add(new CacheEntry(key, out));
78
79         actSize += out.length();
80
81         Comparator JavaDoc _myComperator = new Comparator JavaDoc() {
82
83             public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
84                 Date JavaDoc a = ((CacheEntry) arg0).lastAccess;
85                 Date JavaDoc b = ((CacheEntry) arg1).lastAccess;
86
87                 if (a.before(b))
88                     return 1;
89                 if (a.after(b))
90                     return -1;
91                 return 0;
92             }
93
94         };
95         Collections.sort(fifo, _myComperator);
96
97         ensureMaxSize();
98     }
99
100     private void ensureMaxSize() {
101         while (actSize > maxSize) {
102             CacheEntry entry = fifo.remove(fifo.size() - 1);
103             actSize -= entry.file.length();
104             entry.file.delete();
105         }
106     }
107
108     public InputStream JavaDoc get(Object JavaDoc key) {
109         Iterator JavaDoc it = fifo.iterator();
110
111         while (it.hasNext()) {
112             CacheEntry c = (CacheEntry) it.next();
113             if (c.key.equals(key)) {
114                 try {
115                     return c.createStream();
116                 } catch (FileNotFoundException JavaDoc e) {
117                     it.remove();
118                     actSize -= c.file.length();
119                 }
120             }
121         }
122
123         return null;
124     }
125
126     public long getActSize() {
127         return actSize;
128     }
129
130     public void clear() {
131         Iterator JavaDoc it = fifo.iterator();
132
133         while (it.hasNext()) {
134             CacheEntry c = (CacheEntry) it.next();
135             if (!c.file.delete()) {
136                 // Try again after shutdown
137
c.file.deleteOnExit();
138             }
139             it.remove();
140         }
141
142         actSize = 0;
143     }
144
145     public void persist() throws IOException JavaDoc {
146         File JavaDoc file = new File JavaDoc(dir.getAbsoluteFile() + File.separator + FIFO_FILE);
147         ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(new FileOutputStream JavaDoc(
148                 file));
149         out.writeObject(fifo);
150         out.close();
151     }
152
153     public void restore() throws IOException JavaDoc {
154         File JavaDoc file = new File JavaDoc(dir.getAbsoluteFile() + File.separator + FIFO_FILE);
155         try {
156             fifo = (List JavaDoc<CacheEntry>) new ObjectInputStream JavaDoc(new FileInputStream JavaDoc(file))
157                     .readObject();
158         } catch (ClassNotFoundException JavaDoc e) {
159             // ignore this yet
160
}
161     }
162
163     public long getMaxSize() {
164         return maxSize;
165     }
166
167     public void setMaxSize(long maxSize) {
168         this.maxSize = maxSize;
169         ensureMaxSize();
170     }
171 }
172
173 @SuppressWarnings JavaDoc({"serial","serial"}) class CacheEntry implements Serializable JavaDoc {
174     Date JavaDoc lastAccess;
175
176     Object JavaDoc key;
177
178     File JavaDoc file;
179
180     public CacheEntry(Object JavaDoc key, File JavaDoc file) {
181         this.key = key;
182         this.file = file;
183         this.lastAccess = new Date JavaDoc();
184     }
185
186     public InputStream JavaDoc createStream() throws FileNotFoundException JavaDoc {
187         this.lastAccess = new Date JavaDoc();
188         return new FileInputStream JavaDoc(file);
189     }
190
191     /**
192      * @return Returns the file.
193      */

194     public File JavaDoc getFile() {
195         return file;
196     }
197
198     /**
199      * @param file
200      * The file to set.
201      */

202     public void setFile(File JavaDoc file) {
203         this.file = file;
204     }
205
206     /**
207      * @return Returns the key.
208      */

209     public Object JavaDoc getKey() {
210         return key;
211     }
212
213     /**
214      * @param key
215      * The key to set.
216      */

217     public void setKey(Object JavaDoc key) {
218         this.key = key;
219     }
220
221     /**
222      * @return Returns the lastAccess.
223      */

224     public Date JavaDoc getLastAccess() {
225         return lastAccess;
226     }
227
228     /**
229      * @param lastAccess
230      * The lastAccess to set.
231      */

232     public void setLastAccess(Date JavaDoc lastAccess) {
233         this.lastAccess = lastAccess;
234     }
235 }
Popular Tags