1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| package org.apache.xindice.util; |
21 |
| |
22 |
| import java.io.File; |
23 |
| import java.io.FileInputStream; |
24 |
| import java.io.IOException; |
25 |
| import java.util.Map; |
26 |
| import java.util.WeakHashMap; |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| public class FileCache { |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| private final Map cache = new WeakHashMap(); |
39 |
| |
40 |
19
| public FileCache() {
|
41 |
| } |
42 |
| |
43 |
0
| public final boolean isInCache(File file) {
|
44 |
0
| return (cache.get(file) != null);
|
45 |
| } |
46 |
| |
47 |
0
| public final boolean isInCache(String name) {
|
48 |
0
| return (cache.get(new File(name)) != null);
|
49 |
| } |
50 |
| |
51 |
0
| public final boolean isModified(String name) {
|
52 |
0
| return isModified(new File(name));
|
53 |
| } |
54 |
| |
55 |
0
| public final boolean isModified(File file) {
|
56 |
0
| FileCacheInfo finfo = (FileCacheInfo) cache.get(file);
|
57 |
0
| return !file.exists()
|
58 |
| || finfo == null |
59 |
| || (file.lastModified() != finfo.lastModified); |
60 |
| } |
61 |
| |
62 |
0
| public final byte[] getFile(String name) throws IOException {
|
63 |
0
| return getFile(new File(name));
|
64 |
| } |
65 |
| |
66 |
1961
| public final byte[] getFile(File file) throws IOException {
|
67 |
1961
| if (!file.exists()) {
|
68 |
3
| return null;
|
69 |
| } |
70 |
| |
71 |
1958
| FileCacheInfo finfo = (FileCacheInfo) cache.get(file);
|
72 |
1958
| long lastmod = file.lastModified();
|
73 |
1958
| if (finfo == null || finfo.lastModified != lastmod) {
|
74 |
1957
| FileInputStream fis = new FileInputStream(file);
|
75 |
1957
| byte[] content = new byte[fis.available()];
|
76 |
1957
| fis.read(content);
|
77 |
1957
| fis.close();
|
78 |
1957
| finfo = new FileCacheInfo(file, lastmod, content);
|
79 |
1957
| cache.put(file, finfo);
|
80 |
1957
| return content;
|
81 |
| } else { |
82 |
1
| return finfo.content;
|
83 |
| } |
84 |
| } |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| private class FileCacheInfo { |
90 |
| public File file; |
91 |
| public long lastModified = 0; |
92 |
| public byte[] content; |
93 |
| |
94 |
1957
| public FileCacheInfo(File file, long lastModified, byte[] content) {
|
95 |
1957
| this.file = file;
|
96 |
1957
| this.lastModified = lastModified;
|
97 |
1957
| this.content = content;
|
98 |
| } |
99 |
| } |
100 |
| } |