1 package org.apache.maven.shared.utils.io;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.shared.utils.Os;
23 import org.apache.maven.shared.utils.StringUtils;
24
25 import javax.annotation.Nonnull;
26 import javax.annotation.Nullable;
27 import javax.annotation.WillClose;
28
29 import java.io.BufferedReader;
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.FileOutputStream;
33 import java.io.FileReader;
34 import java.io.FileWriter;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.InputStreamReader;
38 import java.io.OutputStream;
39 import java.io.OutputStreamWriter;
40 import java.io.Reader;
41 import java.io.Writer;
42 import java.net.URL;
43 import java.nio.channels.FileChannel;
44 import java.security.SecureRandom;
45 import java.text.DecimalFormat;
46 import java.util.ArrayList;
47 import java.util.Arrays;
48 import java.util.Collections;
49 import java.util.List;
50 import java.util.Random;
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89 public class FileUtils
90 {
91
92
93
94 protected FileUtils()
95 {
96
97 }
98
99
100
101
102 private static final int ONE_KB = 1024;
103
104
105
106
107 private static final int ONE_MB = ONE_KB * ONE_KB;
108
109
110
111
112 private static final int ONE_GB = ONE_KB * ONE_MB;
113
114
115
116
117 private static final long FILE_COPY_BUFFER_SIZE = ONE_MB * 30;
118
119
120
121
122 private static final String FS = System.getProperty( "file.separator" );
123
124
125
126
127
128
129
130 private static final String[] INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME = { ":", "*", "?", "\"", "<", ">", "|" };
131
132
133
134
135
136 @Nonnull public static String[] getDefaultExcludes()
137 {
138 return DirectoryScanner.DEFAULTEXCLUDES;
139 }
140
141
142
143
144
145 @Nonnull public static List<String> getDefaultExcludesAsList()
146 {
147 return Arrays.asList( getDefaultExcludes() );
148 }
149
150
151
152
153
154
155 @Nonnull public static String getDefaultExcludesAsString()
156 {
157 return StringUtils.join( DirectoryScanner.DEFAULTEXCLUDES, "," );
158 }
159
160
161
162
163
164
165
166
167 @Nonnull public static String dirname( @Nonnull String filename )
168 {
169 int i = filename.lastIndexOf( File.separator );
170 return ( i >= 0 ? filename.substring( 0, i ) : "" );
171 }
172
173
174
175
176
177
178
179 @Nonnull public static String filename( @Nonnull String filename )
180 {
181 int i = filename.lastIndexOf( File.separator );
182 return ( i >= 0 ? filename.substring( i + 1 ) : filename );
183 }
184
185
186
187
188
189
190
191
192
193 @Nonnull public static String extension( @Nonnull String filename )
194 {
195
196 int lastSep = filename.lastIndexOf( File.separatorChar );
197 int lastDot;
198 if ( lastSep < 0 )
199 {
200 lastDot = filename.lastIndexOf( '.' );
201 }
202 else
203 {
204 lastDot = filename.substring( lastSep + 1 ).lastIndexOf( '.' );
205 if ( lastDot >= 0 )
206 {
207 lastDot += lastSep + 1;
208 }
209 }
210
211 if ( lastDot >= 0 && lastDot > lastSep )
212 {
213 return filename.substring( lastDot + 1 );
214 }
215
216 return "";
217 }
218
219
220
221
222
223
224
225 public static boolean fileExists( @Nonnull String fileName )
226 {
227 File file = new File( fileName );
228 return file.exists();
229 }
230
231
232
233
234
235
236
237
238 @Nonnull public static String fileRead( @Nonnull String file )
239 throws IOException
240 {
241 return fileRead( file, null );
242 }
243
244
245
246
247
248
249
250 @Nonnull private static String fileRead( @Nonnull String file, @Nullable String encoding )
251 throws IOException
252 {
253 return fileRead( new File( file ), encoding );
254 }
255
256
257
258
259
260
261
262
263 @Nonnull public static String fileRead( @Nonnull File file )
264 throws IOException
265 {
266 return fileRead( file, null );
267 }
268
269
270
271
272
273
274
275 @Nonnull public static String fileRead( @Nonnull File file, @Nullable String encoding )
276 throws IOException
277 {
278 StringBuilder buf = new StringBuilder();
279
280 Reader reader = null;
281
282 try
283 {
284 if ( encoding != null )
285 {
286 reader = new InputStreamReader( new FileInputStream( file ), encoding );
287 }
288 else
289 {
290 reader = new InputStreamReader( new FileInputStream( file ) );
291 }
292 int count;
293 char[] b = new char[512];
294 while ( ( count = reader.read( b ) ) >= 0 )
295 {
296 buf.append( b, 0, count );
297 }
298 reader.close();
299 reader = null;
300 }
301 finally
302 {
303 IOUtil.close( reader );
304 }
305
306 return buf.toString();
307 }
308
309
310
311
312
313
314
315 @Nonnull public static String[] fileReadArray( @Nonnull File file )
316 throws IOException
317 {
318 List<String> lines = loadFile( file );
319
320 return lines.toArray( new String[lines.size()] );
321 }
322
323
324
325
326
327
328
329
330
331 public static void fileAppend( @Nonnull String fileName, @Nonnull String data )
332 throws IOException
333 {
334 fileAppend( fileName, null, data );
335 }
336
337
338
339
340
341
342
343
344
345 public static void fileAppend( @Nonnull String fileName, @Nullable String encoding, @Nonnull String data )
346 throws IOException
347 {
348 FileOutputStream out = null;
349 try
350 {
351 out = new FileOutputStream( fileName, true );
352 if ( encoding != null )
353 {
354 out.write( data.getBytes( encoding ) );
355 }
356 else
357 {
358 out.write( data.getBytes() );
359 }
360 out.close();
361 out = null;
362 }
363 finally
364 {
365 IOUtil.close( out );
366 }
367 }
368
369
370
371
372
373
374
375
376
377 public static void fileWrite( @Nonnull String fileName, @Nonnull String data )
378 throws IOException
379 {
380 fileWrite( fileName, null, data );
381 }
382
383
384
385
386
387
388
389
390
391 public static void fileWrite( @Nonnull String fileName, @Nullable String encoding, @Nonnull String data )
392 throws IOException
393 {
394 File file = new File( fileName );
395 fileWrite( file, encoding, data );
396 }
397
398
399
400
401
402
403
404
405
406 public static void fileWrite( @Nonnull File file, @Nullable String encoding, @Nonnull String data )
407 throws IOException
408 {
409 Writer writer = null;
410 try
411 {
412 if ( encoding != null )
413 {
414 writer = new OutputStreamWriter( new FileOutputStream( file ), encoding );
415 }
416 else
417 {
418 writer = new OutputStreamWriter( new FileOutputStream( file ) );
419 }
420 writer.write( data );
421 writer.close();
422 writer = null;
423 }
424 finally
425 {
426 IOUtil.close( writer );
427 }
428 }
429
430
431
432
433
434
435
436
437
438 public static void fileWriteArray( @Nonnull File file, @Nullable String... data )
439 throws IOException
440 {
441 fileWriteArray( file, null, data );
442 }
443
444
445
446
447
448
449
450
451
452 public static void fileWriteArray( @Nonnull File file, @Nullable String encoding, @Nullable String... data )
453 throws IOException
454 {
455 Writer writer = null;
456 try
457 {
458 if ( encoding != null )
459 {
460 writer = new OutputStreamWriter( new FileOutputStream( file ), encoding );
461 }
462 else
463 {
464 writer = new OutputStreamWriter( new FileOutputStream( file ) );
465 }
466
467 for ( int i = 0; data != null && i < data.length; i++ )
468 {
469 writer.write( data[i] );
470 if ( i < data.length )
471 {
472 writer.write( "\n" );
473 }
474 }
475
476 writer.close();
477 writer = null;
478 }
479 finally
480 {
481 IOUtil.close( writer );
482 }
483 }
484
485
486
487
488
489
490 public static void fileDelete( @Nonnull String fileName )
491 {
492 File file = new File( fileName );
493
494 deleteLegacyStyle( file );
495 }
496
497
498
499
500
501
502
503
504
505
506
507
508
509 public static String[] getFilesFromExtension( @Nonnull String directory, @Nonnull String... extensions )
510 {
511 List<String> files = new ArrayList<String>();
512
513 File currentDir = new File( directory );
514
515 String[] unknownFiles = currentDir.list();
516
517 if ( unknownFiles == null )
518 {
519 return new String[0];
520 }
521
522 for ( String unknownFile : unknownFiles )
523 {
524 String currentFileName = directory + System.getProperty( "file.separator" ) + unknownFile;
525 File currentFile = new File( currentFileName );
526
527 if ( currentFile.isDirectory() )
528 {
529
530 if ( currentFile.getName().equals( "CVS" ) )
531 {
532 continue;
533 }
534
535
536
537
538 String[] fetchFiles = getFilesFromExtension( currentFileName, extensions );
539 files = blendFilesToList( files, fetchFiles );
540 }
541 else
542 {
543
544
545 String add = currentFile.getAbsolutePath();
546 if ( isValidFile( add, extensions ) )
547 {
548 files.add( add );
549 }
550 }
551 }
552
553
554 String[] foundFiles = new String[files.size()];
555 files.toArray( foundFiles );
556
557 return foundFiles;
558 }
559
560
561
562
563 @Nonnull private static List<String> blendFilesToList( @Nonnull List<String> v, @Nonnull String... files )
564 {
565 Collections.addAll( v, files );
566
567 return v;
568 }
569
570
571
572
573
574
575 private static boolean isValidFile( @Nonnull String file, @Nonnull String... extensions )
576 {
577 String extension = extension( file );
578
579
580
581
582 for ( String extension1 : extensions )
583 {
584 if ( extension1.equals( extension ) )
585 {
586 return true;
587 }
588 }
589
590 return false;
591
592 }
593
594
595
596
597
598
599
600
601 public static void mkdir( @Nonnull String dir )
602 {
603 File file = new File( dir );
604
605 if ( Os.isFamily( Os.FAMILY_WINDOWS ) && !isValidWindowsFileName( file ) )
606 {
607 throw new IllegalArgumentException(
608 "The file (" + dir + ") cannot contain any of the following characters: \n" + StringUtils.join(
609 INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME, " " ) );
610 }
611
612 if ( !file.exists() )
613 {
614
615 file.mkdirs();
616 }
617 }
618
619
620
621
622
623
624
625
626
627 public static boolean contentEquals( @Nonnull final File file1, @Nonnull final File file2 )
628 throws IOException
629 {
630 final boolean file1Exists = file1.exists();
631 if ( file1Exists != file2.exists() )
632 {
633 return false;
634 }
635
636 if ( !file1Exists )
637 {
638
639 return true;
640 }
641
642 if ( file1.isDirectory() || file2.isDirectory() )
643 {
644
645 return false;
646 }
647
648 InputStream input1 = null;
649 InputStream input2 = null;
650 boolean equals = false;
651 try
652 {
653 input1 = new FileInputStream( file1 );
654 input2 = new FileInputStream( file2 );
655 equals = IOUtil.contentEquals( input1, input2 );
656 input1.close();
657 input1 = null;
658 input2.close();
659 input2 = null;
660 }
661 finally
662 {
663 IOUtil.close( input1 );
664 IOUtil.close( input2 );
665 }
666 return equals;
667 }
668
669
670
671
672
673
674
675
676 @Nullable public static File toFile( @Nullable final URL url )
677 {
678 if ( url == null || !url.getProtocol().equalsIgnoreCase( "file" ) )
679 {
680 return null;
681 }
682
683 String filename = url.getFile().replace( '/', File.separatorChar );
684 int pos = -1;
685 while ( ( pos = filename.indexOf( '%', pos + 1 ) ) >= 0 )
686 {
687 if ( pos + 2 < filename.length() )
688 {
689 String hexStr = filename.substring( pos + 1, pos + 3 );
690 char ch = (char) Integer.parseInt( hexStr, 16 );
691 filename = filename.substring( 0, pos ) + ch + filename.substring( pos + 3 );
692 }
693 }
694 return new File( filename );
695 }
696
697
698
699
700
701
702
703
704 @Nonnull public static URL[] toURLs( @Nonnull final File... files )
705 throws IOException
706 {
707 final URL[] urls = new URL[files.length];
708
709 for ( int i = 0; i < urls.length; i++ )
710 {
711 urls[i] = files[i].toURI().toURL();
712 }
713
714 return urls;
715 }
716
717
718
719
720
721
722
723
724
725
726
727
728
729 @Nonnull public static String removeExtension( @Nonnull final String filename )
730 {
731 String ext = extension( filename );
732
733 if ( "".equals( ext ) )
734 {
735 return filename;
736 }
737
738 final int index = filename.lastIndexOf( ext ) - 1;
739 return filename.substring( 0, index );
740 }
741
742
743
744
745
746
747
748
749
750
751
752
753
754 @Nonnull public static String getExtension( @Nonnull final String filename )
755 {
756 return extension( filename );
757 }
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772 public static void copyFileToDirectory( @Nonnull final File source, @Nonnull final File destinationDirectory )
773 throws IOException
774 {
775 if ( destinationDirectory.exists() && !destinationDirectory.isDirectory() )
776 {
777 throw new IllegalArgumentException( "Destination is not a directory" );
778 }
779
780 copyFile( source, new File( destinationDirectory, source.getName() ) );
781 }
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797 private static void copyFileToDirectoryIfModified( @Nonnull final File source,
798 @Nonnull final File destinationDirectory )
799 throws IOException
800 {
801 if ( destinationDirectory.exists() && !destinationDirectory.isDirectory() )
802 {
803 throw new IllegalArgumentException( "Destination is not a directory" );
804 }
805
806 copyFileIfModified( source, new File( destinationDirectory, source.getName() ) );
807 }
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822 public static void copyFile( @Nonnull final File source, @Nonnull final File destination )
823 throws IOException
824 {
825
826 if ( !source.exists() )
827 {
828 final String message = "File " + source + " does not exist";
829 throw new IOException( message );
830 }
831 if ( Java7Support.isAtLeastJava7() && Java7Support.isSymLink( source ) )
832 {
833 File target = Java7Support.readSymbolicLink( source );
834 Java7Support.createSymbolicLink( destination, target );
835 return;
836 }
837
838
839 if ( source.getCanonicalPath().equals( destination.getCanonicalPath() ) )
840 {
841
842 return;
843 }
844
845 mkdirsFor( destination );
846
847 doCopyFile( source, destination );
848
849 if ( source.length() != destination.length() )
850 {
851 final String message = "Failed to copy full contents from " + source + " to " + destination;
852 throw new IOException( message );
853 }
854 }
855
856 private static void mkdirsFor( @Nonnull File destination )
857 {
858
859 if ( destination.getParentFile() != null && !destination.getParentFile().exists() )
860 {
861
862 destination.getParentFile().mkdirs();
863 }
864 }
865
866 private static void doCopyFile( @Nonnull File source, @Nonnull File destination )
867 throws IOException
868 {
869 FileInputStream fis = null;
870 FileOutputStream fos = null;
871 FileChannel input = null;
872 FileChannel output = null;
873 try
874 {
875 fis = new FileInputStream( source );
876 fos = new FileOutputStream( destination );
877 input = fis.getChannel();
878 output = fos.getChannel();
879 long size = input.size();
880 long pos = 0;
881 long count;
882 while ( pos < size )
883 {
884 count = size - pos > FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : size - pos;
885 pos += output.transferFrom( input, pos, count );
886 }
887 output.close();
888 output = null;
889 fos.close();
890 fos = null;
891 input.close();
892 input = null;
893 fis.close();
894 fis = null;
895 }
896 finally
897 {
898 IOUtil.close( output );
899 IOUtil.close( fos );
900 IOUtil.close( input );
901 IOUtil.close( fis );
902 }
903 }
904
905
906
907
908
909
910
911
912
913
914
915
916
917 private static boolean copyFileIfModified( @Nonnull final File source, @Nonnull final File destination )
918 throws IOException
919 {
920 if ( destination.lastModified() < source.lastModified() )
921 {
922 copyFile( source, destination );
923
924 return true;
925 }
926
927 return false;
928 }
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945 public static void copyURLToFile( @Nonnull final URL source, @Nonnull final File destination )
946 throws IOException
947 {
948 copyStreamToFile( source.openStream(), destination );
949 }
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967 private static void copyStreamToFile( @Nonnull @WillClose final InputStream source,
968 @Nonnull final File destination )
969 throws IOException
970 {
971 InputStream in = source;
972 OutputStream out = null;
973 try
974 {
975
976 if ( destination.getParentFile() != null && !destination.getParentFile().exists() )
977 {
978
979 destination.getParentFile().mkdirs();
980 }
981
982
983 if ( destination.exists() && !destination.canWrite() )
984 {
985 final String message = "Unable to open file " + destination + " for writing.";
986 throw new IOException( message );
987 }
988
989 out = new FileOutputStream( destination );
990 IOUtil.copy( in, out );
991 out.close();
992 out = null;
993 in.close();
994 in = null;
995 }
996 finally
997 {
998 IOUtil.close( out );
999 IOUtil.close( in );
1000 }
1001 }
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021 @Nonnull public static String normalize( @Nonnull final String path )
1022 {
1023 String normalized = path;
1024
1025 while ( true )
1026 {
1027 int index = normalized.indexOf( "//" );
1028 if ( index < 0 )
1029 {
1030 break;
1031 }
1032 normalized = normalized.substring( 0, index ) + normalized.substring( index + 1 );
1033 }
1034
1035
1036 while ( true )
1037 {
1038 int index = normalized.indexOf( "/./" );
1039 if ( index < 0 )
1040 {
1041 break;
1042 }
1043 normalized = normalized.substring( 0, index ) + normalized.substring( index + 2 );
1044 }
1045
1046
1047 while ( true )
1048 {
1049 int index = normalized.indexOf( "/../" );
1050 if ( index < 0 )
1051 {
1052 break;
1053 }
1054 if ( index == 0 )
1055 {
1056 return null;
1057 }
1058 int index2 = normalized.lastIndexOf( '/', index - 1 );
1059 normalized = normalized.substring( 0, index2 ) + normalized.substring( index + 3 );
1060 }
1061
1062
1063 return normalized;
1064 }
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076 @Nonnull public static File resolveFile( final File baseFile, @Nonnull String filename )
1077 {
1078 String filenm = filename;
1079 if ( '/' != File.separatorChar )
1080 {
1081 filenm = filename.replace( '/', File.separatorChar );
1082 }
1083
1084 if ( '\\' != File.separatorChar )
1085 {
1086 filenm = filename.replace( '\\', File.separatorChar );
1087 }
1088
1089
1090 if ( filenm.startsWith( File.separator ) || ( Os.isFamily( Os.FAMILY_WINDOWS ) && filenm.indexOf( ":" ) > 0 ) )
1091 {
1092 File file = new File( filenm );
1093
1094 try
1095 {
1096 file = file.getCanonicalFile();
1097 }
1098 catch ( final IOException ioe )
1099 {
1100
1101 }
1102
1103 return file;
1104 }
1105
1106
1107 final char[] chars = filename.toCharArray();
1108 final StringBuilder sb = new StringBuilder();
1109
1110
1111
1112
1113 int start = 0;
1114 if ( '\\' == File.separatorChar )
1115 {
1116 sb.append( filenm.charAt( 0 ) );
1117 start++;
1118 }
1119
1120 for ( int i = start; i < chars.length; i++ )
1121 {
1122 final boolean doubleSeparator = File.separatorChar == chars[i] && File.separatorChar == chars[i - 1];
1123
1124 if ( !doubleSeparator )
1125 {
1126 sb.append( chars[i] );
1127 }
1128 }
1129
1130 filenm = sb.toString();
1131
1132
1133 File file = ( new File( baseFile, filenm ) ).getAbsoluteFile();
1134
1135 try
1136 {
1137 file = file.getCanonicalFile();
1138 }
1139 catch ( final IOException ioe )
1140 {
1141
1142 }
1143
1144 return file;
1145 }
1146
1147
1148
1149
1150
1151
1152
1153 public static void forceDelete( @Nonnull final String file )
1154 throws IOException
1155 {
1156 forceDelete( new File( file ) );
1157 }
1158
1159
1160
1161
1162
1163
1164
1165 public static void forceDelete( @Nonnull final File file )
1166 throws IOException
1167 {
1168 if ( file.isDirectory() )
1169 {
1170 deleteDirectory( file );
1171 }
1172 else
1173 {
1174
1175
1176
1177
1178 boolean filePresent = file.getCanonicalFile().exists();
1179 if ( !deleteFile( file ) && filePresent )
1180 {
1181 final String message = "File " + file + " unable to be deleted.";
1182 throw new IOException( message );
1183 }
1184 }
1185 }
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195 public static void delete( @Nonnull File file )
1196 throws IOException
1197 {
1198 if ( Java7Support.isAtLeastJava7() )
1199 {
1200 Java7Support.delete( file );
1201 }
1202 else
1203 {
1204 if ( !file.delete() )
1205 {
1206 throw new IOException( "Could not delete " + file.getName() );
1207 }
1208 }
1209 }
1210
1211
1212
1213
1214
1215 public static boolean deleteLegacyStyle( @Nonnull File file )
1216 {
1217 if ( Java7Support.isAtLeastJava7() )
1218 {
1219 try
1220 {
1221 Java7Support.delete( file );
1222 return true;
1223 }
1224 catch ( IOException e )
1225 {
1226 return false;
1227 }
1228 }
1229 else
1230 {
1231 return file.delete();
1232 }
1233 }
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243 private static boolean deleteFile( @Nonnull File file )
1244 throws IOException
1245 {
1246 if ( file.isDirectory() )
1247 {
1248 throw new IOException( "File " + file + " isn't a file." );
1249 }
1250
1251 if ( !deleteLegacyStyle( file ) )
1252 {
1253 if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
1254 {
1255 file = file.getCanonicalFile();
1256 }
1257
1258 try
1259 {
1260 Thread.sleep( 10 );
1261 return deleteLegacyStyle( file );
1262 }
1263 catch ( InterruptedException ex )
1264 {
1265 return deleteLegacyStyle( file );
1266 }
1267 }
1268
1269 return true;
1270 }
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282 public static void forceMkdir( @Nonnull final File file )
1283 throws IOException
1284 {
1285 if ( Os.isFamily( Os.FAMILY_WINDOWS ) && !isValidWindowsFileName( file ) )
1286 {
1287 throw new IllegalArgumentException(
1288 "The file (" + file.getAbsolutePath() + ") cannot contain any of the following characters: \n"
1289 + StringUtils.join( INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME, " " ) );
1290 }
1291
1292 if ( file.exists() )
1293 {
1294 if ( file.isFile() )
1295 {
1296 final String message =
1297 "File " + file + " exists and is " + "not a directory. Unable to create directory.";
1298 throw new IOException( message );
1299 }
1300 }
1301 else
1302 {
1303 if ( !file.mkdirs() )
1304 {
1305 final String message = "Unable to create directory " + file;
1306 throw new IOException( message );
1307 }
1308 }
1309 }
1310
1311
1312
1313
1314
1315
1316
1317 public static void deleteDirectory( @Nonnull final String directory )
1318 throws IOException
1319 {
1320 deleteDirectory( new File( directory ) );
1321 }
1322
1323
1324
1325
1326
1327
1328
1329 public static void deleteDirectory( @Nonnull final File directory )
1330 throws IOException
1331 {
1332 if ( !directory.exists() )
1333 {
1334 return;
1335 }
1336
1337
1338
1339
1340 if ( deleteLegacyStyle( directory ) )
1341 {
1342 return;
1343 }
1344
1345 cleanDirectory( directory );
1346 if ( !deleteLegacyStyle( directory ) )
1347 {
1348 final String message = "Directory " + directory + " unable to be deleted.";
1349 throw new IOException( message );
1350 }
1351 }
1352
1353
1354
1355
1356
1357
1358
1359 public static void cleanDirectory( @Nonnull final File directory )
1360 throws IOException
1361 {
1362 if ( !directory.exists() )
1363 {
1364 final String message = directory + " does not exist";
1365 throw new IllegalArgumentException( message );
1366 }
1367
1368 if ( !directory.isDirectory() )
1369 {
1370 final String message = directory + " is not a directory";
1371 throw new IllegalArgumentException( message );
1372 }
1373
1374 IOException exception = null;
1375
1376 final File[] files = directory.listFiles();
1377
1378 if ( files == null )
1379 {
1380 return;
1381 }
1382
1383 for ( final File file : files )
1384 {
1385 try
1386 {
1387 forceDelete( file );
1388 }
1389 catch ( final IOException ioe )
1390 {
1391 exception = ioe;
1392 }
1393 }
1394
1395 if ( null != exception )
1396 {
1397 throw exception;
1398 }
1399 }
1400
1401
1402
1403
1404
1405
1406
1407 public static long sizeOfDirectory( @Nonnull final String directory )
1408 {
1409 return sizeOfDirectory( new File( directory ) );
1410 }
1411
1412
1413
1414
1415
1416
1417
1418 public static long sizeOfDirectory( @Nonnull final File directory )
1419 {
1420 if ( !directory.exists() )
1421 {
1422 final String message = directory + " does not exist";
1423 throw new IllegalArgumentException( message );
1424 }
1425
1426 if ( !directory.isDirectory() )
1427 {
1428 final String message = directory + " is not a directory";
1429 throw new IllegalArgumentException( message );
1430 }
1431
1432 long size = 0;
1433
1434 final File[] files = directory.listFiles();
1435 if ( files == null )
1436 {
1437 throw new IllegalArgumentException( "Problems reading directory" );
1438 }
1439
1440 for ( final File file : files )
1441 {
1442 if ( file.isDirectory() )
1443 {
1444 size += sizeOfDirectory( file );
1445 }
1446 else
1447 {
1448 size += file.length();
1449 }
1450 }
1451
1452 return size;
1453 }
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466 @Nonnull
1467 public static List<File> getFiles( @Nonnull File directory, @Nullable String includes, @Nullable String excludes )
1468 throws IOException
1469 {
1470 return getFiles( directory, includes, excludes, true );
1471 }
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484 @Nonnull
1485 public static List<File> getFiles( @Nonnull File directory, @Nullable String includes, @Nullable String excludes,
1486 boolean includeBasedir )
1487 throws IOException
1488 {
1489 List<String> fileNames = getFileNames( directory, includes, excludes, includeBasedir );
1490
1491 List<File> files = new ArrayList<File>();
1492
1493 for ( String filename : fileNames )
1494 {
1495 files.add( new File( filename ) );
1496 }
1497
1498 return files;
1499 }
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512 @Nonnull public static List<String> getFileNames( @Nonnull File directory, @Nullable String includes,
1513 @Nullable String excludes, boolean includeBasedir )
1514 throws IOException
1515 {
1516 return getFileNames( directory, includes, excludes, includeBasedir, true );
1517 }
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530 @Nonnull private static List<String> getFileNames( @Nonnull File directory, @Nullable String includes,
1531 @Nullable String excludes, boolean includeBasedir,
1532 boolean isCaseSensitive )
1533 throws IOException
1534 {
1535 return getFileAndDirectoryNames( directory, includes, excludes, includeBasedir, isCaseSensitive, true, false );
1536 }
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549 @Nonnull public static List<String> getDirectoryNames( @Nonnull File directory, @Nullable String includes,
1550 @Nullable String excludes, boolean includeBasedir )
1551 throws IOException
1552 {
1553 return getDirectoryNames( directory, includes, excludes, includeBasedir, true );
1554 }
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567 @Nonnull public static List<String> getDirectoryNames( @Nonnull File directory, @Nullable String includes,
1568 @Nullable String excludes, boolean includeBasedir,
1569 boolean isCaseSensitive )
1570 throws IOException
1571 {
1572 return getFileAndDirectoryNames( directory, includes, excludes, includeBasedir, isCaseSensitive, false, true );
1573 }
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587 @Nonnull public static List<String> getFileAndDirectoryNames( File directory, @Nullable String includes,
1588 @Nullable String excludes, boolean includeBasedir,
1589 boolean isCaseSensitive, boolean getFiles,
1590 boolean getDirectories )
1591 {
1592 DirectoryScanner scanner = new DirectoryScanner();
1593
1594 scanner.setBasedir( directory );
1595
1596 if ( includes != null )
1597 {
1598 scanner.setIncludes( StringUtils.split( includes, "," ) );
1599 }
1600
1601 if ( excludes != null )
1602 {
1603 scanner.setExcludes( StringUtils.split( excludes, "," ) );
1604 }
1605
1606 scanner.setCaseSensitive( isCaseSensitive );
1607
1608 scanner.scan();
1609
1610 List<String> list = new ArrayList<String>();
1611
1612 if ( getFiles )
1613 {
1614 String[] files = scanner.getIncludedFiles();
1615
1616 for ( String file : files )
1617 {
1618 if ( includeBasedir )
1619 {
1620 list.add( directory + FileUtils.FS + file );
1621 }
1622 else
1623 {
1624 list.add( file );
1625 }
1626 }
1627 }
1628
1629 if ( getDirectories )
1630 {
1631 String[] directories = scanner.getIncludedDirectories();
1632
1633 for ( String directory1 : directories )
1634 {
1635 if ( includeBasedir )
1636 {
1637 list.add( directory + FileUtils.FS + directory1 );
1638 }
1639 else
1640 {
1641 list.add( directory1 );
1642 }
1643 }
1644 }
1645
1646 return list;
1647 }
1648
1649
1650
1651
1652
1653
1654
1655
1656 public static void copyDirectory( @Nonnull File sourceDirectory, @Nonnull File destinationDirectory )
1657 throws IOException
1658 {
1659 copyDirectory( sourceDirectory, destinationDirectory, "**", null );
1660 }
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672 public static void copyDirectory( @Nonnull File sourceDirectory, @Nonnull File destinationDirectory,
1673 @Nullable String includes, @Nullable String excludes )
1674 throws IOException
1675 {
1676 if ( !sourceDirectory.exists() )
1677 {
1678 return;
1679 }
1680
1681 List<File> files = getFiles( sourceDirectory, includes, excludes );
1682
1683 for ( File file : files )
1684 {
1685 copyFileToDirectory( file, destinationDirectory );
1686 }
1687 }
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702 public static void copyDirectoryStructure( @Nonnull File sourceDirectory, @Nonnull File destinationDirectory )
1703 throws IOException
1704 {
1705 copyDirectoryStructure( sourceDirectory, destinationDirectory, destinationDirectory, false );
1706 }
1707
1708 private static void copyDirectoryStructure( @Nonnull File sourceDirectory, @Nonnull File destinationDirectory,
1709 File rootDestinationDirectory, boolean onlyModifiedFiles )
1710 throws IOException
1711 {
1712
1713 if ( sourceDirectory == null )
1714 {
1715 throw new IOException( "source directory can't be null." );
1716 }
1717
1718
1719 if ( destinationDirectory == null )
1720 {
1721 throw new IOException( "destination directory can't be null." );
1722 }
1723
1724 if ( sourceDirectory.equals( destinationDirectory ) )
1725 {
1726 throw new IOException( "source and destination are the same directory." );
1727 }
1728
1729 if ( !sourceDirectory.exists() )
1730 {
1731 throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
1732 }
1733
1734 File[] files = sourceDirectory.listFiles();
1735
1736 if ( files == null )
1737 {
1738 return;
1739 }
1740
1741 String sourcePath = sourceDirectory.getAbsolutePath();
1742
1743 for ( File file : files )
1744 {
1745 if ( file.equals( rootDestinationDirectory ) )
1746 {
1747
1748 continue;
1749 }
1750
1751 String dest = file.getAbsolutePath();
1752
1753 dest = dest.substring( sourcePath.length() + 1 );
1754
1755 File destination = new File( destinationDirectory, dest );
1756
1757 if ( file.isFile() )
1758 {
1759 destination = destination.getParentFile();
1760
1761 if ( onlyModifiedFiles )
1762 {
1763 copyFileToDirectoryIfModified( file, destination );
1764 }
1765 else
1766 {
1767 copyFileToDirectory( file, destination );
1768 }
1769 }
1770 else if ( file.isDirectory() )
1771 {
1772 if ( !destination.exists() && !destination.mkdirs() )
1773 {
1774 throw new IOException(
1775 "Could not create destination directory '" + destination.getAbsolutePath() + "'." );
1776 }
1777
1778 copyDirectoryStructure( file, destination, rootDestinationDirectory, onlyModifiedFiles );
1779 }
1780 else
1781 {
1782 throw new IOException( "Unknown file type: " + file.getAbsolutePath() );
1783 }
1784 }
1785 }
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800 public static void rename( @Nonnull File from, @Nonnull File to )
1801 throws IOException
1802 {
1803 if ( to.exists() && !deleteLegacyStyle( to ) )
1804 {
1805 throw new IOException( "Failed to delete " + to + " while trying to rename " + from );
1806 }
1807
1808 File parent = to.getParentFile();
1809 if ( parent != null && !parent.exists() && !parent.mkdirs() )
1810 {
1811 throw new IOException( "Failed to create directory " + parent + " while trying to rename " + from );
1812 }
1813
1814 if ( !from.renameTo( to ) )
1815 {
1816 copyFile( from, to );
1817 if ( !deleteLegacyStyle( from ) )
1818 {
1819 throw new IOException( "Failed to delete " + from + " while trying to rename it." );
1820 }
1821 }
1822 }
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847 public static File createTempFile( @Nonnull String prefix, @Nonnull String suffix, @Nullable File parentDir )
1848 {
1849 File result;
1850 String parent = System.getProperty( "java.io.tmpdir" );
1851 if ( parentDir != null )
1852 {
1853 parent = parentDir.getPath();
1854 }
1855 DecimalFormat fmt = new DecimalFormat( "#####" );
1856 SecureRandom secureRandom = new SecureRandom();
1857 long secureInitializer = secureRandom.nextLong();
1858 Random rand = new Random( secureInitializer + Runtime.getRuntime().freeMemory() );
1859 do
1860 {
1861 result = new File( parent, prefix + fmt.format( positiveRandom( rand ) ) + suffix );
1862 }
1863 while ( result.exists() );
1864
1865 return result;
1866 }
1867
1868 private static int positiveRandom( Random rand )
1869 {
1870 int a = rand.nextInt();
1871 while ( a == Integer.MIN_VALUE )
1872 {
1873 a = rand.nextInt();
1874 }
1875 return Math.abs( a );
1876 }
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887 public static void copyFile( @Nonnull File from, @Nonnull File to, @Nullable String encoding,
1888 @Nullable FilterWrapper... wrappers )
1889 throws IOException
1890 {
1891 copyFile( from, to, encoding, wrappers, false );
1892 }
1893
1894
1895
1896
1897
1898 public abstract static class FilterWrapper
1899 {
1900
1901
1902
1903
1904 public abstract Reader getReader( Reader fileReader );
1905 }
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919 public static void copyFile( @Nonnull File from, @Nonnull File to, @Nullable String encoding,
1920 @Nullable FilterWrapper[] wrappers, boolean overwrite )
1921 throws IOException
1922 {
1923 if ( wrappers != null && wrappers.length > 0 )
1924 {
1925
1926 Reader fileReader = null;
1927 Writer fileWriter = null;
1928 try
1929 {
1930 if ( encoding == null || encoding.length() < 1 )
1931 {
1932 fileReader = new BufferedReader( new FileReader( from ) );
1933 fileWriter = new FileWriter( to );
1934 }
1935 else
1936 {
1937 fileReader = new BufferedReader( new InputStreamReader( new FileInputStream( from ), encoding ) );
1938 fileWriter = new OutputStreamWriter( new FileOutputStream( to ), encoding );
1939 }
1940
1941 for ( FilterWrapper wrapper : wrappers )
1942 {
1943 fileReader = wrapper.getReader( fileReader );
1944 }
1945
1946 IOUtil.copy( fileReader, fileWriter );
1947 fileWriter.close();
1948 fileWriter = null;
1949 fileReader.close();
1950 fileReader = null;
1951 }
1952 finally
1953 {
1954 IOUtil.close( fileReader );
1955 IOUtil.close( fileWriter );
1956 }
1957 }
1958 else
1959 {
1960 if ( to.lastModified() < from.lastModified() || overwrite )
1961 {
1962 copyFile( from, to );
1963 }
1964 }
1965 }
1966
1967
1968
1969
1970
1971
1972
1973
1974 @Nonnull public static List<String> loadFile( @Nonnull File file )
1975 throws IOException
1976 {
1977 List<String> lines = new ArrayList<String>();
1978
1979 if ( file.exists() )
1980 {
1981 BufferedReader reader = null;
1982 try
1983 {
1984 reader = new BufferedReader( new FileReader( file ) );
1985
1986 for ( String line = reader.readLine(); line != null; line = reader.readLine() )
1987 {
1988 line = line.trim();
1989
1990 if ( !line.startsWith( "#" ) && line.length() != 0 )
1991 {
1992 lines.add( line );
1993 }
1994 }
1995
1996 reader.close();
1997 reader = null;
1998 }
1999 finally
2000 {
2001 IOUtil.close( reader );
2002 }
2003 }
2004
2005 return lines;
2006 }
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017 private static boolean isValidWindowsFileName( @Nonnull File f )
2018 {
2019 if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
2020 {
2021 if ( StringUtils.indexOfAny( f.getName(), INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME ) != -1 )
2022 {
2023 return false;
2024 }
2025
2026 if ( f.getParentFile() != null )
2027 {
2028 return isValidWindowsFileName( f.getParentFile() );
2029 }
2030 }
2031
2032 return true;
2033 }
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050 public static boolean isSymbolicLink( @Nonnull final File file )
2051 throws IOException
2052 {
2053 if ( Java7Support.isAtLeastJava7() )
2054 {
2055 return Java7Support.isSymLink( file );
2056 }
2057 return isSymbolicLinkLegacy( file );
2058 }
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069 public static boolean isSymbolicLinkForSure( @Nonnull final File file )
2070 throws IOException
2071 {
2072 return Java7Support.isAtLeastJava7() && Java7Support.isSymLink( file );
2073 }
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093 static boolean isSymbolicLinkLegacy( @Nonnull final File file )
2094 throws IOException
2095 {
2096 final File canonical = new File( file.getCanonicalPath() );
2097 return !file.getAbsolutePath().equals( canonical.getPath() );
2098 }
2099
2100 }