001: /*
002: * Cache.java
003: *
004: * Copyright (C) 2000-2003 Peter Graves
005: * $Id: Cache.java,v 1.2 2003/06/29 00:19:34 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.io.BufferedReader;
025: import java.io.BufferedWriter;
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.io.InputStreamReader;
029: import java.io.OutputStream;
030: import java.io.OutputStreamWriter;
031: import java.net.HttpURLConnection;
032: import java.net.URL;
033: import java.util.Vector;
034:
035: public final class Cache {
036: private static final File cacheDir = File.getInstance(Directories
037: .getEditorDirectory(), "cache");
038:
039: private static Cache cache;
040:
041: private File catalogFile;
042: private Vector catalog;
043:
044: private Cache() {
045: }
046:
047: public static Cache getCache() {
048: if (cache == null) {
049: cache = new Cache();
050: if (!cache.initialize())
051: cache = null;
052: }
053: return cache;
054: }
055:
056: // Currently this just deletes everything in the cache directory.
057: // Called from Editor.maybeExit.
058: public static void cleanup() {
059: if (cacheDir.isDirectory()) {
060: String[] files = cacheDir.list();
061: for (int i = files.length - 1; i >= 0; i--) {
062: File file = File.getInstance(cacheDir, files[i]);
063: file.delete();
064: }
065: }
066: }
067:
068: private boolean initialize() {
069: if (!cacheDir.isDirectory())
070: cacheDir.mkdirs();
071: if (!cacheDir.isDirectory())
072: return false;
073: catalogFile = File.getInstance(cacheDir, "catalog");
074: catalog = loadCatalog();
075: return true;
076: }
077:
078: public File get(String netPath) {
079: for (int i = catalog.size() - 1; i >= 0; i--) {
080: StringPair pair = (StringPair) catalog.get(i);
081: if (pair.second.equals(netPath))
082: return File.getInstance(cacheDir, pair.first);
083: }
084: return null;
085: }
086:
087: public File put(String netPath) {
088: File file = null;
089: try {
090: URL url = new URL(netPath);
091: HttpURLConnection connection = (HttpURLConnection) url
092: .openConnection();
093: InputStream in = connection.getInputStream();
094: if (in != null) {
095: file = Utilities.getTempFile(cacheDir);
096: OutputStream out = file.getOutputStream();
097: byte[] buf = new byte[4096];
098: int bytesRead;
099: while ((bytesRead = in.read(buf)) > 0)
100: out.write(buf, 0, bytesRead);
101: out.close();
102: in.close();
103: }
104: } catch (IOException e) {
105: Log.error(e);
106: if (file.exists())
107: file.delete();
108: file = null;
109: }
110: if (file != null) {
111: catalog.add(new StringPair(file.getName(), netPath));
112: saveCatalog();
113: }
114: return file;
115: }
116:
117: private Vector loadCatalog() {
118: Vector v = new Vector();
119: if (catalogFile.exists()) {
120: try {
121: BufferedReader reader = new BufferedReader(
122: new InputStreamReader(catalogFile
123: .getInputStream()));
124: String s;
125: while ((s = reader.readLine()) != null) {
126: int index = s.indexOf(' ');
127: if (index >= 0)
128: v.add(new StringPair(s.substring(0, index), s
129: .substring(index + 1)));
130: }
131: } catch (IOException e) {
132: Log.error(e);
133: }
134: }
135: return v;
136: }
137:
138: private void saveCatalog() {
139: try {
140: BufferedWriter writer = new BufferedWriter(
141: new OutputStreamWriter(catalogFile
142: .getOutputStream()));
143: for (int i = 0; i < catalog.size(); i++) {
144: StringPair pair = (StringPair) catalog.get(i);
145: writer.write(pair.first);
146: writer.write(' ');
147: writer.write(pair.second);
148: writer.newLine();
149: }
150: writer.flush();
151: writer.close();
152: } catch (IOException e) {
153: Log.error(e);
154: }
155: }
156: }
|