1 21 22 package com.izforge.izpack.util; 23 24 import java.io.BufferedInputStream ; 25 import java.io.BufferedOutputStream ; 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.FileOutputStream ; 29 import java.io.IOException ; 30 import java.util.Properties ; 31 import java.util.StringTokenizer ; 32 33 39 public class IoHelper 40 { 41 42 47 51 52 private static final String MASKED_SLASH_PLACEHOLDER = "~&_&~"; 53 54 private static Properties envVars = null; 55 56 59 private IoHelper() 60 { 61 } 62 63 69 public static void copyFile(String inFile, String outFile) throws IOException 70 { 71 copyFile(new File (inFile), new File (outFile)); 72 } 73 74 82 public static void copyFile(File inFile, File outFile) throws IOException 83 { 84 copyFile(inFile, outFile, null, null); 85 } 86 87 97 public static void copyFile(File inFile, File outFile, String permissions) throws IOException 98 { 99 copyFile(inFile, outFile, permissions, null); 100 } 101 102 112 public static void copyFile(File inFile, File outFile, VariableSubstitutor vss) 113 throws IOException 114 { 115 copyFile(inFile, outFile, null, vss); 116 } 117 118 130 public static void copyFile(File inFile, File outFile, String permissions, 131 VariableSubstitutor vs) throws IOException 132 { 133 copyFile(inFile, outFile, permissions, vs, null); 134 } 135 136 149 public static void copyFile(File inFile, File outFile, String permissions, 150 VariableSubstitutor vs, String type) throws IOException 151 { 152 FileOutputStream out = new FileOutputStream (outFile); 153 FileInputStream in = new FileInputStream (inFile); 154 if (vs == null) 155 { 156 byte[] buffer = new byte[5120]; 157 long bytesCopied = 0; 158 int bytesInBuffer; 159 while ((bytesInBuffer = in.read(buffer)) != -1) 160 { 161 out.write(buffer, 0, bytesInBuffer); 162 bytesCopied += bytesInBuffer; 163 } 164 in.close(); 165 out.close(); 166 } 167 else 168 { 169 BufferedInputStream bin = new BufferedInputStream (in, 5120); 170 BufferedOutputStream bout = new BufferedOutputStream (out, 5120); 171 vs.substitute(bin, bout, type, null); 172 bin.close(); 173 bout.close(); 174 } 175 if (permissions != null && IoHelper.supported("chmod")) 176 { 177 chmod(outFile.getAbsolutePath(), permissions); 178 } 179 } 180 181 191 public static File copyToTempFile(File template, String defaultExtension) throws IOException 192 { 193 return copyToTempFile(template, defaultExtension, null); 194 } 195 196 208 public static File copyToTempFile(File template, String defaultExtension, 209 VariableSubstitutor vss) throws IOException 210 { 211 String path = template.getCanonicalPath(); 212 int pos = path.lastIndexOf('.'); 213 String ext = path.substring(pos); 214 if (ext == null) ext = defaultExtension; 215 File tmpFile = File.createTempFile("izpack_io", ext); 216 tmpFile.deleteOnExit(); 217 IoHelper.copyFile(template, tmpFile, vss); 218 return tmpFile; 219 } 220 221 231 public static File copyToTempFile(String template, String defaultExtension) throws IOException 232 { 233 return copyToTempFile(new File (template), defaultExtension); 234 } 235 236 243 public static void chmod(File file, String permissions) throws IOException 244 { 245 chmod(file.getAbsolutePath(), permissions); 246 } 247 248 256 public static void chmod(String path, String permissions) throws IOException 257 { 258 if (OsVersion.IS_UNIX) 260 { 261 String [] params = { "chmod", permissions, path}; 262 String [] output = new String [2]; 263 FileExecutor fe = new FileExecutor(); 264 fe.executeCommand(params, output); 265 } 266 else 267 { 268 throw new IOException ("Sorry, chmod not supported yet on " + OsVersion.OS_NAME + "."); 269 } 270 } 271 272 278 public static long getFreeSpace(String path) 279 { 280 long retval = -1; 281 if (OsVersion.IS_WINDOWS) 282 { 283 String command = "cmd.exe"; 284 if (System.getProperty("os.name").toLowerCase().indexOf("windows 9") > -1) return (-1); 285 String [] params = { command, "/C", "\"dir /D /-C \"" + path + "\"\""}; 286 String [] output = new String [2]; 287 FileExecutor fe = new FileExecutor(); 288 fe.executeCommand(params, output); 289 retval = extractLong(output[0], -3, 3, "%"); 290 } 291 else if (OsVersion.IS_SUNOS) 292 { 293 String [] params = { "df", "-k", path}; 294 String [] output = new String [2]; 295 FileExecutor fe = new FileExecutor(); 296 fe.executeCommand(params, output); 297 retval = extractLong(output[0], -3, 3, "%") * 1024; 298 } 299 else if (OsVersion.IS_HPUX) 300 { 301 String [] params = { "bdf", path }; 302 String [] output = new String [2]; 303 FileExecutor fe = new FileExecutor(); 304 fe.executeCommand(params, output); 305 retval = extractLong(output[0], -3, 3, "%") * 1024; 306 } 307 else if (OsVersion.IS_UNIX) 308 { 309 String [] params = { "df", "-Pk", path}; 310 String [] output = new String [2]; 311 FileExecutor fe = new FileExecutor(); 312 fe.executeCommand(params, output); 313 retval = extractLong(output[0], -3, 3, "%") * 1024; 314 } 315 return retval; 316 } 317 318 326 public static boolean supported(String method) 327 { 328 if ("getFreeSpace".equals(method)) 329 { 330 if (OsVersion.IS_UNIX) return true; 331 if (OsVersion.IS_WINDOWS) 332 { if (System.getProperty("os.name").toLowerCase().indexOf("windows 9") > -1) 334 return (false); 335 return (true); 336 } 337 } 338 else if ("chmod".equals(method)) 339 { 340 if (OsVersion.IS_UNIX) return true; 341 } 342 else if ("copyFile".equals(method)) 343 { 344 return true; 345 } 346 else if ("getPrimaryGroup".equals(method)) 347 { 348 if (OsVersion.IS_UNIX) return true; 349 } 350 else if ("getenv".equals(method)) 351 { 352 return true; 353 } 354 else 355 { 356 throw new RuntimeException ("method name " + method + "not supported by this method"); 357 } 358 return false; 359 360 } 361 362 368 public static File existingParent(File path) 369 { 370 File result = path; 371 while (!result.exists()) 372 { 373 if (result.getParent() == null) return result; 374 result = result.getParentFile(); 375 } 376 return result; 377 } 378 379 391 private static long extractLong(String in, int assumedPlace, int halfRange, 392 String useNotIdentifier) 393 { 394 long retval = -1; 395 StringTokenizer st = new StringTokenizer (in); 396 int length = st.countTokens(); 397 int i; 398 int currentRange = 0; 399 String [] interestedEntries = new String [halfRange + halfRange]; 400 for (i = 0; i < length - halfRange + assumedPlace; ++i) 401 st.nextToken(); 403 for (i = 0; i < halfRange + halfRange; ++i) 404 { if (st.hasMoreTokens()) 406 { 407 interestedEntries[i] = st.nextToken(); 408 currentRange++; 409 } 410 } 411 412 for (i = currentRange - 1; i >= 0; --i) 413 { 414 if (useNotIdentifier != null && interestedEntries[i].indexOf(useNotIdentifier) > -1) 415 continue; 416 try 417 { 418 retval = Long.parseLong(interestedEntries[i]); 419 } 420 catch (NumberFormatException nfe) 421 { 422 continue; 423 } 424 break; 425 } 426 return retval; 427 } 428 429 435 public static String getPrimaryGroup() 436 { 437 if (supported("getPrimaryGroup")) 438 { 439 if (OsVersion.IS_SUNOS) 440 { String [] params = { "id"}; 442 String [] output = new String [2]; 443 FileExecutor fe = new FileExecutor(); 444 fe.executeCommand(params, output); 445 if (output[0] != null) 447 { 448 StringTokenizer st = new StringTokenizer (output[0], "()"); 449 int length = st.countTokens(); 450 if (length >= 4) 451 { 452 for (int i = 0; i < 3; ++i) 453 st.nextToken(); 454 return (st.nextToken()); 455 } 456 } 457 return (null); 458 } 459 else 460 { 461 String [] params = { "id", "-gn"}; 462 String [] output = new String [2]; 463 FileExecutor fe = new FileExecutor(); 464 fe.executeCommand(params, output); 465 return output[0]; 466 } 467 } 468 else 469 return null; 470 } 471 472 483 public static String replaceString(String destination, String what, String with) 484 { 485 if (destination.indexOf(what) >= 0) 486 { StringBuffer buf = new StringBuffer (); 489 int last = 0; 490 int current = destination.indexOf(what); 491 int whatLength = what.length(); 492 while (current >= 0) 493 { if (current > 0) buf.append(destination.substring(last, current)); 495 buf.append(with); 496 last = current + whatLength; 497 current = destination.indexOf(what, last); 498 } 499 if (destination.length() > last) buf.append(destination.substring(last)); 500 return buf.toString(); 501 } 502 return destination; 503 } 504 505 511 public static String translatePath(String destination, VariableSubstitutor vs) 512 { 513 destination = vs.substitute(destination, null); 515 516 518 522 if (destination.indexOf("\\/") >= 0 && destination.indexOf(MASKED_SLASH_PLACEHOLDER) < 0) 529 { destination = replaceString(destination, "\\/", MASKED_SLASH_PLACEHOLDER); 532 destination = destination.replace('/', File.separatorChar); 535 destination = replaceString(destination, MASKED_SLASH_PLACEHOLDER, "/"); 539 } 540 else 541 destination = destination.replace('/', File.separatorChar); 542 return destination; 543 } 544 545 553 public static String getenv(String key) 554 { 555 if (envVars == null) loadEnv(); 556 if (envVars == null) return (null); 557 if (OsVersion.IS_WINDOWS) key = key.toUpperCase(); 558 return (String ) (envVars.get(key)); 559 } 560 561 564 private static void loadEnv() 565 { 566 String [] output = new String [2]; 567 String [] params; 568 if (OsVersion.IS_WINDOWS) 569 { 570 String command = "cmd.exe"; 571 if (System.getProperty("os.name").toLowerCase().indexOf("windows 9") > -1) 572 command = "command.com"; 573 String [] paramst = { command, "/C", "set"}; 574 params = paramst; 575 } 576 else 577 { 578 String [] paramst = { "env"}; 579 params = paramst; 580 } 581 FileExecutor fe = new FileExecutor(); 582 fe.executeCommand(params, output); 583 if (output[0].length() <= 0) return; 584 String lineSep = System.getProperty("line.separator"); 585 StringTokenizer st = new StringTokenizer (output[0], lineSep); 586 envVars = new Properties (); 587 String var = null; 588 while (st.hasMoreTokens()) 589 { 590 String line = st.nextToken(); 591 if (line.indexOf('=') == -1) 592 { if (var == null) 594 { 595 var = lineSep + line; 596 } 597 else 598 { 599 var += lineSep + line; 600 } 601 } 602 else 603 { setEnvVar(var); 605 var = line; 606 } 607 } 608 setEnvVar(var); 609 } 610 611 617 private static void setEnvVar(String var) 618 { 619 if (var == null) return; 620 int index = var.indexOf('='); 621 if (index < 0) return; 622 String key = var.substring(0, index); 623 if (OsVersion.IS_WINDOWS) key = key.toUpperCase(); 625 envVars.setProperty(key, var.substring(index + 1)); 626 627 } 628 } 629 | Popular Tags |