1 16 17 package org.apache.taglibs.gnat.util; 20 21 import java.io.*; 22 import java.net.URL ; 23 24 29 public final class FileUtil 30 { 31 35 private FileUtil() 36 { 37 } 38 39 public static String removeExtention( final String filename ) 40 { 41 final int index = filename.lastIndexOf( '.' ); 42 43 if( -1 == index ) 44 { 45 return filename; 46 } 47 else 48 { 49 return filename.substring( 0, index ); 50 } 51 } 52 53 public static String removePath( final String filepath ) 54 { 55 final int index = filepath.lastIndexOf( File.separator ); 56 57 if( -1 == index ) 58 { 59 return filepath; 60 } 61 else 62 { 63 return filepath.substring( index + 1 ); 64 } 65 } 66 67 70 public static void copyFileToDirectory( final String source, 71 final String destinationDirectory ) 72 throws IOException 73 { 74 copyFileToDirectory( new File( source ), new File( destinationDirectory ) ); 75 } 76 77 80 public static void copyFileToDirectory( final File source, 81 final File destinationDirectory ) 82 throws IOException 83 { 84 if( destinationDirectory.exists() && !destinationDirectory.isDirectory() ) 85 { 86 throw new IllegalArgumentException ( "Destination is not a directory" ); 87 } 88 89 copyFile( source, new File( destinationDirectory, source.getName() ) ); 90 } 91 92 95 public static void copyFile( final File source, final File destination ) 96 throws IOException 97 { 98 if( !source.exists() ) 100 { 101 throw new IOException( "File " + source + " does not exist" ); 102 } 103 104 if( !destination.getParentFile().exists() ) 106 { 107 destination.mkdirs(); 108 } 109 110 if( destination.exists() && !destination.canWrite() ) 112 { 113 throw new IOException( "Unable to open file " + destination + " for writing." ); 114 } 115 116 copy( new FileInputStream( source ), new FileOutputStream( destination ) ); 117 118 if( source.length() != destination.length() ) 119 { 120 throw new IOException( "Failed to copy full contents from " + source + 121 " to " + destination ); 122 } 123 } 124 125 public static void copyURLToFile( final URL source, final File destination ) 126 throws IOException 127 { 128 if( !destination.getParentFile().exists() ) 130 { 131 destination.mkdirs(); 132 } 133 134 if( destination.exists() && !destination.canWrite() ) 136 { 137 throw new IOException( "Unable to open file " + destination + " for writing." ); 138 } 139 140 copy( source.openStream(), new FileOutputStream( destination ) ); 141 } 142 143 146 public static void copy( final InputStream source, final OutputStream destination ) 147 throws IOException 148 { 149 try 150 { 151 final BufferedInputStream input = new BufferedInputStream( source ); 152 final BufferedOutputStream output = new BufferedOutputStream( destination ); 153 154 final int BUFFER_SIZE = 1024 * 4; 155 final byte[] buffer = new byte[ BUFFER_SIZE ]; 156 157 while( true ) 158 { 159 final int count = input.read( buffer, 0, BUFFER_SIZE ); 160 if( -1 == count ) break; 161 162 output.write( buffer, 0, count ); 164 } 165 166 input.close(); 168 output.close(); 169 } 170 finally 171 { 172 try { source.close(); } catch( final IOException ioe ) {} 173 try { destination.close(); } catch( final IOException ioe ) {} 174 } 175 } 176 177 184 public static String catPath( String lookupPath, String path ) 185 { 186 int index = lookupPath.lastIndexOf( "/" ); 188 lookupPath = lookupPath.substring( 0, index ); 189 190 while( path.startsWith( "../" ) ) 192 { 193 if( lookupPath.length() > 0 ) 194 { 195 index = lookupPath.lastIndexOf( "/" ); 196 lookupPath = lookupPath.substring( 0, index ); 197 } 198 else 199 { 200 return null; 202 } 203 204 index = path.indexOf( "../" ) + 3; 205 path = path.substring( index ); 206 } 207 208 return lookupPath + "/" + path; 209 } 210 211 public static File resolveFile( final File baseFile, String filename ) 212 { 213 if( '/' != File.separatorChar ) 214 { 215 filename = filename.replace( '/', File.separatorChar ); 216 } 217 218 if( '\\' != File.separatorChar ) 219 { 220 filename = filename.replace( '\\', File.separatorChar ); 221 } 222 223 if( filename.startsWith( File.separator ) ) 225 { 226 File file = new File( filename ); 227 228 try { file = file.getCanonicalFile(); } 229 catch( final IOException ioe ) {} 230 231 return file; 232 } 233 234 final char[] chars = filename.toCharArray(); 235 final StringBuffer sb = new StringBuffer (); 236 237 int start = 0; 240 if( '\\' == File.separatorChar ) 241 { 242 sb.append( filename.charAt( 0 ) ); 243 start++; 244 } 245 246 for( int i = start; i < chars.length; i++ ) 247 { 248 final boolean doubleSeparator = 249 File.separatorChar == chars[ i ] && File.separatorChar == chars[ i - 1 ]; 250 251 if( !doubleSeparator ) sb.append( chars[ i ] ); 252 } 253 254 filename = sb.toString(); 255 256 File file = (new File( baseFile, filename )).getAbsoluteFile(); 258 259 try { file = file.getCanonicalFile(); } 260 catch( final IOException ioe ) {} 261 262 return file; 263 } 264 265 268 public static void forceDelete( final String file ) 269 throws IOException 270 { 271 forceDelete( new File( file ) ); 272 } 273 274 277 public static void forceDelete( final File file ) 278 throws IOException 279 { 280 if( file.isDirectory() ) deleteDirectory( file ); 281 else 282 { 283 if( false == file.delete() ) 284 { 285 throw new IOException( "File " + file + " unable to be deleted." ); 286 } 287 } 288 } 289 290 293 public static void deleteDirectory( final String directory ) 294 throws IOException 295 { 296 deleteDirectory( new File( directory ) ); 297 } 298 299 302 public static void deleteDirectory( final File directory ) 303 throws IOException 304 { 305 if( !directory.exists() ) return; 306 307 cleanDirectory( directory ); 308 if( false == directory.delete() ) 309 { 310 throw new IOException( "Directory " + directory + " unable to be deleted." ); 311 } 312 } 313 314 317 public static void cleanDirectory( final String directory ) 318 throws IOException 319 { 320 cleanDirectory( new File( directory ) ); 321 } 322 323 326 public static void cleanDirectory( final File directory ) 327 throws IOException 328 { 329 if( !directory.exists() ) 330 { 331 throw new IllegalArgumentException ( directory + " does not exist" ); 332 } 333 334 if( !directory.isDirectory() ) 335 { 336 throw new IllegalArgumentException ( directory + " is not a directory" ); 337 } 338 339 final File[] files = directory.listFiles(); 340 341 for( int i = 0; i < files.length; i++ ) 342 { 343 final File file = files[ i ]; 344 345 if( file.isFile() ) file.delete(); 346 else if( file.isDirectory() ) 347 { 348 cleanDirectory( file ); 349 if( false == file.delete() ) 350 { 351 throw new IOException( "Directory " + file + " unable to be deleted." ); 352 } 353 } 354 } 355 } 356 } 357 | Popular Tags |