KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > util > FileUtils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.subversion.util;
21
22 import java.io.*;
23 import org.openide.ErrorManager;
24 import org.openide.filesystems.FileUtil;
25
26 /**
27  *
28  * @author pkuzel
29  */

30 public class FileUtils {
31
32     /**
33      * Copies the specified sourceFile to the specified targetFile.
34      */

35     public static void copyFile(File sourceFile, File targetFile) throws IOException {
36         if (sourceFile == null || targetFile == null) {
37             throw new NullPointerException JavaDoc("sourceFile and targetFile must not be null"); // NOI18N
38
}
39
40         InputStream inputStream = null;
41         try {
42             inputStream = createInputStream(sourceFile);
43             copyStreamToFile(inputStream, targetFile);
44         } finally {
45             if (inputStream != null) {
46                 try {
47                     inputStream.close();
48                 }
49                 catch (IOException ex) {
50                     // ignore
51
}
52             }
53         }
54     }
55
56     public static void copyDirFiles(File sourceDir, File targetDir) {
57         copyDirFiles(sourceDir, targetDir, false);
58     }
59     
60     public static void copyDirFiles(File sourceDir, File targetDir, boolean preserveTimestamp) {
61         File[] files = sourceDir.listFiles();
62
63         if(files==null || files.length == 0) {
64             targetDir.mkdirs();
65             if(preserveTimestamp) targetDir.setLastModified(sourceDir.lastModified());
66             return;
67         }
68         if(preserveTimestamp) targetDir.setLastModified(sourceDir.lastModified());
69         for (int i = 0; i < files.length; i++) {
70             try {
71                 File target = FileUtil.normalizeFile(new File(targetDir.getAbsolutePath() + "/" + files[i].getName())); // NOI18N
72
if(files[i].isDirectory()) {
73                     copyDirFiles(files[i], target, preserveTimestamp);
74                 } else {
75                     FileUtils.copyFile (files[i], target);
76                     if(preserveTimestamp) target.setLastModified(files[i].lastModified());
77                 }
78             } catch (IOException ex) {
79                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); // should not happen
80
}
81         }
82     }
83     
84     /**
85      * Copies the specified sourceFile to the specified targetFile.
86      * It <b>closes</b> the input stream.
87      */

88     public static void copyStreamToFile(InputStream inputStream, File targetFile) throws IOException {
89         if (inputStream == null || targetFile == null) {
90             throw new NullPointerException JavaDoc("sourcStream and targetFile must not be null"); // NOI18N
91
}
92
93         // ensure existing parent directories
94
File directory = targetFile.getParentFile();
95         if (!directory.exists() && !directory.mkdirs()) {
96             throw new IOException("Could not create directory '" + directory + "'"); // NOI18N
97
}
98
99         OutputStream outputStream = null;
100         try {
101             outputStream = createOutputStream(targetFile);
102             try {
103                 byte[] buffer = new byte[32768];
104                 for (int readBytes = inputStream.read(buffer);
105                      readBytes > 0;
106                      readBytes = inputStream.read(buffer)) {
107                     outputStream.write(buffer, 0, readBytes);
108                 }
109             }
110             catch (IOException ex) {
111                 targetFile.delete();
112                 throw ex;
113             }
114         }
115         finally {
116             if (inputStream != null) {
117                 try {
118                     inputStream.close();
119                 }
120                 catch (IOException ex) {
121                     // ignore
122
}
123             }
124             if (outputStream != null) {
125                 try {
126                     outputStream.close();
127                 }
128                 catch (IOException ex) {
129                     // ignore
130
}
131             }
132         }
133     }
134
135     /**
136      * Recursively deletes all files and directories under a given file/directory.
137      *
138      * @param file file/directory to delete
139      */

140     public static void deleteRecursively(File file) {
141         if (file.isDirectory()) {
142             File [] files = file.listFiles();
143             for (int i = 0; i < files.length; i++) {
144                 deleteRecursively(files[i]);
145             }
146         }
147         file.delete();
148     }
149     
150     /**
151      * Do the best to rename the file.
152      * @param orig regular file
153      * @param dest regular file (if exists it's rewritten)
154      */

155     public static void renameFile(File orig, File dest) throws IOException {
156         boolean destExists = dest.exists();
157         if (destExists) {
158             for (int i = 0; i<3; i++) {
159                 if (dest.delete()) {
160                     destExists = false;
161                     break;
162                 }
163                 try {
164                     Thread.sleep(71);
165                 } catch (InterruptedException JavaDoc e) {
166                 }
167             }
168         }
169
170         if (destExists == false) {
171             for (int i = 0; i<3; i++) {
172                 if (orig.renameTo(dest)) {
173                     return;
174                 }
175                 try {
176                     Thread.sleep(71);
177                 } catch (InterruptedException JavaDoc e) {
178                 }
179             }
180         }
181
182         // requires less permisions than renameTo
183
FileUtils.copyFile(orig, dest);
184
185         for (int i = 0; i<3; i++) {
186             if (orig.delete()) {
187                 return;
188             }
189             try {
190                 Thread.sleep(71);
191             } catch (InterruptedException JavaDoc e) {
192             }
193         }
194         throw new IOException("Can not delete: " + orig.getAbsolutePath()); // NOI18N
195
}
196
197     /**
198      * This utility class needs not to be instantiated anywhere.
199      */

200     private FileUtils() {
201     }
202     
203     public static BufferedInputStream createInputStream(File file) throws IOException {
204         int retry = 0;
205         while (true) {
206             try {
207                 return new BufferedInputStream(new FileInputStream(file));
208             } catch (IOException ex) {
209                 retry++;
210                 if (retry > 7) {
211                     throw ex;
212                 }
213                 try {
214                     Thread.sleep(retry * 34);
215                 } catch (InterruptedException JavaDoc iex) {
216                     throw ex;
217                 }
218             }
219         }
220     }
221     
222     public static BufferedOutputStream createOutputStream(File file) throws IOException {
223         int retry = 0;
224         while (true) {
225             try {
226                 return new BufferedOutputStream(new FileOutputStream(file));
227             } catch (IOException ex) {
228                 retry++;
229                 if (retry > 7) {
230                     throw ex;
231                 }
232                 try {
233                     Thread.sleep(retry * 34);
234                 } catch (InterruptedException JavaDoc iex) {
235                     throw ex;
236                 }
237             }
238         }
239     }
240
241     /** Creates new tmp dir in java.io.tmpdir */
242     public static File createTmpFolder(String JavaDoc prefix) {
243         String JavaDoc tmpDir = System.getProperty("java.io.tmpdir"); // NOI18N
244
File tmpFolder = new File(tmpDir);
245         File checkoutFolder = null;
246         try {
247             // generate unique name for tmp folder
248
File tmp = File.createTempFile(prefix, "", tmpFolder); // NOI18N
249
if (tmp.delete() == false) {
250                 return checkoutFolder;
251             }
252             if (tmp.mkdirs() == false) {
253                 return checkoutFolder;
254             }
255             checkoutFolder = FileUtil.normalizeFile(tmp);
256         } catch (IOException e) {
257             ErrorManager err = ErrorManager.getDefault();
258             err.notify(e);
259         }
260         return checkoutFolder;
261     }
262
263     
264 }
265
Popular Tags