001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.lib;
032:
033: import java.io.File;
034: import java.io.FileDescriptor;
035: import java.io.FileInputStream;
036: import java.io.FileOutputStream;
037: import java.io.IOException;
038: import java.util.Random;
039:
040: import org.hsqldb.lib.java.JavaSystem;
041:
042: /**
043: * A collection of static file management methods.<p>
044: * Also implements the default FileAccess method
045: *
046: * @author fredt@users
047: * @author boucherb@users
048: * @author Ocke Janssen oj@openoffice.org
049: * @version 1.8.0
050: * @since 1.7.2
051: */
052: public class FileUtil implements FileAccess {
053:
054: private static FileUtil fileUtil;
055:
056: /** Creates a new instance of FileUtil */
057: FileUtil() {
058: }
059:
060: public static FileUtil getDefaultInstance() {
061:
062: if (fileUtil == null) {
063: fileUtil = new FileUtil();
064: }
065:
066: return fileUtil;
067: }
068:
069: public boolean isStreamElement(java.lang.String elementName) {
070: return (new File(elementName)).exists();
071: }
072:
073: public java.io.InputStream openInputStreamElement(
074: java.lang.String streamName) throws java.io.IOException {
075:
076: try {
077: return new FileInputStream(new File(streamName));
078: } catch (Throwable e) {
079: throw toIOException(e);
080: }
081: }
082:
083: public void createParentDirs(java.lang.String filename) {
084: makeParentDirectories(new File(filename));
085: }
086:
087: public void removeElement(java.lang.String filename) {
088:
089: if (isStreamElement(filename)) {
090: delete(filename);
091: }
092: }
093:
094: public void renameElement(java.lang.String oldName,
095: java.lang.String newName) {
096: renameOverwrite(oldName, newName);
097: }
098:
099: public java.io.OutputStream openOutputStreamElement(
100: java.lang.String streamName) throws java.io.IOException {
101: return new FileOutputStream(new File(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 = (new File("A"))
114: .equals(new File("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 = (new File(
119: "/")).getPath().endsWith(File.separator);
120:
121: // for JDK 1.1 createTempFile
122: static final Random random = new Random(System.currentTimeMillis());
123:
124: /**
125: * Delete the named file
126: */
127: public static void delete(String filename) {
128: (new File(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 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 filename) {
154: return (new File(filename)).exists();
155: }
156:
157: public static boolean exists(String fileName, boolean resource,
158: Class cla) {
159:
160: if (fileName == null || fileName.length() == 0) {
161: return false;
162: }
163:
164: return resource ? null != cla.getResource(fileName) : FileUtil
165: .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 oldname, String newname) {
176:
177: delete(newname);
178:
179: if (exists(oldname)) {
180: File file = new File(oldname);
181:
182: file.renameTo(new File(newname));
183: }
184: }
185:
186: public static IOException toIOException(Throwable e) {
187:
188: if (e instanceof IOException) {
189: return (IOException) e;
190: } else {
191: return new IOException(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 absolutePath(String path) {
202: return (new File(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 canonicalFile(File f) throws IOException {
213: return new File(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 canonicalFile(String path) throws IOException {
224: return new File(new File(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 canonicalPath(File f) throws IOException {
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 canonicalPath(String path) throws IOException {
246: return new File(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 canonicalOrAbsolutePath(String path) {
258:
259: try {
260: return canonicalPath(path);
261: } catch (Exception e) {
262: return absolutePath(path);
263: }
264: }
265:
266: public static void makeParentDirectories(File f) {
267:
268: String parent = f.getParent();
269:
270: if (parent != null) {
271: new File(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(parent).mkdirs();
283: }
284: }
285: }
286:
287: public class FileSync implements FileAccess.FileSync {
288:
289: FileDescriptor outDescriptor;
290:
291: FileSync(FileOutputStream os) throws java.io.IOException {
292: outDescriptor = os.getFD();
293: }
294:
295: public void sync() throws java.io.IOException {
296: outDescriptor.sync();
297: }
298: }
299:
300: public FileAccess.FileSync getFileSync(java.io.OutputStream os)
301: throws java.io.IOException {
302: return new FileSync((FileOutputStream) os);
303: }
304: }
|