1 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 30 public class FileUtils { 31 32 35 public static void copyFile(File sourceFile, File targetFile) throws IOException { 36 if (sourceFile == null || targetFile == null) { 37 throw new NullPointerException ("sourceFile and targetFile must not be null"); } 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 } 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())); 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); } 81 } 82 } 83 84 88 public static void copyStreamToFile(InputStream inputStream, File targetFile) throws IOException { 89 if (inputStream == null || targetFile == null) { 90 throw new NullPointerException ("sourcStream and targetFile must not be null"); } 92 93 File directory = targetFile.getParentFile(); 95 if (!directory.exists() && !directory.mkdirs()) { 96 throw new IOException("Could not create directory '" + directory + "'"); } 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 } 123 } 124 if (outputStream != null) { 125 try { 126 outputStream.close(); 127 } 128 catch (IOException ex) { 129 } 131 } 132 } 133 } 134 135 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 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 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 e) { 178 } 179 } 180 } 181 182 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 e) { 192 } 193 } 194 throw new IOException("Can not delete: " + orig.getAbsolutePath()); } 196 197 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 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 iex) { 235 throw ex; 236 } 237 } 238 } 239 } 240 241 242 public static File createTmpFolder(String prefix) { 243 String tmpDir = System.getProperty("java.io.tmpdir"); File tmpFolder = new File(tmpDir); 245 File checkoutFolder = null; 246 try { 247 File tmp = File.createTempFile(prefix, "", tmpFolder); 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 |