KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > transaction > util > FileHelper


1 /*
2  * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//transaction/src/java/org/apache/commons/transaction/util/FileHelper.java,v 1.2 2004/11/19 17:42:10 ozeigermann Exp $
3 <<<<<<< .mine
4  * $Revision: 1.2 $
5  * $Date: 2005-02-26 14:16:14 +0100 (Sa, 26 Feb 2005) $
6 =======
7  * $Revision$
8  * $Date: 2005-02-26 14:16:14 +0100 (Sa, 26 Feb 2005) $
9 >>>>>>> .r168169
10  *
11  * ====================================================================
12  *
13  * Copyright 2004 The Apache Software Foundation
14  *
15  * Licensed under the Apache License, Version 2.0 (the "License");
16  * you may not use this file except in compliance with the License.
17  * You may obtain a copy of the License at
18  *
19  * http://www.apache.org/licenses/LICENSE-2.0
20  *
21  * Unless required by applicable law or agreed to in writing, software
22  * distributed under the License is distributed on an "AS IS" BASIS,
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24  * See the License for the specific language governing permissions and
25  * limitations under the License.
26  *
27  */

28
29 package org.apache.commons.transaction.util;
30
31 import java.io.File JavaDoc;
32 import java.io.FileInputStream JavaDoc;
33 import java.io.FileOutputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.io.OutputStream JavaDoc;
37
38 /**
39  * Helper methods for file manipulation.
40  * All methods are <em>thread safe</em>.
41  *
42  * @version $Revision$
43  */

44 public final class FileHelper {
45
46     private static int BUF_SIZE = 50000;
47     private static byte[] BUF = new byte[BUF_SIZE];
48
49     /**
50      * Deletes a file specified by a path.
51      *
52      * @param path path of file to be deleted
53      * @return <code>true</code> if file has been deleted, <code>false</code> otherwise
54      */

55     public static boolean deleteFile(String JavaDoc path) {
56         File JavaDoc file = new File JavaDoc(path);
57         return file.delete();
58     }
59
60     /**
61      * Checks if a file specified by a path exits.
62      *
63      * @param path path of file to be checked
64      * @return <code>true</code> if file exists, <code>false</code> otherwise
65      */

66     public static boolean fileExists(String JavaDoc path) {
67         File JavaDoc file = new File JavaDoc(path);
68         return file.exists();
69     }
70
71     /**
72      * Creates a file specified by a path. All necessary directories will be created.
73      *
74      * @param path path of file to be created
75      * @return <code>true</code> if file has been created, <code>false</code> if the file already exists
76      * @throws IOException
77      * If an I/O error occurred
78      */

79     public static boolean createFile(String JavaDoc path) throws IOException JavaDoc {
80         File JavaDoc file = new File JavaDoc(path);
81         if (file.isDirectory()) {
82             return file.mkdirs();
83         } else {
84             File JavaDoc dir = file.getParentFile();
85             // do not check if this worked, as it may also return false, when all neccessary dirs are present
86
dir.mkdirs();
87             return file.createNewFile();
88         }
89     }
90
91     /**
92      * Removes a file. If the specified file is a directory all contained files will
93      * be removed recursively as well.
94      *
95      * @param toRemove file to be removed
96      */

97     public static void removeRec(File JavaDoc toRemove) {
98         if (toRemove.isDirectory()) {
99             File JavaDoc fileList[] = toRemove.listFiles();
100             for (int a = 0; a < fileList.length; a++) {
101                 removeRec(fileList[a]);
102             }
103         }
104         toRemove.delete();
105     }
106
107     /**
108      * Moves one directory or file to another. Existing files will be replaced.
109      *
110      * @param source file to move from
111      * @param target file to move to
112      * @throws IOException if an I/O error occurs (may result in partially done work)
113      */

114     public static void moveRec(File JavaDoc source, File JavaDoc target) throws IOException JavaDoc {
115         byte[] sharedBuffer = new byte[BUF_SIZE];
116         moveRec(source, target, sharedBuffer);
117     }
118
119     static void moveRec(File JavaDoc source, File JavaDoc target, byte[] sharedBuffer) throws IOException JavaDoc {
120         if (source.isDirectory()) {
121             if (!target.exists()) {
122                 target.mkdirs();
123             }
124             if (target.isDirectory()) {
125
126                 File JavaDoc[] files = source.listFiles();
127                 for (int i = 0; i < files.length; i++) {
128                     File JavaDoc file = files[i];
129                     File JavaDoc targetFile = new File JavaDoc(target, file.getName());
130                     if (file.isFile()) {
131                         if (targetFile.exists()) {
132                             targetFile.delete();
133                         }
134                         if (!file.renameTo(targetFile)) {
135                             copy(file, targetFile, sharedBuffer);
136                             file.delete();
137                         }
138                     } else {
139                         targetFile.mkdirs();
140                         moveRec(file, targetFile);
141                     }
142                 }
143                 source.delete();
144             }
145         } else {
146             if (!target.isDirectory()) {
147                 copy(source, target, sharedBuffer);
148                 source.delete();
149             }
150         }
151     }
152
153     /**
154      * Copies one directory or file to another. Existing files will be replaced.
155      *
156      * @param source directory or file to copy from
157      * @param target directory or file to copy to
158      * @throws IOException if an I/O error occurs (may result in partially done work)
159      */

160     public static void copyRec(File JavaDoc source, File JavaDoc target) throws IOException JavaDoc {
161         byte[] sharedBuffer = new byte[BUF_SIZE];
162         copyRec(source, target, sharedBuffer);
163     }
164
165     static void copyRec(File JavaDoc source, File JavaDoc target, byte[] sharedBuffer) throws IOException JavaDoc {
166         if (source.isDirectory()) {
167             if (!target.exists()) {
168                 target.mkdirs();
169             }
170             if (target.isDirectory()) {
171
172                 File JavaDoc[] files = source.listFiles();
173                 for (int i = 0; i < files.length; i++) {
174                     File JavaDoc file = files[i];
175                     File JavaDoc targetFile = new File JavaDoc(target, file.getName());
176                     if (file.isFile()) {
177                         if (targetFile.exists()) {
178                             targetFile.delete();
179                         }
180                         copy(file, targetFile, sharedBuffer);
181                     } else {
182                         targetFile.mkdirs();
183                         copyRec(file, targetFile);
184                     }
185                 }
186             }
187         } else {
188             if (!target.isDirectory()) {
189                 if (!target.exists()) {
190                     target.getParentFile().mkdirs();
191                     target.createNewFile();
192                 }
193                 copy(source, target, sharedBuffer);
194             }
195         }
196     }
197
198     /**
199      * Copies one file to another using {@link #copy(InputStream, OutputStream)}.
200      *
201      * @param input
202      * source file
203      * @param output
204      * destination file
205      * @return the number of bytes copied
206      * @throws IOException
207      * if an I/O error occurs (may result in partially done work)
208      * @see #copy(InputStream, OutputStream)
209      */

210     public static long copy(File JavaDoc input, File JavaDoc output) throws IOException JavaDoc {
211         FileInputStream JavaDoc in = null;
212         try {
213             in = new FileInputStream JavaDoc(input);
214             return copy(in, output);
215         } finally {
216             if (in != null) {
217                 try {
218                     in.close();
219                 } catch (IOException JavaDoc e) {
220                 }
221             }
222         }
223     }
224
225     /**
226      * Copies one file to another using the supplied buffer.
227      *
228      * @param input source file
229      * @param output destination file
230      * @param copyBuffer buffer used for copying
231      * @return the number of bytes copied
232      * @throws IOException if an I/O error occurs (may result in partially done work)
233      * @see #copy(InputStream, OutputStream)
234      */

235     public static long copy(File JavaDoc input, File JavaDoc output, byte[] copyBuffer) throws IOException JavaDoc {
236         FileInputStream JavaDoc in = null;
237         FileOutputStream JavaDoc out = null;
238         try {
239             in = new FileInputStream JavaDoc(input);
240             out = new FileOutputStream JavaDoc(output);
241             return copy(in, out, copyBuffer);
242         } finally {
243             if (in != null) {
244                 try {
245                     in.close();
246                 } catch (IOException JavaDoc e) {
247                 }
248             }
249             if (out != null) {
250                 try {
251                     out.close();
252                 } catch (IOException JavaDoc e) {
253                 }
254             }
255         }
256     }
257
258     /**
259      * Copies an <code>InputStream</code> to a file using {@link #copy(InputStream, OutputStream)}.
260      *
261      * @param in stream to copy from
262      * @param outputFile file to copy to
263      * @return the number of bytes copied
264      * @throws IOException if an I/O error occurs (may result in partially done work)
265      * @see #copy(InputStream, OutputStream)
266      */

267     public static long copy(InputStream JavaDoc in, File JavaDoc outputFile) throws IOException JavaDoc {
268         FileOutputStream JavaDoc out = null;
269         try {
270             out = new FileOutputStream JavaDoc(outputFile);
271             return copy(in, out);
272         } finally {
273             if (out != null) {
274                 try {
275                     out.close();
276                 } catch (IOException JavaDoc e) {
277                 }
278             }
279         }
280     }
281
282     /**
283      * Copies an <code>InputStream</code> to an <code>OutputStream</code> using a local internal buffer for performance.
284      * Compared to {@link #globalBufferCopy(InputStream, OutputStream)} this method allows for better
285      * concurrency, but each time it is called generates a buffer which will be garbage.
286      *
287      * @param in stream to copy from
288      * @param out stream to copy to
289      * @return the number of bytes copied
290      * @throws IOException if an I/O error occurs (may result in partially done work)
291      * @see #globalBufferCopy(InputStream, OutputStream)
292      */

293     public static long copy(InputStream JavaDoc in, OutputStream JavaDoc out) throws IOException JavaDoc {
294         // we need a buffer of our own, so no one else interferes
295
byte[] buf = new byte[BUF_SIZE];
296         return copy(in, out, buf);
297     }
298
299     /**
300      * Copies an <code>InputStream</code> to an <code>OutputStream</code> using a global internal buffer for performance.
301      * Compared to {@link #copy(InputStream, OutputStream)} this method generated no garbage,
302      * but decreases concurrency.
303      *
304      * @param in stream to copy from
305      * @param out stream to copy to
306      * @return the number of bytes copied
307      * @throws IOException if an I/O error occurs (may result in partially done work)
308      * @see #copy(InputStream, OutputStream)
309      */

310     public static long globalBufferCopy(InputStream JavaDoc in, OutputStream JavaDoc out) throws IOException JavaDoc {
311         synchronized (BUF) {
312             return copy(in, out, BUF);
313         }
314     }
315
316     /**
317      * Copies an <code>InputStream</code> to an <code>OutputStream</code> using the specified buffer.
318      *
319      * @param in stream to copy from
320      * @param out stream to copy to
321      * @param copyBuffer buffer used for copying
322      * @return the number of bytes copied
323      * @throws IOException if an I/O error occurs (may result in partially done work)
324      * @see #globalBufferCopy(InputStream, OutputStream)
325      * @see #copy(InputStream, OutputStream)
326      */

327     public static long copy(InputStream JavaDoc in, OutputStream JavaDoc out, byte[] copyBuffer) throws IOException JavaDoc {
328         long bytesCopied = 0;
329         int read = -1;
330
331         while ((read = in.read(copyBuffer, 0, copyBuffer.length)) != -1) {
332             out.write(copyBuffer, 0, read);
333             bytesCopied += read;
334         }
335         return bytesCopied;
336     }
337 }
338
Popular Tags