KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > Cache


1 /*
2  * Cache.java
3  *
4  * Copyright (C) 2000-2003 Peter Graves
5  * $Id: Cache.java,v 1.2 2003/06/29 00:19:34 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.io.BufferedReader JavaDoc;
25 import java.io.BufferedWriter JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.InputStreamReader JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.io.OutputStreamWriter JavaDoc;
31 import java.net.HttpURLConnection JavaDoc;
32 import java.net.URL JavaDoc;
33 import java.util.Vector JavaDoc;
34
35 public final class Cache
36 {
37     private static final File cacheDir =
38         File.getInstance(Directories.getEditorDirectory(), "cache");
39
40     private static Cache cache;
41
42     private File catalogFile;
43     private Vector JavaDoc catalog;
44
45     private Cache()
46     {
47     }
48
49     public static Cache getCache()
50     {
51         if (cache == null) {
52             cache = new Cache();
53             if (!cache.initialize())
54                 cache = null;
55         }
56         return cache;
57     }
58
59     // Currently this just deletes everything in the cache directory.
60
// Called from Editor.maybeExit.
61
public static void cleanup()
62     {
63         if (cacheDir.isDirectory()) {
64             String JavaDoc[] files = cacheDir.list();
65             for (int i = files.length-1; i >= 0; i--) {
66                 File file = File.getInstance(cacheDir, files[i]);
67                 file.delete();
68             }
69         }
70     }
71
72     private boolean initialize()
73     {
74         if (!cacheDir.isDirectory())
75             cacheDir.mkdirs();
76         if (!cacheDir.isDirectory())
77             return false;
78         catalogFile = File.getInstance(cacheDir, "catalog");
79         catalog = loadCatalog();
80         return true;
81     }
82
83     public File get(String JavaDoc netPath)
84     {
85         for (int i = catalog.size()-1; i >= 0; i--) {
86             StringPair pair = (StringPair) catalog.get(i);
87             if (pair.second.equals(netPath))
88                 return File.getInstance(cacheDir, pair.first);
89         }
90         return null;
91     }
92
93     public File put(String JavaDoc netPath)
94     {
95         File file = null;
96         try {
97             URL JavaDoc url = new URL JavaDoc(netPath);
98             HttpURLConnection JavaDoc connection =
99                 (HttpURLConnection JavaDoc) url.openConnection();
100             InputStream JavaDoc in = connection.getInputStream();
101             if (in != null) {
102                 file = Utilities.getTempFile(cacheDir);
103                 OutputStream JavaDoc out = file.getOutputStream();
104                 byte[] buf = new byte[4096];
105                 int bytesRead;
106                 while ((bytesRead = in.read(buf)) > 0)
107                     out.write(buf, 0, bytesRead);
108                 out.close();
109                 in.close();
110             }
111         }
112         catch (IOException JavaDoc e) {
113             Log.error(e);
114             if (file.exists())
115                 file.delete();
116             file = null;
117         }
118         if (file != null) {
119             catalog.add(new StringPair(file.getName(), netPath));
120             saveCatalog();
121         }
122         return file;
123     }
124
125     private Vector JavaDoc loadCatalog()
126     {
127         Vector JavaDoc v = new Vector JavaDoc();
128         if (catalogFile.exists()) {
129             try {
130                 BufferedReader JavaDoc reader = new BufferedReader JavaDoc(
131                     new InputStreamReader JavaDoc(catalogFile.getInputStream()));
132                 String JavaDoc s;
133                 while ((s = reader.readLine()) != null) {
134                     int index = s.indexOf(' ');
135                     if (index >= 0)
136                         v.add(new StringPair(s.substring(0, index), s.substring(index+1)));
137                 }
138             }
139             catch (IOException JavaDoc e) {
140                 Log.error(e);
141             }
142         }
143         return v;
144     }
145
146     private void saveCatalog()
147     {
148         try {
149             BufferedWriter JavaDoc writer = new BufferedWriter JavaDoc(
150                 new OutputStreamWriter JavaDoc(catalogFile.getOutputStream()));
151             for (int i = 0; i < catalog.size(); i++) {
152                 StringPair pair = (StringPair) catalog.get(i);
153                 writer.write(pair.first);
154                 writer.write(' ');
155                 writer.write(pair.second);
156                 writer.newLine();
157             }
158             writer.flush();
159             writer.close();
160         }
161         catch (IOException JavaDoc e) {
162             Log.error(e);
163         }
164     }
165 }
166
Popular Tags