001: /*
002: ** Copyright (c) 1998 by Timothy Gerard Endres
003: ** <mailto:time@ice.com> <http://www.ice.com>
004: **
005: ** This program is free software.
006: **
007: ** You may redistribute it and/or modify it under the terms of the GNU
008: ** General Public License as published by the Free Software Foundation.
009: ** Version 2 of the license should be included with this distribution in
010: ** the file LICENSE, as well as License.html. If the license is not
011: ** included with this distribution, you may find a copy at the FSF web
012: ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
013: ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
014: **
015: ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
016: ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
017: ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
018: ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
019: ** REDISTRIBUTION OF THIS SOFTWARE.
020: **
021: */
022:
023: package com.ice.util;
024:
025: import java.io.*;
026: import java.util.Vector;
027: import com.ice.util.UserProperties;
028:
029: public class TempFileManager extends Object {
030: private static int tempDocCounter = 0;
031:
032: private static String temnpDirName = null;
033: private static String tempFilePrefix = null;
034: private static String tempFileSuffix = null;
035:
036: private static Vector tempFiles = new Vector();
037:
038: public static void initialize(String dirName, String prefix,
039: String suffix) {
040: TempFileManager.temnpDirName = dirName;
041: TempFileManager.tempFilePrefix = prefix;
042: TempFileManager.tempFileSuffix = suffix;
043: }
044:
045: public static void setTemporaryDirectory(String dirName) {
046: TempFileManager.temnpDirName = dirName;
047: }
048:
049: public static void setFilenamePrefix(String prefix) {
050: TempFileManager.tempFilePrefix = prefix;
051: }
052:
053: public static void setFilenameSuffix(String suffix) {
054: TempFileManager.tempFileSuffix = suffix;
055: }
056:
057: public static void addTemporaryFile(String filename) {
058: TempFileManager.tempFiles.addElement(filename);
059: }
060:
061: public static String getTemporaryDirectory() {
062: return TempFileManager.temnpDirName;
063: }
064:
065: public static String getTemporaryFilename() {
066: TempFileManager.tempDocCounter++;
067:
068: return TempFileManager
069: .getTemporaryFilename(TempFileManager.tempFileSuffix);
070: }
071:
072: public static String getTemporaryFilename(String suffix) {
073: TempFileManager.tempDocCounter++;
074:
075: String tempFileName = TempFileManager.temnpDirName
076: + File.separator + TempFileManager.tempFilePrefix + "-"
077: + TempFileManager.tempDocCounter + suffix;
078:
079: return tempFileName;
080: }
081:
082: public static void clearTemporaryFiles() {
083: int count = 0;
084: int numFiles = TempFileManager.tempFiles.size();
085:
086: for (int idx = 0; idx < numFiles; ++idx) {
087: String fileName = (String) TempFileManager.tempFiles
088: .elementAt(idx);
089:
090: File f = new File(fileName);
091:
092: if (f.exists() && f.isFile()) {
093: f.delete();
094: count++;
095: }
096: }
097:
098: System.err
099: .println("Deleted " + count + " temporary documents.");
100: }
101:
102: public static void writeTemporaryFile(InputStream source,
103: String tempFileName) throws IOException {
104: FileOutputStream out = new FileOutputStream(tempFileName);
105:
106: byte[] buf = new byte[32 * 1024];
107: for (;;) {
108: int cnt = source.read(buf, 0, buf.length);
109: if (cnt == -1)
110: break;
111:
112: out.write(buf, 0, cnt);
113: }
114:
115: out.close();
116:
117: TempFileManager.addTemporaryFile(tempFileName);
118: }
119:
120: }
|