KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > file > 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 package org.netbeans.lib.cvsclient.file;
20
21 import java.io.*;
22
23 /**
24  * A utility class for file based operations.
25  *
26  * @author Thomas Singer
27  * @version Nov 23, 2001
28  */

29 public class FileUtils {
30     private static FileReadOnlyHandler fileReadOnlyHandler;
31
32     /**
33      * Returns the current FileReadOnlyHandler used by setFileReadOnly().
34      */

35     public static FileReadOnlyHandler getFileReadOnlyHandler() {
36         return fileReadOnlyHandler;
37     }
38
39     /**
40      * Sets the specified fileReadOnlyHandler to be used with setFileReadOnly().
41      */

42     public static void setFileReadOnlyHandler(FileReadOnlyHandler fileReadOnlyHandler) {
43         FileUtils.fileReadOnlyHandler = fileReadOnlyHandler;
44     }
45
46     /**
47      * Sets the specified file read-only (readOnly == true) or writable (readOnly == false).
48      * If no fileReadOnlyHandler is set, nothing happens.
49      *
50      * @throws IOException if the operation failed
51      */

52     public static void setFileReadOnly(File file, boolean readOnly) throws IOException {
53         if (getFileReadOnlyHandler() == null) {
54             return;
55         }
56
57         getFileReadOnlyHandler().setFileReadOnly(file, readOnly);
58     }
59
60     /**
61      * Copies the specified sourceFile to the specified targetFile.
62      */

63     public static void copyFile(File sourceFile, File targetFile) throws IOException {
64         if (sourceFile == null || targetFile == null) {
65             throw new NullPointerException JavaDoc("sourceFile and targetFile must not be null"); // NOI18N
66
}
67
68         // ensure existing parent directories
69
File directory = targetFile.getParentFile();
70         if (!directory.exists() && !directory.mkdirs()) {
71             throw new IOException("Could not create directory '" + directory + "'"); // NOI18N
72
}
73
74         InputStream inputStream = null;
75         OutputStream outputStream = null;
76         try {
77             inputStream = new BufferedInputStream(new FileInputStream(sourceFile));
78             outputStream = new BufferedOutputStream(new FileOutputStream(targetFile));
79
80             try {
81                 byte[] buffer = new byte[32768];
82                 for (int readBytes = inputStream.read(buffer);
83                      readBytes > 0;
84                      readBytes = inputStream.read(buffer)) {
85                     outputStream.write(buffer, 0, readBytes);
86                 }
87             }
88             catch (IOException ex) {
89                 targetFile.delete();
90                 throw ex;
91             }
92         }
93         finally {
94             if (inputStream != null) {
95                 try {
96                     inputStream.close();
97                 }
98                 catch (IOException ex) {
99                     // ignore
100
}
101             }
102             if (outputStream != null) {
103                 try {
104                     outputStream.close();
105                 }
106                 catch (IOException ex) {
107                     // ignore
108
}
109             }
110         }
111     }
112
113
114     /**
115      * Do the best to rename the file.
116      * @param orig regular file
117      * @param dest regular file (if exists it's rewritten)
118      */

119     public static void renameFile(File orig, File dest) throws IOException {
120         boolean destExists = dest.exists();
121         if (destExists) {
122             for (int i = 0; i<3; i++) {
123                 if (dest.delete()) {
124                     destExists = false;
125                     break;
126                 }
127                 try {
128                     Thread.sleep(71);
129                 } catch (InterruptedException JavaDoc e) {
130                 }
131             }
132         }
133
134         if (destExists == false) {
135             for (int i = 0; i<3; i++) {
136                 if (orig.renameTo(dest)) {
137                     return;
138                 }
139                 try {
140                     Thread.sleep(71);
141                 } catch (InterruptedException JavaDoc e) {
142                 }
143             }
144         }
145
146         // requires less permisions than renameTo
147
FileUtils.copyFile(orig, dest);
148
149         for (int i = 0; i<3; i++) {
150             if (orig.delete()) {
151                 return;
152             }
153             try {
154                 Thread.sleep(71);
155             } catch (InterruptedException JavaDoc e) {
156             }
157         }
158         throw new IOException("Can not delete: " + orig.getAbsolutePath()); // NOI18N
159
}
160
161     /**
162      * This utility class needs not to be instantiated anywhere.
163      */

164     private FileUtils() {
165     }
166 }
167
Popular Tags