1 21 22 package org.opensubsystems.core.util; 23 24 import java.io.BufferedInputStream ; 25 import java.io.BufferedOutputStream ; 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.FileOutputStream ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.OutputStream ; 32 import java.util.Arrays ; 33 import java.util.Date ; 34 import java.util.List ; 35 import java.util.Properties ; 36 import java.util.logging.Logger ; 37 38 import org.opensubsystems.core.error.OSSException; 39 import org.opensubsystems.core.error.OSSMultiException; 40 41 49 public class FileUtils 50 { 51 53 59 public static final String TEMPORARY_DIRECTORY_PATH = "oss.file.path.temp"; 60 61 63 66 public static final String TEMPORARY_DIRECTORY_PATH_DEFAULT 67 = GlobalConstants.getTempDirectory() + "osstemp"; 68 69 75 public static final int[] DEFAULT_STRORAGE_TREE_DISTRIBUTION = {2, 2, 2, 4}; 76 77 80 public static final int BUFFER_SIZE = 65536; 81 82 84 87 private static Logger s_logger = Log.getInstance(FileUtils.class); 88 89 92 protected static String s_strTempDirectory; 93 94 96 99 static 100 { 101 Properties prpSettings; 103 104 prpSettings = Config.getInstance().getPropertiesSafely(); 105 106 s_strTempDirectory = Config.getStringProperty( 107 prpSettings, 108 TEMPORARY_DIRECTORY_PATH, 109 TEMPORARY_DIRECTORY_PATH_DEFAULT, 110 "Directory to store temporary files"); 111 112 if ((!s_strTempDirectory.endsWith(File.separator)) 113 && (GlobalConstants.isWindows() && (!s_strTempDirectory.endsWith("/")))) 115 { 116 s_strTempDirectory += File.separator; 117 } 118 s_logger.finest(TEMPORARY_DIRECTORY_PATH + " = " + s_strTempDirectory); 120 } 121 122 124 136 public static void moveFile( 137 File flCurrent, 138 String strNewName 139 ) throws IOException , 140 OSSException 141 { 142 File flDestination; 144 145 flDestination = new File (strNewName); 146 FileUtils.moveFile(flCurrent, flDestination); 147 } 148 149 161 public static void moveFile( 162 File flCurrent, 163 File flDestination 164 ) throws IOException , 165 OSSException 166 { 167 if (flCurrent.exists()) 170 { 171 if (flDestination.exists()) 173 { 174 s_logger.warning("Destination file " 178 + flDestination.getAbsolutePath() 179 + " exists. Renaming as duplicate."); 180 renameToTemporaryName(flDestination, "old"); 181 } 182 183 File flFolder; 185 186 flFolder = flDestination.getParentFile(); 187 if ((flFolder != null) && (!flFolder.exists())) 188 { 189 if (!flFolder.mkdirs()) 190 { 191 if (!flFolder.exists()) 195 { 196 throw new IOException ("Cannot create directory " + flFolder); 197 } 198 } 199 } 200 201 if (!flCurrent.renameTo(flDestination)) 206 { 207 copyFile(flCurrent, flDestination); 210 211 if (!flCurrent.delete()) 213 { 214 flDestination.delete(); 217 throw new IOException ("Cannot delete already copied file " + flCurrent); 218 } 219 } 220 } 221 } 222 223 231 public static void copyFile( 232 File flCurrent, 233 File flDestination 234 ) throws IOException , 235 OSSException 236 { 237 File flFolder; 239 240 flFolder = flDestination.getParentFile(); 241 if ((flFolder != null) && (!flFolder.exists())) 242 { 243 if (!flFolder.mkdirs()) 244 { 245 if (!flFolder.exists()) 249 { 250 throw new IOException ("Cannot create directory " + flFolder); 251 } 252 } 253 } 254 255 FileInputStream finInput = null; 258 259 272 try 273 { 274 finInput = new FileInputStream (flCurrent); 275 } 276 catch (IOException ioExec) 277 { 278 if (finInput != null) 279 { 280 try 281 { 282 finInput.close(); 283 } 284 catch (Throwable thr) 285 { 286 throw new OSSMultiException(ioExec, thr); 287 } 288 } 289 throw ioExec; 290 } 291 292 FileUtils.copyStreamToFile(finInput, flDestination); 293 } 294 295 302 public static void renameToTemporaryName( 303 File flFileToRename, 304 String strPrefix 305 ) throws IOException 306 { 307 assert strPrefix != null : "Prefix cannot be null."; 308 309 String strParent; 310 StringBuffer sbBuffer = new StringBuffer (); 311 File flTemp; 312 int iIndex = 0; 313 314 strParent = flFileToRename.getParent(); 315 316 do 318 { 319 iIndex++; 320 sbBuffer.delete(0, sbBuffer.length()); 321 if (strParent != null) 322 { 323 sbBuffer.append(strParent); 324 sbBuffer.append(File.separatorChar); 325 } 326 327 sbBuffer.append(strPrefix); 328 sbBuffer.append("_"); 329 sbBuffer.append(iIndex); 330 sbBuffer.append("_"); 331 sbBuffer.append(flFileToRename.getName()); 332 333 flTemp = new File (sbBuffer.toString()); 334 } 335 while (flTemp.exists()); 336 337 if (!flFileToRename.renameTo(flTemp)) 339 { 340 throw new IOException ("Cannot rename " + flFileToRename.getAbsolutePath() 341 + " to " + flTemp.getAbsolutePath()); 342 } 343 } 344 345 352 public static boolean deleteDirectoryContent( 353 String strDir 354 ) 355 { 356 return ((strDir != null) && (strDir.length() > 0)) 357 ? deleteDirectoryContent(new File (strDir)) : false; 358 } 359 360 367 public static boolean deleteDirectoryContent( 368 File fDir 369 ) 370 { 371 boolean bRetval = false; 372 373 if (fDir != null && fDir.isDirectory()) 374 { 375 File [] files = fDir.listFiles(); 376 377 if (files != null) 378 { 379 bRetval = true; 380 boolean dirDeleted; 381 382 for (int index = 0; index < files.length; index++) 383 { 384 if (files[index].isDirectory()) 385 { 386 dirDeleted = deleteDirectoryContent(files[index]); 390 if (dirDeleted) 391 { 392 bRetval = bRetval && files[index].delete(); 393 } 394 else 395 { 396 bRetval = false; 397 } 398 } 399 else 400 { 401 bRetval = bRetval && files[index].delete(); 402 } 403 } 404 } 405 } 406 407 return bRetval; 408 } 409 410 417 public static boolean deleteDir( 418 String strDir 419 ) 420 { 421 return ((strDir != null) && (strDir.length() > 0)) 422 ? deleteDir(new File (strDir)) : false; 423 } 424 425 432 public static boolean deleteDir( 433 File fDir 434 ) 435 { 436 boolean bRetval = false; 437 if (fDir != null && fDir.exists()) 438 { 439 bRetval = deleteDirectoryContent(fDir); 440 if (bRetval) 441 { 442 bRetval = bRetval && fDir.delete(); 443 } 444 } 445 return bRetval; 446 } 447 448 456 public boolean isFileBinaryEqual( 457 File first, 458 File second 459 ) throws IOException 460 { 461 boolean retval = false; 463 464 if ((first.exists()) && (second.exists()) 465 && (first.isFile()) && (second.isFile())) 466 { 467 if (first.getCanonicalPath().equals(second.getCanonicalPath())) 468 { 469 retval = true; 470 } 471 else 472 { 473 FileInputStream firstInput = null; 474 FileInputStream secondInput = null; 475 BufferedInputStream bufFirstInput = null; 476 BufferedInputStream bufSecondInput = null; 477 478 try 479 { 480 firstInput = new FileInputStream (first); 481 secondInput = new FileInputStream (second); 482 bufFirstInput = new BufferedInputStream (firstInput, BUFFER_SIZE); 483 bufSecondInput = new BufferedInputStream (secondInput, BUFFER_SIZE); 484 485 int firstByte; 486 int secondByte; 487 488 while (true) 489 { 490 firstByte = bufFirstInput.read(); 491 secondByte = bufSecondInput.read(); 492 if (firstByte != secondByte) 493 { 494 break; 495 } 496 if ((firstByte < 0) && (secondByte < 0)) 497 { 498 retval = true; 499 break; 500 } 501 } 502 } 503 finally 504 { 505 try 506 { 507 if (bufFirstInput != null) 508 { 509 bufFirstInput.close(); 510 } 511 } 512 finally 513 { 514 if (bufSecondInput != null) 515 { 516 bufSecondInput.close(); 517 } 518 } 519 } 520 } 521 } 522 523 return retval; 524 } 525 526 534 public static List listFiles( 535 File directory, 536 int maximum, 537 Date olderThan 538 ) 539 { 540 List lRetval = null; 541 if (directory != null && directory.isDirectory()) 542 { 543 File [] files = null; 544 files = directory.listFiles(new MaximumFileFilter(maximum, olderThan)); 549 550 if (files != null) 551 { 552 lRetval = Arrays.asList(files); 553 } 554 } 555 return lRetval; 556 } 557 558 570 public static String getFileStoragePath( 571 long lID, 572 String rootDirPath, 573 String idExtension, 574 String fileExtension 575 ) 576 { 577 return getFileStoragePath(lID, rootDirPath, idExtension, fileExtension, 578 DEFAULT_STRORAGE_TREE_DISTRIBUTION); 579 } 580 581 594 public static String getFileStoragePath( 595 long lID, 596 String rootDirPath, 597 String idExtension, 598 String fileExtension, 599 int[] treeDistribution 600 ) 601 { 602 int iDigit = 0; 603 604 StringBuffer idString = new StringBuffer (Long.toString(lID)); 605 606 StringBuffer retval = new StringBuffer (rootDirPath); 607 608 for (int index = 0; index < treeDistribution.length; index++) 609 { 610 if (GlobalConstants.ERROR_CHECKING) 611 { 612 assert treeDistribution[index] > 0 : "value <= 0 in tree distribution array"; 613 } 614 iDigit += treeDistribution[index]; 615 } 616 617 if (GlobalConstants.ERROR_CHECKING) 618 { 619 assert idString.length() <= iDigit : "ID to big for three capacity"; 620 } 621 622 idString.insert(0, NumberUtils.ZEROCHARS, 0, iDigit - idString.length()); 624 625 int currentCount = 0; 627 for (int index = 0; index < treeDistribution.length - 1; index++) 630 { 631 retval.append(idString.subSequence( 632 currentCount, 633 currentCount + treeDistribution[index])); 634 currentCount += treeDistribution[index]; 635 retval.append(File.separator); 636 } 637 638 retval.append(idString); 640 641 if (idExtension != null && idExtension.length() > 0) 643 { 644 retval.append(idExtension); 645 } 646 retval.append(fileExtension); 648 649 return retval.toString(); 650 } 651 652 659 public static TwoObjectStruct getFilenameSplit( 660 String filePath 661 ) 662 { 663 return new TwoObjectStruct( 664 filePath.substring(0, filePath.lastIndexOf(File.separator) + 1), 665 filePath.substring(filePath.lastIndexOf(File.separator) + 1)); 666 } 667 668 674 public static String getTemporaryDirectory( 675 ) 676 { 677 return s_strTempDirectory; 678 } 679 680 692 public static String createTemporarySubdirectory( 693 String strPrefix, 694 String strTemporarySubDirID, 695 boolean bUseSession 696 ) throws IOException 697 { 698 StringBuffer sbRepositoryPath = new StringBuffer (); 699 File fRepositoryPath; 700 701 sbRepositoryPath.append(getTemporaryDirectory()); 702 if (bUseSession) 703 { 704 sbRepositoryPath.append(CallContext.getInstance().getCurrentSession()); 705 sbRepositoryPath.append(File.separator); 706 } 707 sbRepositoryPath.append(strPrefix); 708 sbRepositoryPath.append(strTemporarySubDirID); 709 sbRepositoryPath.append(File.separator); 710 711 fRepositoryPath = new File (sbRepositoryPath.toString()); 712 713 if (!fRepositoryPath.exists()) 714 { 715 if (!fRepositoryPath.mkdirs()) 717 { 718 if (!fRepositoryPath.exists()) 722 { 723 throw new IOException ( 725 "Error occured during creating temporary directory."); 726 } 727 } 728 } 729 else if (bUseSession) 730 { 731 throw new IOException ("Cannot create unique temporary directory because" + 733 " it already exists."); 734 } 735 736 return sbRepositoryPath.toString(); 737 } 738 739 748 public static void copyStreamToFile( 749 InputStream input, 750 File output 751 ) throws IOException , 752 OSSMultiException 753 { 754 FileOutputStream foutOutput = null; 755 756 try 758 { 759 foutOutput = new FileOutputStream (output); 760 } 761 catch (IOException ioExec) 762 { 763 if (foutOutput != null) 764 { 765 try 766 { 767 foutOutput.close(); 768 } 769 catch (IOException ioExec2) 770 { 771 throw new OSSMultiException(ioExec, ioExec2); 772 } 773 } 774 775 throw ioExec; 776 } 777 778 FileUtils.copyStreamToStream(input, foutOutput); 781 } 782 783 792 public static void copyStreamToStream( 793 InputStream input, 794 OutputStream output 795 ) throws IOException , 796 OSSMultiException 797 { 798 InputStream is = null; 799 OutputStream os = null; 800 int ch; 801 802 try 803 { 804 if (input instanceof BufferedInputStream ) 805 { 806 is = input; 807 } 808 else 809 { 810 is = new BufferedInputStream (input); 811 } 812 if (output instanceof BufferedOutputStream ) 813 { 814 os = output; 815 } 816 else 817 { 818 os = new BufferedOutputStream (output); 819 } 820 821 while ((ch = is.read()) != -1) 822 { 823 os.write(ch); 824 } 825 os.flush(); 826 } 827 finally 828 { 829 IOException exec1 = null; 830 IOException exec2 = null; 831 try 832 { 833 if (os != null) 836 { 837 try 838 { 839 os.close(); 840 } 841 catch (IOException exec) 842 { 843 exec1 = exec; 844 } 845 } 846 } 847 finally 848 { 849 if (is != null) 850 { 851 try 852 { 853 is.close(); 854 } 855 catch (IOException exec) 856 { 857 exec2 = exec; 858 } 859 } 860 } 861 if ((exec1 != null) && (exec2 != null)) 862 { 863 throw new OSSMultiException(exec1, exec2); 864 } 865 else if (exec1 != null) 866 { 867 throw exec1; 868 } 869 else if (exec2 != null) 870 { 871 throw exec2; 872 } 873 } 874 } 875 } 876 | Popular Tags |