1 21 22 package org.apache.derby.iapi.services.io; 23 24 import org.apache.derby.io.StorageFactory; 25 import org.apache.derby.io.WritableStorageFactory; 26 import org.apache.derby.io.StorageFile; 27 28 import java.io.*; 29 import java.net.*; 30 31 34 public abstract class FileUtil { 35 36 private static final int BUFFER_SIZE = 4096*4; 37 49 public static boolean removeDirectory(File directory) { 50 51 53 if (directory == null) 54 return false; 55 if (!directory.exists()) 56 return true; 57 if (!directory.isDirectory()) 58 return false; 59 60 String [] list = directory.list(); 61 62 if (list != null) { 65 for (int i = 0; i < list.length; i++) { 66 File entry = new File(directory, list[i]); 67 68 70 if (entry.isDirectory()) 71 { 72 if (!removeDirectory(entry)) 73 return false; 74 } 75 else 76 { 77 if (!entry.delete()) 78 return false; 79 } 80 } 81 } 82 83 return directory.delete(); 84 } 85 86 public static boolean removeDirectory(String directory) 87 { 88 return removeDirectory(new File(directory)); 89 } 90 91 94 public static boolean copyDirectory(File from, File to) 95 { 96 return copyDirectory(from, to, (byte[])null, (String [])null); 97 } 98 99 public static boolean copyDirectory(String from, String to) 100 { 101 return copyDirectory(new File(from), new File(to)); 102 } 103 104 107 public static boolean copyDirectory(File from, File to, byte[] buffer, 108 String [] filter) 109 { 110 113 if (from == null) 114 return false; 115 if (!from.exists()) 116 return true; 117 if (!from.isDirectory()) 118 return false; 119 120 if (to.exists()) 121 { 122 return false; 124 } 125 if (!to.mkdirs()) 126 { 127 return false; 129 } 130 131 String [] list = from.list(); 132 133 if (list != null) { 136 137 if (buffer == null) 138 buffer = new byte[BUFFER_SIZE]; 140 nextFile: for (int i = 0; i < list.length; i++) { 141 142 String fileName = list[i]; 143 144 if (filter != null) { 145 for (int j = 0; j < filter.length; j++) { 146 if (fileName.equals(filter[j])) 147 continue nextFile; 148 } 149 } 150 151 152 File entry = new File(from, fileName); 153 154 156 if (entry.isDirectory()) 157 { 158 if (!copyDirectory(entry,new File(to,fileName),buffer,filter)) 159 return false; 160 } 161 else 162 { 163 if (!copyFile(entry,new File(to,fileName),buffer)) 164 return false; 165 } 166 } 167 } 168 return true; 169 } 170 171 public static boolean copyFile(File from, File to) 172 { 173 return copyFile(from, to, (byte[])null); 174 } 175 176 public static boolean copyFile(File from, File to, byte[] buf) 177 { 178 if (buf == null) 179 buf = new byte[BUFFER_SIZE]; 180 181 FileInputStream from_s = null; 184 FileOutputStream to_s = null; 185 186 try { 187 from_s = new FileInputStream(from); 188 to_s = new FileOutputStream(to); 189 190 for (int bytesRead = from_s.read(buf); 191 bytesRead != -1; 192 bytesRead = from_s.read(buf)) 193 to_s.write(buf,0,bytesRead); 194 195 from_s.close(); 196 from_s = null; 197 198 to_s.getFD().sync(); to_s.close(); 200 to_s = null; 201 } 202 catch (IOException ioe) 203 { 204 return false; 205 } 206 finally 207 { 208 if (from_s != null) 209 { 210 try { from_s.close(); } 211 catch (IOException ioe) {} 212 } 213 if (to_s != null) 214 { 215 try { to_s.close(); } 216 catch (IOException ioe) {} 217 } 218 } 219 220 return true; 221 } 222 223 public static boolean copyDirectory( StorageFactory storageFactory, 224 StorageFile from, 225 File to) 226 { 227 return copyDirectory( storageFactory, from, to, null, null, true); 228 } 229 230 231 public static boolean copyDirectory( StorageFactory storageFactory, 232 StorageFile from, 233 File to, 234 byte[] buffer, 235 String [] filter, 236 boolean copySubDirs) 237 { 238 if (from == null) 239 return false; 240 if (!from.exists()) 241 return true; 242 if (!from.isDirectory()) 243 return false; 244 245 if (to.exists()) 246 { 247 return false; 249 } 250 if (!to.mkdirs()) 251 { 252 return false; 254 } 255 256 String [] list = from.list(); 257 258 if (list != null) 261 { 262 if (buffer == null) 263 buffer = new byte[BUFFER_SIZE]; 265 nextFile: 266 for (int i = 0; i < list.length; i++) 267 { 268 String fileName = list[i]; 269 270 if (filter != null) { 271 for (int j = 0; j < filter.length; j++) { 272 if (fileName.equals(filter[j])) 273 continue nextFile; 274 } 275 } 276 277 StorageFile entry = storageFactory.newStorageFile(from, fileName); 278 279 if (entry.isDirectory()) 280 { 281 if(copySubDirs) { 282 if (!copyDirectory( storageFactory, entry, 283 new File(to,fileName), buffer, 284 filter, copySubDirs)) 285 return false; 286 } 287 else { 288 continue nextFile; 291 } 292 293 } 294 else 295 { 296 if (!copyFile( storageFactory, entry, new File(to,fileName), buffer)) 297 return false; 298 } 299 } 300 } 301 return true; 302 } 304 public static boolean copyFile( StorageFactory storageFactory, StorageFile from, File to) 305 { 306 return copyFile( storageFactory, from, to, (byte[]) null); 307 } 308 309 public static boolean copyFile( StorageFactory storageFactory, StorageFile from, File to, byte[] buf) 310 { 311 InputStream from_s = null; 312 FileOutputStream to_s = null; 313 314 try { 315 from_s = from.getInputStream(); 316 to_s = new FileOutputStream( to); 317 318 if (buf == null) 319 buf = new byte[BUFFER_SIZE]; 321 for (int bytesRead = from_s.read(buf); 322 bytesRead != -1; 323 bytesRead = from_s.read(buf)) 324 to_s.write(buf,0,bytesRead); 325 326 from_s.close(); 327 from_s = null; 328 329 to_s.getFD().sync(); to_s.close(); 331 to_s = null; 332 } 333 catch (IOException ioe) 334 { 335 return false; 336 } 337 finally 338 { 339 if (from_s != null) 340 { 341 try { from_s.close(); } 342 catch (IOException ioe) {} 343 } 344 if (to_s != null) 345 { 346 try { to_s.close(); } 347 catch (IOException ioe) {} 348 } 349 } 350 351 return true; 352 } 354 public static boolean copyDirectory( WritableStorageFactory storageFactory, 355 File from, 356 StorageFile to) 357 { 358 return copyDirectory( storageFactory, from, to, null, null); 359 } 360 361 public static boolean copyDirectory( WritableStorageFactory storageFactory, 362 File from, 363 StorageFile to, 364 byte[] buffer, 365 String [] filter) 366 { 367 if (from == null) 368 return false; 369 if (!from.exists()) 370 return true; 371 if (!from.isDirectory()) 372 return false; 373 374 if (to.exists()) 375 { 376 return false; 378 } 379 if (!to.mkdirs()) 380 { 381 return false; 383 } 384 385 String [] list = from.list(); 386 387 if (list != null) 390 { 391 if (buffer == null) 392 buffer = new byte[BUFFER_SIZE]; 394 nextFile: 395 for (int i = 0; i < list.length; i++) 396 { 397 String fileName = list[i]; 398 399 if (filter != null) { 400 for (int j = 0; j < filter.length; j++) { 401 if (fileName.equals(filter[j])) 402 continue nextFile; 403 } 404 } 405 406 File entry = new File(from, fileName); 407 408 if (entry.isDirectory()) 409 { 410 if (!copyDirectory( storageFactory, entry, storageFactory.newStorageFile(to,fileName), buffer, filter)) 411 return false; 412 } 413 else 414 { 415 if (!copyFile( storageFactory, entry, storageFactory.newStorageFile(to,fileName), buffer)) 416 return false; 417 } 418 } 419 } 420 return true; 421 } 423 public static boolean copyFile( WritableStorageFactory storageFactory, File from, StorageFile to) 424 { 425 return copyFile( storageFactory, from, to, (byte[]) null); 426 } 427 428 public static boolean copyFile( WritableStorageFactory storageFactory, File from, StorageFile to, byte[] buf) 429 { 430 InputStream from_s = null; 431 OutputStream to_s = null; 432 433 try { 434 from_s = new FileInputStream( from); 435 to_s = to.getOutputStream(); 436 437 if (buf == null) 438 buf = new byte[BUFFER_SIZE]; 440 for (int bytesRead = from_s.read(buf); 441 bytesRead != -1; 442 bytesRead = from_s.read(buf)) 443 to_s.write(buf,0,bytesRead); 444 445 from_s.close(); 446 from_s = null; 447 448 storageFactory.sync( to_s, false); to_s.close(); 450 to_s = null; 451 } 452 catch (IOException ioe) 453 { 454 return false; 455 } 456 finally 457 { 458 if (from_s != null) 459 { 460 try { from_s.close(); } 461 catch (IOException ioe) {} 462 } 463 if (to_s != null) 464 { 465 try { to_s.close(); } 466 catch (IOException ioe) {} 467 } 468 } 469 470 return true; 471 } 473 474 public static boolean copyFile( WritableStorageFactory storageFactory, 475 StorageFile from, StorageFile to) 476 { 477 return copyFile( storageFactory, from, to, (byte[]) null); 478 } 479 480 public static boolean copyFile( WritableStorageFactory storageFactory, 481 StorageFile from, StorageFile to, 482 byte[] buf) 483 { 484 InputStream from_s = null; 485 OutputStream to_s = null; 486 487 try { 488 from_s = from.getInputStream(); 489 to_s = to.getOutputStream(); 490 491 if (buf == null) 492 buf = new byte[BUFFER_SIZE]; 494 for (int bytesRead = from_s.read(buf); 495 bytesRead != -1; 496 bytesRead = from_s.read(buf)) 497 to_s.write(buf,0,bytesRead); 498 499 from_s.close(); 500 from_s = null; 501 502 storageFactory.sync( to_s, false); to_s.close(); 504 to_s = null; 505 } 506 catch (IOException ioe) 507 { 508 return false; 509 } 510 finally 511 { 512 if (from_s != null) 513 { 514 try { from_s.close(); } 515 catch (IOException ioe) {} 516 } 517 if (to_s != null) 518 { 519 try { to_s.close(); } 520 catch (IOException ioe) {} 521 } 522 } 523 524 return true; 525 } 527 534 public static File getAbsoluteFile(File root, String path) { 535 File file = new File(path); 536 if (file.isAbsolute()) 537 return file; 538 539 if (root == null) 540 return null; 541 542 return new File(root, path); 543 } 544 545 551 public static File newFile(File parent, String name) { 552 553 if (parent == null) 554 return new File(name); 555 else 556 return new File(parent, name); 557 } 558 559 566 public static InputStream getInputStream(String fileOrURL,int bufferSize) 567 throws IOException 568 { 569 InputStream is; 570 try { 571 is = new FileInputStream( fileOrURL ); 572 } 573 574 catch (FileNotFoundException fnfe){ 575 try { 576 is = new URL( fileOrURL ).openStream(); 577 } catch (MalformedURLException mfurle) { 578 579 if (fileOrURL.indexOf(':') > 2) 584 throw mfurle; 585 throw fnfe; 586 } 587 } 588 if (bufferSize > 0) 589 is = new BufferedInputStream(is,bufferSize); 590 591 return is; 592 } 593 } 594 | Popular Tags |