KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > lib > FileUtil


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.lib;
33
34 import java.io.File JavaDoc;
35 import java.io.FileDescriptor JavaDoc;
36 import java.io.FileInputStream JavaDoc;
37 import java.io.FileOutputStream JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.util.Random JavaDoc;
40
41 import org.hsqldb.lib.java.JavaSystem;
42
43 /**
44  * A collection of static file management methods.<p>
45  * Also implements the default FileAccess method
46  *
47  * @author fredt@users
48  * @author boucherb@users
49  * @author Ocke Janssen oj@openoffice.org
50  * @version 1.8.0
51  * @since 1.7.2
52  */

53 public class FileUtil implements FileAccess {
54
55     private static FileUtil fileUtil;
56
57     /** Creates a new instance of FileUtil */
58     FileUtil() {}
59
60     public static FileUtil getDefaultInstance() {
61
62         if (fileUtil == null) {
63             fileUtil = new FileUtil();
64         }
65
66         return fileUtil;
67     }
68
69     public boolean isStreamElement(java.lang.String JavaDoc elementName) {
70         return (new File JavaDoc(elementName)).exists();
71     }
72
73     public java.io.InputStream JavaDoc openInputStreamElement(
74             java.lang.String JavaDoc streamName) throws java.io.IOException JavaDoc {
75
76         try {
77             return new FileInputStream JavaDoc(new File JavaDoc(streamName));
78         } catch (Throwable JavaDoc e) {
79             throw toIOException(e);
80         }
81     }
82
83     public void createParentDirs(java.lang.String JavaDoc filename) {
84         makeParentDirectories(new File JavaDoc(filename));
85     }
86
87     public void removeElement(java.lang.String JavaDoc filename) {
88
89         if (isStreamElement(filename)) {
90             delete(filename);
91         }
92     }
93
94     public void renameElement(java.lang.String JavaDoc oldName,
95                               java.lang.String JavaDoc newName) {
96         renameOverwrite(oldName, newName);
97     }
98
99     public java.io.OutputStream JavaDoc openOutputStreamElement(
100             java.lang.String JavaDoc streamName) throws java.io.IOException JavaDoc {
101         return new FileOutputStream JavaDoc(new File JavaDoc(streamName));
102     }
103
104     // end of FileAccess implementation
105
// a new File("...")'s path is not canonicalized, only resolved
106
// and normalized (e.g. redundant separator chars removed),
107
// so as of JDK 1.4.2, this is a valid test for case insensitivity,
108
// at least when it is assumed that we are dealing with a configuration
109
// that only needs to consider the host platform's native file system,
110
// even if, unlike for File.getCanonicalPath(), (new File("a")).exists() or
111
// (new File("A")).exits(), regardless of the hosting system's
112
// file path case sensitivity policy.
113
public static final boolean fsIsIgnoreCase =
114         (new File JavaDoc("A")).equals(new File JavaDoc("a"));
115
116     // posix separator normalized to File.separator?
117
// CHECKME: is this true for every file system under Java?
118
public static final boolean fsNormalizesPosixSeparator =
119         (new File JavaDoc("/")).getPath().endsWith(File.separator);
120
121     // for JDK 1.1 createTempFile
122
static final Random JavaDoc random = new Random JavaDoc(System.currentTimeMillis());
123
124     /**
125      * Delete the named file
126      */

127     public static void delete(String JavaDoc filename) {
128         (new File JavaDoc(filename)).delete();
129     }
130
131     /**
132      * Requests, in a JDK 1.1 compliant way, that the file or directory denoted
133      * by the given abstract pathname be deleted when the virtual machine
134      * terminates. <p>
135      *
136      * Deletion will be attempted only for JDK 1.2 and greater runtime
137      * environments and only upon normal termination of the virtual
138      * machine, as defined by the Java Language Specification. <p>
139      *
140      * Once deletion has been sucessfully requested, it is not possible to
141      * cancel the request. This method should therefore be used with care. <p>
142      *
143      * @param f the abstract pathname of the file be deleted when the virtual
144      * machine terminates
145      */

146     public static void deleteOnExit(File JavaDoc f) {
147         JavaSystem.deleteOnExit(f);
148     }
149
150     /**
151      * Return true or false based on whether the named file exists.
152      */

153     public static boolean exists(String JavaDoc filename) {
154         return (new File JavaDoc(filename)).exists();
155     }
156
157     public static boolean exists(String JavaDoc fileName, boolean resource,
158                                  Class JavaDoc cla) {
159
160         if (fileName == null || fileName.length() == 0) {
161             return false;
162         }
163
164         return resource ? null != cla.getResource(fileName)
165                         : FileUtil.exists(fileName);
166     }
167
168     /**
169      * Rename the file with oldname to newname. If a file with newname already
170      * exists, it is deleted before the renaming operation proceeds.
171      *
172      * If a file with oldname does not exist, no file will exist after the
173      * operation.
174      */

175     public static void renameOverwrite(String JavaDoc oldname, String JavaDoc newname) {
176
177         delete(newname);
178
179         if (exists(oldname)) {
180             File JavaDoc file = new File JavaDoc(oldname);
181
182             file.renameTo(new File JavaDoc(newname));
183         }
184     }
185
186     public static IOException JavaDoc toIOException(Throwable JavaDoc e) {
187
188         if (e instanceof IOException JavaDoc) {
189             return (IOException JavaDoc) e;
190         } else {
191             return new IOException JavaDoc(e.toString());
192         }
193     }
194
195     /**
196      * Retrieves the absolute path, given some path specification.
197      *
198      * @param path the path for which to retrieve the absolute path
199      * @return the absolute path
200      */

201     public static String JavaDoc absolutePath(String JavaDoc path) {
202         return (new File JavaDoc(path)).getAbsolutePath();
203     }
204
205     /**
206      * Retrieves the canonical file for the given file, in a
207      * JDK 1.1 complaint way.
208      *
209      * @param f the File for which to retrieve the absolute File
210      * @return the canonical File
211      */

212     public static File JavaDoc canonicalFile(File JavaDoc f) throws IOException JavaDoc {
213         return new File JavaDoc(f.getCanonicalPath());
214     }
215
216     /**
217      * Retrieves the canonical file for the given path, in a
218      * JDK 1.1 complaint way.
219      *
220      * @param path the path for which to retrieve the canonical File
221      * @return the canonical File
222      */

223     public static File JavaDoc canonicalFile(String JavaDoc path) throws IOException JavaDoc {
224         return new File JavaDoc(new File JavaDoc(path).getCanonicalPath());
225     }
226
227     /**
228      * Retrieves the canonical path for the given File, in a
229      * JDK 1.1 complaint way.
230      *
231      * @param f the File for which to retrieve the canonical path
232      * @return the canonical path
233      */

234     public static String JavaDoc canonicalPath(File JavaDoc f) throws IOException JavaDoc {
235         return f.getCanonicalPath();
236     }
237
238     /**
239      * Retrieves the canonical path for the given path, in a
240      * JDK 1.1 complaint way.
241      *
242      * @param path the path for which to retrieve the canonical path
243      * @return the canonical path
244      */

245     public static String JavaDoc canonicalPath(String JavaDoc path) throws IOException JavaDoc {
246         return new File JavaDoc(path).getCanonicalPath();
247     }
248
249     /**
250      * Retrieves the canonical path for the given path, or the absolute
251      * path if attemting to retrieve the canonical path fails.
252      *
253      * @param path the path for which to retrieve the canonical or
254      * absolute path
255      * @return the canonical or absolute path
256      */

257     public static String JavaDoc canonicalOrAbsolutePath(String JavaDoc path) {
258
259         try {
260             return canonicalPath(path);
261         } catch (Exception JavaDoc e) {
262             return absolutePath(path);
263         }
264     }
265
266     public static void makeParentDirectories(File JavaDoc f) {
267
268         String JavaDoc parent = f.getParent();
269
270         if (parent != null) {
271             new File JavaDoc(parent).mkdirs();
272         } else {
273
274             // workaround for jdk 1.1 bug (returns null when there is a parent)
275
parent = f.getPath();
276
277             int index = parent.lastIndexOf('/');
278
279             if (index > 0) {
280                 parent = parent.substring(0, index);
281
282                 new File JavaDoc(parent).mkdirs();
283             }
284         }
285     }
286
287     public class FileSync implements FileAccess.FileSync {
288
289         FileDescriptor JavaDoc outDescriptor;
290
291         FileSync(FileOutputStream JavaDoc os) throws java.io.IOException JavaDoc {
292             outDescriptor = os.getFD();
293         }
294
295         public void sync() throws java.io.IOException JavaDoc {
296             outDescriptor.sync();
297         }
298     }
299
300     public FileAccess.FileSync getFileSync(java.io.OutputStream JavaDoc os)
301     throws java.io.IOException JavaDoc {
302         return new FileSync((FileOutputStream JavaDoc) os);
303     }
304 }
305
Popular Tags