1 50 package org.apache.avalon.excalibur.io; 51 52 import java.io.File ; 53 import java.io.FileInputStream ; 54 import java.io.FileOutputStream ; 55 import java.io.IOException ; 56 import java.io.InputStream ; 57 import java.net.URL ; 58 59 92 public final class FileUtil 93 { 94 98 private FileUtil() 99 { 100 } 101 102 109 public static boolean contentEquals( final File file1, final File file2 ) 110 throws IOException 111 { 112 final boolean file1Exists = file1.exists(); 113 if( file1Exists != file2.exists() ) 114 { 115 return false; 116 } 117 118 if( !file1Exists ) 119 { 120 return true; 122 } 123 124 if( file1.isDirectory() || file2.isDirectory() ) 125 { 126 return false; 128 } 129 130 InputStream input1 = null; 131 InputStream input2 = null; 132 try 133 { 134 input1 = new FileInputStream ( file1 ); 135 input2 = new FileInputStream ( file2 ); 136 return IOUtil.contentEquals( input1, input2 ); 137 138 } 139 finally 140 { 141 IOUtil.shutdownStream( input1 ); 142 IOUtil.shutdownStream( input2 ); 143 } 144 } 145 146 152 public static File toFile( final URL url ) 153 { 154 if( url.getProtocol().equals( "file" ) == false ) 155 { 156 return null; 157 } 158 else 159 { 160 final String filename = url.getFile().replace( '/', File.separatorChar ); 161 return new File ( filename ); 162 } 163 } 164 165 172 public static URL [] toURLs( final File [] files ) 173 throws IOException 174 { 175 final URL [] urls = new URL [ files.length ]; 176 177 for( int i = 0; i < urls.length; i++ ) 178 { 179 urls[ i ] = files[ i ].toURL(); 180 } 181 182 return urls; 183 } 184 185 198 public static String removeExtention( final String filename ) 199 { 200 return removeExtension( filename ); 201 } 202 203 215 public static String removeExtension( final String filename ) 216 { 217 final int index = filename.lastIndexOf( '.' ); 218 219 if( -1 == index ) 220 { 221 return filename; 222 } 223 else 224 { 225 return filename.substring( 0, index ); 226 } 227 } 228 229 241 public static String getExtension( final String filename ) 242 { 243 final int index = filename.lastIndexOf( '.' ); 244 245 if( -1 == index ) 246 { 247 return ""; 248 } 249 else 250 { 251 return filename.substring( index + 1 ); 252 } 253 } 254 255 266 public static String removePath( final String filepath ) 267 { 268 return removePath( filepath, File.separatorChar ); 269 } 270 271 282 public static String removePath( final String filepath, final char fileSeparatorChar ) 283 { 284 final int index = filepath.lastIndexOf( fileSeparatorChar ); 285 286 if( -1 == index ) 287 { 288 return filepath; 289 } 290 else 291 { 292 return filepath.substring( index + 1 ); 293 } 294 } 295 296 307 public static String getPath( final String filepath ) 308 { 309 return getPath( filepath, File.separatorChar ); 310 } 311 312 323 public static String getPath( final String filepath, final char fileSeparatorChar ) 324 { 325 final int index = filepath.lastIndexOf( fileSeparatorChar ); 326 if( -1 == index ) 327 { 328 return ""; 329 } 330 else 331 { 332 return filepath.substring( 0, index ); 333 } 334 } 335 336 349 public static void copyFileToDirectory( final String source, 350 final String destinationDirectory ) 351 throws IOException 352 { 353 copyFileToDirectory( new File ( source ), 354 new File ( destinationDirectory ) ); 355 } 356 357 370 public static void copyFileToDirectory( final File source, 371 final File destinationDirectory ) 372 throws IOException 373 { 374 if( destinationDirectory.exists() && !destinationDirectory.isDirectory() ) 375 { 376 throw new IllegalArgumentException ( "Destination is not a directory" ); 377 } 378 379 copyFile( source, new File ( destinationDirectory, source.getName() ) ); 380 } 381 382 397 public static void copyFile( final File source, final File destination ) 398 throws IOException 399 { 400 if( !source.exists() ) 402 { 403 final String message = "File " + source + " does not exist"; 404 throw new IOException ( message ); 405 } 406 407 if( destination.getParentFile() != null && 409 !destination.getParentFile().exists() ) 410 { 411 destination.getParentFile().mkdirs(); 412 } 413 414 if( destination.exists() && !destination.canWrite() ) 416 { 417 final String message = "Unable to open file " + 418 destination + " for writing."; 419 throw new IOException ( message ); 420 } 421 422 final FileInputStream input = new FileInputStream ( source ); 423 final FileOutputStream output = new FileOutputStream ( destination ); 424 IOUtil.copy( input, output ); 425 IOUtil.shutdownStream( input ); 426 IOUtil.shutdownStream( output ); 427 428 if( source.length() != destination.length() ) 429 { 430 final String message = "Failed to copy full contents from " + source + 431 " to " + destination; 432 throw new IOException ( message ); 433 } 434 } 435 436 452 public static void copyURLToFile( final URL source, final File destination ) 453 throws IOException 454 { 455 if( destination.getParentFile() != null && 457 !destination.getParentFile().exists() ) 458 { 459 destination.getParentFile().mkdirs(); 460 } 461 462 if( destination.exists() && !destination.canWrite() ) 464 { 465 final String message = "Unable to open file " + 466 destination + " for writing."; 467 throw new IOException ( message ); 468 } 469 470 final InputStream input = source.openStream(); 471 final FileOutputStream output = new FileOutputStream ( destination ); 472 IOUtil.copy( input, output ); 473 IOUtil.shutdownStream( input ); 474 IOUtil.shutdownStream( output ); 475 } 476 477 507 public static final String normalize( String path ) 508 { 509 if( path.length() < 2 ) 510 { 511 return path; 512 } 513 514 StringBuffer buff = new StringBuffer ( path ); 515 516 int length = path.length(); 517 518 String prefix = null; 520 521 if( length > 2 && buff.charAt( 1 ) == ':' ) 522 { 523 prefix = path.substring( 0, 2 ); 524 buff.delete( 0, 2 ); 525 path = path.substring( 2 ); 526 length -= 2; 527 } 528 529 boolean startsWithSlash = length > 0 && ( buff.charAt( 0 ) == '/' || buff.charAt( 0 ) == '\\' ); 530 531 boolean expStart = true; 532 int ptCount = 0; 533 int lastSlash = length + 1; 534 int upLevel = 0; 535 536 for( int i = length - 1; i >= 0; i-- ) 537 switch( path.charAt( i ) ) 538 { 539 case '\\': 540 buff.setCharAt( i, '/' ); 541 case '/': 542 if( lastSlash == i + 1 ) 543 { 544 buff.deleteCharAt( i ); 545 } 546 547 switch( ptCount ) 548 { 549 case 1: 550 buff.delete( i, lastSlash ); 551 break; 552 553 case 2: 554 upLevel++; 555 break; 556 557 default: 558 if( upLevel > 0 && lastSlash != i + 1 ) 559 { 560 buff.delete( i, lastSlash + 3 ); 561 upLevel--; 562 } 563 break; 564 } 565 566 ptCount = 0; 567 expStart = true; 568 lastSlash = i; 569 break; 570 571 case '.': 572 if( expStart ) 573 { 574 ptCount++; 575 } 576 break; 577 578 default: 579 ptCount = 0; 580 expStart = false; 581 break; 582 } 583 584 switch( ptCount ) 585 { 586 case 1: 587 buff.delete( 0, lastSlash ); 588 break; 589 590 case 2: 591 break; 592 593 default: 594 if( upLevel > 0 ) 595 { 596 if( startsWithSlash ) 597 { 598 return null; 599 } 600 else 601 { 602 upLevel = 1; 603 } 604 } 605 606 while( upLevel > 0 ) 607 { 608 buff.delete( 0, lastSlash + 3 ); 609 upLevel--; 610 } 611 break; 612 } 613 614 length = buff.length(); 615 boolean isLengthNull = length == 0; 616 char firstChar = isLengthNull?(char)0:buff.charAt( 0 ); 617 618 if( !startsWithSlash && !isLengthNull && firstChar == '/' ) 619 { 620 buff.deleteCharAt( 0 ); 621 } 622 else if( startsWithSlash && 623 ( isLengthNull || ( !isLengthNull && firstChar != '/' ) ) ) 624 { 625 buff.insert( 0, '/' ); 626 } 627 628 if( prefix != null ) 629 { 630 buff.insert( 0, prefix ); 631 } 632 633 return buff.toString(); 634 } 635 636 655 public static String catPath( String lookupPath, 656 final String path ) 657 { 658 if( path == null ) 659 { 660 throw new NullPointerException ( "path" ); 661 } 662 663 if( !lookupPath.endsWith( "/" ) ) 664 { 665 final int index = lookupPath.lastIndexOf( "/" ); 666 if( index < 0 ) 667 { 668 lookupPath = ""; 669 } 670 else 671 { 672 lookupPath = lookupPath.substring( 0, index + 1 ); 673 } 674 } 675 676 return normalize( lookupPath + path ); 677 } 678 679 689 public static File resolveFile( final File baseFile, String filename ) 690 { 691 String filenm = filename; 692 if( '/' != File.separatorChar ) 693 { 694 filenm = filename.replace( '/', File.separatorChar ); 695 } 696 697 if( '\\' != File.separatorChar ) 698 { 699 filenm = filename.replace( '\\', File.separatorChar ); 700 } 701 702 if( filenm.startsWith( File.separator ) ) 704 { 705 File file = new File ( filenm ); 706 707 try 708 { 709 file = file.getCanonicalFile(); 710 } 711 catch( final IOException ioe ) 712 { 713 } 714 715 return file; 716 } 717 718 final char[] chars = filename.toCharArray(); 719 final StringBuffer sb = new StringBuffer (); 720 721 int start = 0; 725 if( '\\' == File.separatorChar ) 726 { 727 sb.append( filenm.charAt( 0 ) ); 728 start++; 729 } 730 731 for( int i = start; i < chars.length; i++ ) 732 { 733 final boolean doubleSeparator = 734 File.separatorChar == chars[ i ] && File.separatorChar == chars[ i - 1 ]; 735 736 if( !doubleSeparator ) 737 { 738 sb.append( chars[ i ] ); 739 } 740 } 741 742 filenm = sb.toString(); 743 744 File file = ( new File ( baseFile, filenm ) ).getAbsoluteFile(); 746 747 try 748 { 749 file = file.getCanonicalFile(); 750 } 751 catch( final IOException ioe ) 752 { 753 } 754 755 return file; 756 } 757 758 761 public static void forceDelete( final String file ) 762 throws IOException 763 { 764 forceDelete( new File ( file ) ); 765 } 766 767 770 public static void forceDelete( final File file ) 771 throws IOException 772 { 773 if( file.isDirectory() ) 774 { 775 deleteDirectory( file ); 776 } 777 else 778 { 779 if( !file.delete() ) 780 { 781 final String message = 782 "File " + file + " unable to be deleted."; 783 throw new IOException ( message ); 784 } 785 } 786 } 787 788 792 public static void forceDeleteOnExit( final File file ) 793 throws IOException 794 { 795 if( file.isDirectory() ) 796 { 797 deleteDirectoryOnExit( file ); 798 } 799 else 800 { 801 file.deleteOnExit(); 802 } 803 } 804 805 808 private static void deleteDirectoryOnExit( final File directory ) 809 throws IOException 810 { 811 if( !directory.exists() ) 812 { 813 return; 814 } 815 816 cleanDirectoryOnExit( directory ); 817 directory.deleteOnExit(); 818 } 819 820 823 private static void cleanDirectoryOnExit( final File directory ) 824 throws IOException 825 { 826 if( !directory.exists() ) 827 { 828 final String message = directory + " does not exist"; 829 throw new IllegalArgumentException ( message ); 830 } 831 832 if( !directory.isDirectory() ) 833 { 834 final String message = directory + " is not a directory"; 835 throw new IllegalArgumentException ( message ); 836 } 837 838 IOException exception = null; 839 840 final File [] files = directory.listFiles(); 841 for( int i = 0; i < files.length; i++ ) 842 { 843 final File file = files[ i ]; 844 try 845 { 846 FileUtil.forceDeleteOnExit( file ); 847 } 848 catch( final IOException ioe ) 849 { 850 exception = ioe; 851 } 852 } 853 854 if( null != exception ) 855 { 856 throw exception; 857 } 858 } 859 860 864 public static void forceMkdir( final File file ) 865 throws IOException 866 { 867 if( file.exists() ) 868 { 869 if( file.isFile() ) 870 { 871 final String message = "File " + file + " exists and is " + 872 "not a directory. Unable to create directory."; 873 throw new IOException ( message ); 874 } 875 } 876 else 877 { 878 if( false == file.mkdirs() ) 879 { 880 final String message = "Unable to create directory " + file; 881 throw new IOException ( message ); 882 } 883 } 884 } 885 886 889 public static void deleteDirectory( final String directory ) 890 throws IOException 891 { 892 deleteDirectory( new File ( directory ) ); 893 } 894 895 898 public static void deleteDirectory( final File directory ) 899 throws IOException 900 { 901 if( !directory.exists() ) 902 { 903 return; 904 } 905 906 cleanDirectory( directory ); 907 if( !directory.delete() ) 908 { 909 final String message = 910 "Directory " + directory + " unable to be deleted."; 911 throw new IOException ( message ); 912 } 913 } 914 915 918 public static void cleanDirectory( final String directory ) 919 throws IOException 920 { 921 cleanDirectory( new File ( directory ) ); 922 } 923 924 927 public static void cleanDirectory( final File directory ) 928 throws IOException 929 { 930 if( !directory.exists() ) 931 { 932 final String message = directory + " does not exist"; 933 throw new IllegalArgumentException ( message ); 934 } 935 936 if( !directory.isDirectory() ) 937 { 938 final String message = directory + " is not a directory"; 939 throw new IllegalArgumentException ( message ); 940 } 941 942 IOException exception = null; 943 944 final File [] files = directory.listFiles(); 945 for( int i = 0; i < files.length; i++ ) 946 { 947 final File file = files[ i ]; 948 try 949 { 950 FileUtil.forceDelete( file ); 951 } 952 catch( final IOException ioe ) 953 { 954 exception = ioe; 955 } 956 } 957 958 if( null != exception ) 959 { 960 throw exception; 961 } 962 } 963 964 969 public static long sizeOfDirectory( final String directory ) 970 { 971 return sizeOfDirectory( new File ( directory ) ); 972 } 973 974 979 public static long sizeOfDirectory( final File directory ) 980 { 981 if( !directory.exists() ) 982 { 983 final String message = directory + " does not exist"; 984 throw new IllegalArgumentException ( message ); 985 } 986 987 if( !directory.isDirectory() ) 988 { 989 final String message = directory + " is not a directory"; 990 throw new IllegalArgumentException ( message ); 991 } 992 993 long size = 0; 994 995 final File [] files = directory.listFiles(); 996 for( int i = 0; i < files.length; i++ ) 997 { 998 final File file = files[ i ]; 999 1000 if( file.isDirectory() ) 1001 { 1002 size += sizeOfDirectory( file ); 1003 } 1004 else 1005 { 1006 size += file.length(); 1007 } 1008 } 1009 1010 return size; 1011 } 1012} 1013 | Popular Tags |