KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > driver > TestFileOps


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: TestFileOps.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.driver;
25
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.FileNotFoundException JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.io.RandomAccessFile JavaDoc;
34 import java.net.URL JavaDoc;
35 import java.util.ArrayList JavaDoc;
36
37 /**
38  * Class of static methods to do various file related operations.
39  */

40 public class TestFileOps {
41     /**
42      * Seconds to sleep to allow for system time to advance. This takes
43      * makes sure that we sleep longer than the resolution of the clock
44      * used for files.
45      * FIXME: Find out the scoop on the resoultion, not sure this is really
46      * need, it might be some other bug that was causing the problem.
47      */

48     public static final double TOUCH_DELAY = 1.0;
49
50     /** Ensure that a file's directory exists. */
51     public static void ensureFileDir(File JavaDoc file) {
52         File JavaDoc dir = file.getParentFile();
53         if (dir != null) {
54             dir.mkdirs();
55         }
56     }
57
58     /** Copy a file another file, creating dir if needed */
59     public static void copyFile(File JavaDoc srcFile,
60                                 File JavaDoc destFile) {
61         try {
62             ensureFileDir(destFile);
63             InputStream JavaDoc src = new FileInputStream JavaDoc(srcFile);
64             try {
65                 OutputStream JavaDoc dest = new FileOutputStream JavaDoc(destFile);
66                 try {
67                     int byteCnt;
68                     byte[] buffer = new byte[8*1024];
69                     while ((byteCnt = src.read(buffer, 0, buffer.length)) >= 0) {
70                          dest.write(buffer, 0, byteCnt);
71                     }
72                 } finally {
73                     dest.close();
74                 }
75             } finally {
76                 src.close();
77             }
78         } catch (IOException JavaDoc except) {
79             throw new TestException("copy of \"" + srcFile + "\" to \""
80                                     + destFile + "\" failed", except);
81         }
82     }
83
84     /** Copy a file to a directory, creating dir if needed */
85     public static void copyFileToDir(File JavaDoc srcFile,
86                                      File JavaDoc destDir) {
87         destDir.mkdirs();
88         File JavaDoc destFile = new File JavaDoc(destDir, srcFile.getName());
89         copyFile(srcFile, destFile);
90     }
91
92     /** Copy a list of files to a directory, creating dir if needed */
93     public static void copyFilesToDir(File JavaDoc[] srcFiles,
94                                       File JavaDoc destDir) {
95         for (int idx = 0; idx < srcFiles.length; idx++) {
96             copyFileToDir(srcFiles[idx], destDir);
97         }
98     }
99
100     /** Copy a list of files to a directory, creating dir if needed */
101     public static void copyFilesToDir(String JavaDoc[] srcFiles,
102                                       File JavaDoc destDir) {
103         for (int idx = 0; idx < srcFiles.length; idx++) {
104             copyFileToDir(new File JavaDoc(srcFiles[idx]), destDir);
105         }
106     }
107
108     /** Copy a list of files to a directory, creating dir if needed */
109     public static void copyFilesToDir(ArrayList JavaDoc srcFiles,
110                                       File JavaDoc destDir) {
111         for (int idx = 0; idx < srcFiles.size(); idx++) {
112             Object JavaDoc file = srcFiles.get(idx);
113             if (file instanceof File JavaDoc) {
114                 copyFileToDir((File JavaDoc)file, destDir);
115             } else {
116                 copyFileToDir(new File JavaDoc((String JavaDoc)file), destDir);
117             }
118         }
119     }
120
121     /**
122      * Delay for the specified number of seconds.
123      */

124     public static void sleep(double secs) {
125         try {
126             Thread.sleep((long)(secs*1000));
127         } catch (InterruptedException JavaDoc except) {
128             throw new TestError(except);
129         }
130     }
131
132     /**
133      * Touch a file by reading and rewriting one byte.
134      */

135     public static void touchFile(File JavaDoc file) {
136         // Wait, just to let file clock get ahead.
137
sleep(TOUCH_DELAY);
138
139         try {
140             RandomAccessFile JavaDoc raFile = new RandomAccessFile JavaDoc(file, "rw");
141             int fileByte = raFile.read();
142             if (fileByte < 0) {
143                 throw new TestError("unexpected EOF on " + file);
144             }
145             raFile.seek(0);
146             raFile.write(fileByte);
147             raFile.close();
148         } catch (FileNotFoundException JavaDoc except) {
149             throw new TestError("error touching " + file, except);
150         } catch (IOException JavaDoc except) {
151             throw new TestError("error touching " + file, except);
152         }
153     }
154
155
156     /** Create a URL from a directory File */
157     static public URL JavaDoc dirToUrl(File JavaDoc file) {
158         if (!file.isDirectory()) {
159             throw new TestError("file for URL not a directory: \"" + file
160                                 + "\"");
161         }
162         try {
163             return new URL JavaDoc("file", null, -1, file.getCanonicalPath() + "/");
164         } catch (IOException JavaDoc except) {
165             throw new TestError("can't construct URL from File", except);
166         }
167     }
168
169     /** Create a URL from a file File */
170     static public URL JavaDoc fileToUrl(File JavaDoc file) {
171         try {
172             // URL class loader requires a `/' at the end if it's directory
173
String JavaDoc path = file.getAbsolutePath();
174             if (!(path.endsWith(".zip") || path.endsWith(".jar"))) {
175                 path += '/';
176             }
177             return new URL JavaDoc("file", null, -1, path);
178         } catch (IOException JavaDoc except) {
179             throw new TestError("can't construct URL from File", except);
180         }
181     }
182
183     /**
184      * Get the file extension.
185      */

186     public static String JavaDoc getFileExt(String JavaDoc fname) {
187         int dotIdx = fname.lastIndexOf('.');
188         if (dotIdx < 0) {
189             return null;
190         } else {
191             return fname.substring(dotIdx+1);
192         }
193     }
194
195     /**
196      * Get the file extension.
197      */

198     public static String JavaDoc getFileExt(File JavaDoc file) {
199         return getFileExt(file.getPath());
200     }
201 }
202
Popular Tags