001: /*
002: * Copyright (C) 2001, 2002 Robert MacGrogan
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: *
019: * $Archive: SourceJammer$
020: * $FileName: TempDirectoryManager.java$
021: * $FileID: 4319$
022: *
023: * Last change:
024: * $AuthorName: Rob MacGrogan$
025: * $Date: 4/23/03 5:23 PM$
026: * $Comment: Replaced GPL header with LGPL header.$
027: *
028: * $KeyWordsOff: $
029: */
030:
031: package org.sourcejammer.util;
032:
033: /**
034: * Title: SourceJammer 1.2
035: * Description:
036: * Copyright: Copyright (c) 2002
037: * Company: SourceJammer Project
038: * @author Robert MacGrogan
039: * @version 1.0
040: */
041:
042: import java.io.File;
043: import java.io.*;
044: import org.sourcejammer.util.AppConfig;
045: import org.sourcejammer.util.Counter;
046: import org.sourcejammer.util.FileUtil;
047:
048: public class TempDirectoryManager {
049:
050: private static final String TEMP_DIRECTORY_NAME = "_temp";
051: private static final String COUNTER_FILE_NAME = "namer";
052: private static File tempDir = new File(AppConfig.getInstance()
053: .getConfigFilePath(), TEMP_DIRECTORY_NAME);
054:
055: private TempDirectoryManager() {
056: }
057:
058: /**
059: * Remove all files in temp directory other than namer.
060: */
061: public static void clearAllTempFiles() {
062: if (tempDir.exists()) {
063: File[] tempFiles = tempDir.listFiles();
064: for (int i = 0; i < tempFiles.length; i++) {
065: if (!tempFiles[i].getName().equals(COUNTER_FILE_NAME)) {
066: tempFiles[i].delete();
067: }
068: }//end for
069: }
070: }
071:
072: /**
073: * Remove all files in temp directory (except namer) older than specified
074: * Date.
075: */
076: public static void removeTempFiles(java.util.Date olderThanDate) {
077: if (tempDir.exists()) {
078: FileFilter filter = new FileDateFilter(olderThanDate,
079: FileDateFilter.FILTER_LESS_THAN_EQUAL);
080: File removeFiles[] = tempDir.listFiles(filter);
081: for (int i = 0; i < removeFiles.length; i++) {
082: if (!removeFiles[i].getName().equals(COUNTER_FILE_NAME)) {
083: removeFiles[i].delete();
084: }
085: }//end for
086: }
087: }
088:
089: /**
090: * Creates the temp directory if it does not exist.
091: */
092: private static synchronized void makeTempDirIfNeeded()
093: throws IOException {
094: if (!tempDir.exists()) {
095: tempDir.mkdir();
096: Counter namer = new Counter();
097: File flNamer = new File(tempDir, COUNTER_FILE_NAME);
098: FileUtil.writeObjectToFileSys(namer, flNamer);
099: }
100: }
101:
102: /**
103: * Gets the next unique ID from the Counter object.
104: */
105: public static synchronized long getNextID() throws IOException {
106: if (!tempDir.exists()) {
107: makeTempDirIfNeeded();
108: }
109: File flCounter = new File(tempDir, COUNTER_FILE_NAME);
110: if (!flCounter.exists()) {
111: makeCounterIfNeeded(flCounter);
112: }
113: Counter c = (Counter) FileUtil.readObjectFromFileSys(flCounter);
114: long lId = c.getNextValue();
115: FileUtil.writeObjectToFileSys(c, flCounter);
116: return lId;
117: }
118:
119: public static File getNewTempFile() throws IOException {
120: long nextID = getNextID();
121: File nextFile = getFileById(nextID);
122: return nextFile;
123: }
124:
125: public static byte[] getTempFileBytesById(long id)
126: throws IOException {
127: File fl = getTempFileById(id);
128: byte[] file = FileUtil.readBytesFromFileSys(fl);
129: return file;
130: }
131:
132: public static long makeNewTempFile(byte[] by) throws IOException {
133: long nextID = getNextID();
134: File flTemp = getFileById(nextID);
135: FileUtil.writeBytesToFileSys(by, flTemp);
136: return nextID;
137: }
138:
139: public static long streamFromFileToTemp(File fl) throws IOException {
140: if (!fl.exists()) {
141: throw new IOException("The file does not exist.");
142: }
143: if (fl.isDirectory()) {
144: throw new IOException("The file is a directory.");
145: }
146:
147: long nextID = getNextID();
148: File flTemp = getFileById(nextID);
149:
150: FileUtil.streamFileToFile(fl, flTemp);
151: return nextID;
152: }
153:
154: public static void streamFromTempToFile(long tempFileID, File fl)
155: throws IOException {
156: if (fl.isDirectory()) {
157: throw new IOException("The file is a directory.");
158: }
159: File flTemp = getTempFileById(tempFileID);
160: if (!flTemp.exists()) {
161: throw new IOException("Temp file does not exist.");
162: }
163: FileUtil.streamFileToFile(flTemp, fl);
164: }
165:
166: public static File getTempFileById(long id) throws IOException {
167: File fl = getFileById(id);
168: if (!fl.exists()) {
169: throw new IOException(
170: "The specified temp file does not exist.");
171: }
172: return fl;
173: }
174:
175: public static File getNewTempFile(long id) throws IOException {
176: File fl = getFileById(id);
177: return fl;
178: }
179:
180: public static void deleteTempFile(long id) throws IOException {
181: File fl = getFileById(id);
182: fl.delete();
183: }
184:
185: private static File getFileById(long id) {
186: File fl = new File(tempDir, Long.toString(id));
187: return fl;
188: }
189:
190: /**
191: * Creates and stores the Counter object if it does not already exist
192: * in the temp directory.
193: */
194: private static synchronized void makeCounterIfNeeded(File flCounter)
195: throws IOException {
196: if (!flCounter.exists()) {
197: Counter c = new Counter();
198: FileUtil.writeObjectToFileSys(c, flCounter);
199: }
200: }
201:
202: public static void main(String[] args) {
203: try {
204: String sSource = args[0];
205: String sTarget = args[1];
206: long lStart = new java.util.Date().getTime();
207: byte[] by = FileUtil.readBytesFromFileSys(sSource);
208: long lFinish = new java.util.Date().getTime();
209: long lTime = lFinish - lStart;
210: System.out.println("Read bytes took " + lTime + " ms.");
211:
212: lStart = new java.util.Date().getTime();
213: FileUtil.writeBytesToFileSys(by, sTarget);
214: lFinish = new java.util.Date().getTime();
215: lTime = lFinish - lStart;
216: System.out.println("Write bytes took " + lTime + " ms.");
217:
218: } catch (Throwable thr) {
219: thr.printStackTrace();
220: }
221: }
222:
223: }
|