| 1 22 23 package caramel.util; 24 25 import java.io.*; 26 import java.net.*; 27 import java.util.*; 28 import houston.*; 29 30 public class FileUtils 31 { 32 static Logger T = Logger.getLogger( FileUtils.class ); 33 34 38 public static String getBaseName( String name ) 39 { 40 String base = new File( name ).getName(); 42 int index = base.lastIndexOf( '.' ); 44 if( index != -1 ) 45 base = base.substring( 0, index ); 46 47 return base; 48 } 49 50 public static String getFileAsString( String name ) throws IOException 51 { 52 File file = new File( name ); 53 54 return getFileAsString( file ); 55 } 56 57 public static String getFileAsString( File file ) throws IOException 58 { 59 StringBuffer text = new StringBuffer (); 60 61 FileReader in = new FileReader( file ); 62 char buffer[] = new char[4096]; 63 int bytes_read; 64 while( ( bytes_read = in.read( buffer ) ) != -1 ) 65 text.append( new String ( buffer, 0, bytes_read ) ); 66 67 return text.toString(); 68 } 69 70 public static String getFileExtension( String filename ) 71 { 72 74 76 String extension = ""; 77 int index = filename.lastIndexOf( '.' ); 78 if( index != -1 ) 79 extension = filename.substring( index ); 80 81 return extension; 82 } 83 84 public static String getFileExtension( File file ) 85 { 86 88 String base = file.getName(); 90 92 String extension = ""; 93 int index = base.lastIndexOf( '.' ); 94 if( index != -1 ) 95 extension = base.substring( index ); 96 97 return extension; 98 } 99 100 public static String getFileExtension( String filename, boolean keepDot ) 101 { 102 filename = filename.replace( '\\', '/' ); 103 104 String name; 105 int namePos = filename.lastIndexOf( '/' ); 106 if( namePos != -1 ) 107 { 108 name = filename.substring( namePos + 1 ); 109 } 110 else 111 { 112 114 name = filename; 115 } 116 117 String ext; 118 int extPos = name.lastIndexOf( '.' ); 119 if( extPos != -1 ) 120 { 121 if( keepDot ) 122 ext = name.substring( extPos ); 123 else 124 ext = name.substring( extPos + 1 ); 125 } 126 else 127 { 128 130 ext = ""; 131 } 132 133 return ext; 134 } 135 136 public static String getFileExtension( File file, boolean keepDot ) 137 { 138 String base = file.getName(); 140 141 String extension = ""; 142 int index = base.lastIndexOf( '.' ); 143 if( index != -1 ) 144 { 145 if( keepDot ) 146 extension = base.substring( index ); 147 else 148 extension = base.substring( index + 1 ); 149 } 150 return extension; 151 } 152 153 public static String getInputStreamAsString( InputStream inStream ) throws IOException 154 { 155 157 StringBuffer text = new StringBuffer (); 158 159 InputStreamReader in = new InputStreamReader( inStream ); 160 char buffer[] = new char[4096]; 161 int bytes_read; 162 while( ( bytes_read = in.read( buffer ) ) != -1 ) 163 text.append( new String ( buffer, 0, bytes_read ) ); 164 165 return text.toString(); 166 } 167 168 public static File getRoot( File file ) 169 { 170 File parent = file; 171 while( parent.getParentFile() != null ) 172 parent = parent.getParentFile(); 173 174 return parent; 175 } 177 178 public static void copy( File from_file, File to_file ) throws IOException 179 { 180 if( !from_file.exists() ) 182 abort( "FileCopy: no such source file: " + from_file.getName() ); 183 184 if( !from_file.isFile() ) 185 abort( "FileCopy: can't copy directory: " + from_file.getName() ); 186 187 if( !from_file.canRead() ) 188 abort( "FileCopy: source file is unreadable: " + from_file.getName() ); 189 190 if( to_file.isDirectory() ) 193 to_file = new File( to_file, from_file.getName() ); 194 195 if( to_file.exists() ) 199 { 200 if( !to_file.canWrite() ) 201 abort( "FileCopy: destination file is unwriteable: " + to_file.getName() ); 202 } 203 else 204 { 205 String parent = to_file.getParent(); 209 if( parent == null ) 211 parent = System.getProperty( "user.dir" ); 212 213 File dir = new File( parent ); 215 if( !dir.exists() ) 217 abort( "FileCopy: destination directory doesn't exist: " + parent ); 218 219 if( dir.isFile() ) 220 abort( "FileCopy: destination is not a directory: " + parent ); 221 222 if( !dir.canWrite() ) 223 abort( "FileCopy: destination directory is unwriteable: " + parent ); 224 225 } 226 227 FileInputStream from = null; 230 FileOutputStream to = null; 232 try 234 { 235 from = new FileInputStream( from_file ); 236 to = new FileOutputStream( to_file ); 238 byte[] buffer = new byte[4096]; 240 int bytes_read; 242 while( ( bytes_read = from.read( buffer ) ) != -1 ) 248 { 249 to.write( buffer, 0, bytes_read ); 251 } 252 } 254 finally 256 { 257 if( from != null ) 258 { 259 try 260 { 261 from.close(); 262 } 263 catch( IOException e ) 264 { 265 ; 266 } 267 } 268 if( to != null ) 269 { 270 try 271 { 272 to.close(); 273 } 274 catch( IOException e ) 275 { 276 ; 277 } 278 } 279 } 280 } 281 282 287 public static void copy( String from_name, String to_name ) throws IOException 288 { 289 File from_file = new File( from_name ); 290 File to_file = new File( to_name ); 292 293 if( !from_file.exists() ) 295 abort( "FileCopy: no such source file: " + from_name ); 296 297 if( !from_file.isFile() ) 298 abort( "FileCopy: can't copy directory: " + from_name ); 299 300 if( !from_file.canRead() ) 301 abort( "FileCopy: source file is unreadable: " + from_name ); 302 303 if( to_file.isDirectory() ) 306 to_file = new File( to_file, from_file.getName() ); 307 308 if( to_file.exists() ) 312 { 313 if( !to_file.canWrite() ) 314 abort( "FileCopy: destination file is unwriteable: " + to_name ); 315 316 } 317 else 318 { 319 String parent = to_file.getParent(); 323 if( parent == null ) 325 parent = System.getProperty( "user.dir" ); 326 327 File dir = new File( parent ); 329 if( !dir.exists() ) 331 abort( "FileCopy: destination directory doesn't exist: " + parent ); 332 333 if( dir.isFile() ) 334 abort( "FileCopy: destination is not a directory: " + parent ); 335 336 if( !dir.canWrite() ) 337 abort( "FileCopy: destination directory is unwriteable: " + parent ); 338 339 } 340 341 FileInputStream from = null; 344 FileOutputStream to = null; 346 try 348 { 349 from = new FileInputStream( from_file ); 350 to = new FileOutputStream( to_file ); 352 byte[] buffer = new byte[4096]; 354 int bytes_read; 356 while( ( bytes_read = from.read( buffer ) ) != -1 ) 362 { 363 to.write( buffer, 0, bytes_read ); 365 } 366 } 368 finally 370 { 371 if( from != null ) 372 { 373 try 374 { 375 from.close(); 376 } 377 catch( IOException e ) 378 { 379 ; 380 } 381 } 382 if( to != null ) 383 { 384 try 385 { 386 to.close(); 387 } 388 catch( IOException e ) 389 { 390 ; 391 } 392 } 393 } 394 } 395 396 public static void download( DownloadTracker tracker, URL from, File toFile ) 397 throws IOException 398 { 399 tracker.info( "connecting..." ); 400 URLConnection con = from.openConnection(); 401 con.connect(); 402 tracker.info( "connection established" ); 403 404 long size = con.getContentLength(); 405 T.debug( "content length=" + size ); 406 407 tracker.start( size ); 408 tracker.info( "downloading " + size + " bytes..." ); 409 410 InputStream in = con.getInputStream(); 411 413 toFile.getParentFile().mkdirs(); 415 416 FileOutputStream fout = new FileOutputStream( toFile ); 417 OutputStream out = new BufferedOutputStream( fout ); 418 419 try 420 { 421 byte buffer[] = new byte[4096]; 422 int bytes_read = 0; 423 int total_bytes_read = 0; 424 425 while( ( bytes_read = in.read( buffer ) ) != -1 ) 426 { 427 out.write( buffer, 0, bytes_read ); 428 429 total_bytes_read += bytes_read; 430 tracker.update( total_bytes_read ); 431 } 432 433 out.flush(); 434 } 435 finally 436 { 437 try 438 { 439 out.close(); 440 } 441 catch( IOException e ) 442 {} 443 try 444 { 445 in.close(); 446 } 447 catch( IOException e ) 448 {} 449 tracker.stop(); 450 } 451 } 452 453 public static File[] findByExt( File base, String ext ) 454 { 455 458 Vector result = new Vector(); 459 460 findByExtWorker( result, base, ext ); 461 462 Object objs[] = result.toArray(); 464 File files[] = new File[objs.length]; 465 System.arraycopy( objs, 0, files, 0, objs.length ); 466 467 return files; 468 } 469 470 public static String prettyPrintFileSize( File file ) 471 { 472 long size = file.length(); 473 474 if( size < 1028 ) 476 return "1 k"; 477 478 size = size / 1028; 479 480 return size + " k"; 481 } 482 483 public static void saveStreamToFile( InputStream in, File outFile ) 484 throws IOException 485 { 486 FileOutputStream out = null; 487 try 488 { 489 out = new FileOutputStream( outFile ); 490 byte[] buf = new byte[4096]; 491 int bytes_read; 492 while( ( bytes_read = in.read( buf ) ) != -1 ) 493 out.write( buf, 0, bytes_read ); 494 } 495 finally 496 { 497 if( in != null ) 498 try 499 { 500 in.close(); 501 } 502 catch( IOException e ) 503 {} 504 505 if( out != null ) 506 try 507 { 508 out.close(); 509 } 510 catch( IOException e ) 511 {} 512 } 513 } 514 515 516 519 private static void abort( String msg ) throws IOException 520 { 521 throw new IOException( msg ); 522 } 523 524 private static void findByExtWorker( Vector result, File base, String ext ) 525 { 526 File files[] = base.listFiles(); 527 528 for( int i = 0; i < files.length; i++ ) 529 { 530 File file = files[i]; 531 if( !file.isDirectory() ) 532 { 533 String currentExt = getFileExtension( file ); 534 if( currentExt.equalsIgnoreCase( ext ) ) 535 { 536 538 T.debug( "[find] match -> " + file.getAbsolutePath() ); 539 result.add( file ); 540 } 541 } 542 else 543 { 544 546 findByExtWorker( result, file, ext ); 547 } 548 } 549 } 550 551 } 552 | Popular Tags |