1 64 69 package com.jcorporate.expresso.core.misc; 70 71 import com.jcorporate.expresso.core.db.DBException; 72 import com.jcorporate.expresso.services.dbobj.Setup; 73 import org.apache.log4j.Logger; 74 75 import java.io.BufferedInputStream ; 76 import java.io.BufferedOutputStream ; 77 import java.io.BufferedReader ; 78 import java.io.File ; 79 import java.io.FileInputStream ; 80 import java.io.FileOutputStream ; 81 import java.io.IOException ; 82 import java.io.InputStream ; 83 import java.io.InputStreamReader ; 84 import java.io.OutputStream ; 85 import java.util.Enumeration ; 86 import java.util.StringTokenizer ; 87 import java.util.Vector ; 88 89 90 95 public class FileUtil { 96 private static String thisClass = FileUtil.class + "."; 97 98 private static Logger log = Logger.getLogger(FileUtil.class); 99 100 103 public FileUtil() { 104 super(); 105 } 106 107 108 109 119 public static File getTempDirectory(String dataContext) throws ConfigurationException { 120 try { 121 String templocation = Setup.getValueRequired(dataContext, "TempDir"); 122 File f = new File (templocation); 123 if (f != null) { 124 if (f.isFile()) { 125 throw new ConfigurationException("Location: " + templocation 126 + " is a file not a directory"); 127 } else if (!f.exists()) { 128 f.mkdirs(); 129 return f; 130 } else if (f.isDirectory()) { 131 return f; 132 } else { 133 throw new ConfigurationException("Location " + templocation + 134 " is listed as neither a file nor a directory."); 135 } 136 } else { 137 throw new ConfigurationException("Unable to create java.io.File object"); 138 } 139 } catch (DBException ex) { 140 throw new ConfigurationException("Unable to locate temp directory for Expresso" 141 , ex); 142 } 143 } 144 145 153 public static String getBase(String fileName) { 154 155 String tempName1; 156 157 int val = fileName.lastIndexOf("/"); 158 if (val < 0) { 159 val = fileName.lastIndexOf("\\"); 160 } 161 162 if (val < 0) { 163 tempName1 = fileName; 164 } else { 165 tempName1 = fileName.substring(val + 1); 166 } 167 168 if (tempName1 == null || tempName1.length() == 0) { 169 return null; 170 } 171 172 val = tempName1.indexOf("."); 173 174 if (val > 0) { 175 tempName1 = tempName1.substring(0, val); 176 } else if (val == 0) { 177 tempName1 = ""; 178 } 179 180 return tempName1; 181 } 182 183 184 192 public static Vector getDir(String dirName) throws IOException { 193 String myName = (thisClass + "getDir(String)"); 194 File dirFile = new File (dirName); 195 196 if (!dirFile.isDirectory()) { 197 throw new IOException (myName + ":'" + dirName + 198 "' is not a directory."); 199 } 200 201 String [] dir = dirFile.list(); 202 203 if (dir == null) { 204 throw new IOException (myName + ":Null array reading directory " + 205 " of " + dirName); 206 } 207 208 Vector fileList = new Vector (1); 209 String oneFileName = null; 210 211 for (int i = 0; i < dir.length; i++) { 212 oneFileName = dir[i].trim(); 213 fileList.addElement(oneFileName); 214 } 215 216 return fileList; 217 } 218 219 220 231 public static Vector getFileListingRecursive(String dirName, 232 String prefix, 233 String separator, 234 Vector includeExtension) throws IOException { 235 String myName = (thisClass + "getFileListingRecursive(String,String,String,Vector)"); 236 237 File dirFile = new File (dirName); 238 239 if (!dirFile.isDirectory()) { 240 throw new IOException (myName + ":'" + dirName + 241 "' is not a directory."); 242 } 243 244 File [] dir = dirFile.listFiles(); 245 246 if (dir == null) { 247 throw new IOException (myName + ":Null array reading directory " + 248 " of " + dirName); 249 } 250 251 Vector fileList = new Vector (1); 252 String oneFileName = null; 253 254 String prefixToAdd = ""; 255 if (!StringUtil.isBlankOrNull(prefix)) { 256 prefixToAdd = prefix + separator; 257 } 258 259 for (int i = 0; i < dir.length; i++) { 260 if (dir[i].isDirectory()) { 261 fileList.addAll(getFileListingRecursive(dir[i].getAbsolutePath(), prefixToAdd + dir[i].getName(), separator, 262 includeExtension)); 263 } else { 264 if (includeExtension == null || includeExtension.contains(getExtension(dir[i].getName()))) { 265 oneFileName = dir[i].getName(); 266 fileList.add(prefixToAdd + oneFileName); 267 } 268 } 269 } 270 271 return fileList; 272 } 273 274 280 public static String getExtension(String fileName) { 281 String tempName = new File (fileName).getName(); 282 StringTokenizer stk = new StringTokenizer (tempName, "."); 283 stk.nextToken(); 284 285 if (stk.hasMoreTokens()) { 286 String ext; 287 do { 288 ext = stk.nextToken(); 289 } while (stk.hasMoreTokens()); 290 291 return ext; 292 } else { 293 return (""); 294 } 295 } 296 297 298 304 public static String getPath(String fileName) { 305 if (fileName == null || fileName.length() == 0) { 306 return fileName; 307 } 308 309 int val = fileName.lastIndexOf("/"); 310 if (val < 0) { 311 val = fileName.lastIndexOf("\\"); 312 } 313 314 String tempPath; 315 if (val < 0) { 316 return fileName; 317 } else { 318 tempPath = fileName.substring(0, val); 319 } 320 321 return tempPath; 322 } 323 324 325 335 public static void cleanDirs(String fileName) throws IOException { 336 if (log.isDebugEnabled()) { 337 log.debug("Cleaning " + fileName); 338 } 339 340 if (!fileName.endsWith("/")) { 341 fileName = fileName + "/"; 342 } 343 344 Vector contents = getDir(fileName); 345 String oneItem = null; 346 File oneFile = null; 347 348 for (Enumeration e = contents.elements(); e.hasMoreElements();) { 349 oneItem = (String ) e.nextElement(); 350 oneFile = new File (fileName + oneItem); 351 352 353 if (oneFile.isDirectory()) { 354 if (log.isDebugEnabled()) { 355 log.debug("Cleaning subdirectory " + fileName + oneItem); 356 } 357 358 359 cleanDirs(fileName + oneItem); 360 361 362 if (getDir(fileName + oneItem).size() == 0) { 363 oneFile = new File (fileName + oneItem); 364 log.info("Deleting empty directory " + oneItem); 365 366 367 if (!oneFile.delete()) { 368 log.error("Unable to delete directory " + oneItem); 369 } 370 371 } 372 373 } 374 375 } 376 377 } 378 379 380 391 public static void copyFile(String sourceFile, String destFile) 392 throws IOException { 393 String myName = (thisClass + "copyFile(String, String)"); 394 395 if (sourceFile.equals("")) { 396 throw new IOException (myName + ":Source file name empty - cannot" + 397 " copy."); 398 } 399 400 if (destFile.equals("")) { 401 throw new IOException (myName + ":Destination file name empty - " + 402 "cannot copy."); 403 } 404 405 int onechar = 0; 406 407 if (sourceFile.equals(destFile)) { 408 throw new IOException (myName + ":Cannot copy file '" + sourceFile + 409 "' to itself"); 410 } 411 412 File dest = new File (destFile); 413 dest.mkdirs(); 414 415 if (dest.exists()) { 416 if (!dest.delete()) { 417 throw new IOException (myName + ":Unable to delete existing " + 418 "destination file '" + destFile + "'. Logged in as " + 419 System.getProperty("user.name")); 420 } 421 } 422 423 File source = new File (sourceFile); 424 425 if (!source.exists()) { 426 throw new IOException (myName + ":Source file '" + sourceFile + 427 "' does not exist. Cannot copy. Logged in as " + 428 System.getProperty("user.name")); 429 } 430 431 BufferedInputStream bin = null; 432 BufferedOutputStream bout = null; 433 434 try { 435 bin = new BufferedInputStream (new FileInputStream (sourceFile)); 436 bout = new BufferedOutputStream (new FileOutputStream (destFile)); 437 438 onechar = bin.read(); 439 440 while (onechar != -1) { 441 bout.write(onechar); 442 onechar = bin.read(); 443 } 444 } finally { 445 if (bin != null) { 446 bin.close(); 447 } 448 449 if (bout != null) { 450 bout.close(); 451 } 452 } 453 454 if (!dest.exists()) { 455 throw new IOException (myName + 456 ":File copy failed: destination file" + " '" + destFile + 457 "' does not exist after copy."); 458 } 459 460 461 462 463 } 469 470 471 476 public static void main(String [] args) { 477 System.out.println("FileUtil Test"); 478 479 String command = (""); 480 BufferedReader ds = new BufferedReader (new InputStreamReader (System.in)); 481 482 try { 483 while (!command.equals("0")) { 484 menu(); 485 System.out.print("Command==>"); 486 command = ds.readLine(); 487 System.out.println(""); 488 System.out.println("Command:" + command); 489 490 if (command.equals("1")) { 491 System.out.println("copyFile"); 492 System.out.print("sourceFile:"); 493 494 String sourceFile = ds.readLine(); 495 System.out.print("destFile:"); 496 497 String destFile = ds.readLine(); 498 copyFile(sourceFile, destFile); 499 System.out.println("Copy Complete\n\n"); 500 } else if (command.equals("2")) { 501 System.out.println("moveFile"); 502 System.out.print("sourceFile:"); 503 504 String sourceFile = ds.readLine(); 505 System.out.print("destFile:"); 506 507 String destFile = ds.readLine(); 508 moveFile(sourceFile, destFile); 509 System.out.println("Move Complete\n\n"); 510 } else if (command.equals("3")) { 511 System.out.println("getPath"); 512 System.out.print("fileName:"); 513 514 String fileName = ds.readLine(); 515 System.out.println("Path:'" + getPath(fileName) + "'\n\n"); 516 } else if (command.equals("4")) { 517 System.out.println("getBase"); 518 System.out.print("fileName:"); 519 520 String fileName = ds.readLine(); 521 System.out.println("Base:'" + getBase(fileName) + "'\n\n"); 522 } else if (command.equals("5")) { 523 System.out.println("getExtension"); 524 System.out.print("fileName:"); 525 526 String fileName = ds.readLine(); 527 System.out.println("Extension:'" + getExtension(fileName) + 528 "'\n\n"); 529 } else if (command.equals("6")) { 530 System.out.println("getDir"); 531 System.out.print("dirName:"); 532 533 String dirName = ds.readLine(); 534 Vector v = getDir(dirName); 535 536 for (Enumeration e = v.elements(); e.hasMoreElements();) { 537 System.out.println("Item:'" + (String ) e.nextElement() + 538 "'"); 539 } 540 541 System.out.println("Directory Complete"); 542 } else if (command.equals("7")) { 543 System.out.println("cleanDirs"); 544 System.out.println("dirName:"); 545 546 String dirName = ds.readLine(); 547 cleanDirs(dirName); 548 } else if (command.equals("8")) { 549 System.out.println("prefix:"); 550 551 String prefix = ds.readLine(); 552 System.out.println("fileName:"); 553 554 String fileName = ds.readLine(); 555 System.out.println("Converted name:" + 556 makeAbsolutePath(prefix, fileName)); 557 } else if (!command.equals("0")) { 558 System.out.println("Unknown command:" + command); 559 } 560 } 561 } catch (Exception e) { 562 e.printStackTrace(System.out); 563 } 564 } 565 566 567 575 public static String makeAbsolutePath(String prefix, String originalPath) { 576 StringUtil.assertNotBlank(originalPath, 577 "Original path may not be blank here"); 578 prefix = StringUtil.notNull(prefix); 579 originalPath = originalPath.replace('\\', '/'); 580 prefix = prefix.replace('\\', '/'); 581 582 if ('/' == originalPath.charAt(0)) { 583 return originalPath; 584 } 585 586 587 if (originalPath.substring(1, 2).equals(":")) { 588 return originalPath; 589 } 590 591 592 593 if (!prefix.endsWith("/")) { 594 prefix = prefix + "/"; 595 } 596 597 598 return prefix + originalPath; 599 } 600 601 602 609 public static void moveFile(String sourceFile, String destFile) 610 throws IOException { 611 String myName = (thisClass + "moveFile(String, String)"); 612 File source = new File (sourceFile); 613 614 if (!source.canRead()) { 615 throw new IOException (myName + ":Cannot read source file '" + 616 sourceFile + "'. Logged in as " + 617 System.getProperty("user.name")); 618 } 619 620 if (!source.canWrite()) { 621 throw new IOException (myName + ":Cannot write to source file '" + 622 sourceFile + "'. Logged in as " + 623 System.getProperty("user.name") + ". Cannot move without " + 624 "write permission to source file."); 625 } 626 627 if (log.isDebugEnabled()) { 628 log.debug("Moving file '" + sourceFile + "' to '" + destFile + "'"); 629 } 630 631 if (sourceFile.equals(destFile)) { 632 log.error("Source and destination the same - no move " + 633 "required"); 634 635 return; 636 } 637 638 copyFile(sourceFile, destFile); 639 640 if (!new File (sourceFile).delete()) { 641 log.error("Copy completed, but unable to " + 642 "delete source file '" + sourceFile + "'. Logged in as " + 643 System.getProperty("user.name")); 644 } 645 } 646 647 648 649 664 public static void copyStream(InputStream is, OutputStream os) 665 throws java.io.IOException { 666 final int DEFAULT_LENGTH = 4 * 1024; 668 int optimalLength = is.available(); 669 670 if (optimalLength > 128 * 1024 || optimalLength == 0) { 676 optimalLength = DEFAULT_LENGTH; 677 } 678 679 byte[] buf = new byte[optimalLength]; 680 int bytesRead; 681 682 while ((bytesRead = is.read(buf)) != -1) { 683 os.write(buf, 0, bytesRead); 684 } 685 686 os.flush(); 687 } 688 689 690 693 private static void menu() { 694 System.out.println("1. copyFile"); 695 System.out.println("2. moveFile"); 696 System.out.println("3. getPath"); 697 System.out.println("4. getBase"); 698 System.out.println("5. getExtension"); 699 System.out.println("6. getDir"); 700 System.out.println("7. cleanDirs"); 701 System.out.println("8. makeAbsolutePath"); 702 System.out.println("0. quit"); 703 } 704 705 } 706 707 | Popular Tags |