KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > store > util > FileHelper


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

23
24 package org.apache.slide.store.util;
25
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.OutputStream JavaDoc;
32
33 /**
34  * Helper methods for file manipulation.
35  * All methods are <em>thread safe</em>.
36  *
37  */

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

49     public static boolean deleteFile(String JavaDoc path) {
50         File JavaDoc file = new File JavaDoc(path);
51         return file.delete();
52     }
53
54     /**
55      * Checks if a file specified by a path exits.
56      *
57      * @param path path of file to be checked
58      * @return <code>true</code> if file exists, <code>false</code> otherwise
59      */

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

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

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

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

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

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

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

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

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

300     public static long globalBufferCopy(InputStream JavaDoc in, OutputStream JavaDoc out) throws IOException JavaDoc {
301         synchronized (BUF) {
302             return copy(in, out, BUF);
303         }
304     }
305
306     /**
307      * Copies an <code>InputStream</code> to an <code>OutputStream</code> using the specified buffer.
308      *
309      * @param in stream to copy from
310      * @param out stream to copy to
311      * @param copyBuffer buffer used for copying
312      * @return the number of bytes copied
313      * @throws IOException if an I/O error occurs (may result in partially done work)
314      * @see #globalBufferCopy(InputStream, OutputStream)
315      * @see #copy(InputStream, OutputStream)
316      */

317     public static long copy(InputStream JavaDoc in, OutputStream JavaDoc out, byte[] copyBuffer) throws IOException JavaDoc {
318         long bytesCopied = 0;
319         int read = -1;
320
321         while ((read = in.read(copyBuffer, 0, copyBuffer.length)) != -1) {
322             out.write(copyBuffer, 0, read);
323             bytesCopied += read;
324         }
325         return bytesCopied;
326     }
327 }
328
Popular Tags