01: //The contents of this file are subject to the Mozilla Public License Version 1.1
02: //(the "License"); you may not use this file except in compliance with the
03: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
04: //
05: //Software distributed under the License is distributed on an "AS IS" basis,
06: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
07: //for the specific language governing rights and
08: //limitations under the License.
09: //
10: //The Original Code is "The Columba Project"
11: //
12: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14: //
15: //All Rights Reserved.
16: package org.columba.core.util;
17:
18: import java.io.File;
19:
20: import org.columba.core.config.DefaultConfigDirectory;
21: import org.columba.core.io.DiskIO;
22:
23: /**
24: * Factory to create temporary Files on the file storage.
25: */
26: public final class TempFileStore {
27: private static File tempDir;
28:
29: static {
30: File configDir = DefaultConfigDirectory.getInstance()
31: .getCurrentPath();
32:
33: tempDir = new File(configDir, "tmp");
34: DiskIO.emptyDirectory(tempDir);
35: DiskIO.ensureDirectory(tempDir);
36: }
37:
38: /**
39: * Utility class should not have a public constructor.
40: */
41: private TempFileStore() {
42: }
43:
44: /**
45: * Returns a String with spaces replaced by underscore.
46: * @param s in string
47: * @return incoming string without spaces.
48: */
49: private static String replaceWhiteSpaces(String s) {
50: return s.replace(' ', '_');
51: }
52:
53: /**
54: * Create a temporary file on the temporary folder storage.
55: * @return a File. File suffix is tmp.
56: */
57: public static File createTempFile() {
58: return createTempFileWithSuffix("tmp");
59: }
60:
61: /**
62: * Create a temporary file with the specified filename.
63: * @param name the name of the file.
64: * @return a File.
65: */
66: public static File createTempFile(String name) {
67: return createTemporaryFile(replaceWhiteSpaces(name));
68: }
69:
70: /**
71: * Create a temporary file with the specified file name suffix.
72: * @param suffix the suffix for the file.
73: * @return a File.
74: */
75: public static File createTempFileWithSuffix(String suffix) {
76: return createTemporaryFile("columba"
77: + System.currentTimeMillis() + "." + suffix);
78: }
79:
80: /**
81: * Creates a temporary file that is removed when the program exits.
82: * @param name the name of the file.
83: * @return a File.
84: */
85: private static File createTemporaryFile(String name) {
86: File newFile = new File(tempDir, name);
87: newFile.deleteOnExit();
88: return newFile;
89: }
90: }
|