Changeset 923


Ignore:
Timestamp:
Nov 8, 2010, 1:36:40 PM (7 years ago)
Author:
stefan
Message:

reverted accidental change

Location:
trunk/src/util
Files:
1 deleted
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/util/SystemUtils.java

    r922 r923  
    11package util;
    22
    3 import java.io.*;
    4 import java.util.ArrayList;
     3import java.io.File;
    54import java.util.regex.Pattern;
    65
    76/**
    8  * System utilities.
    9  *
    10  * @author Stefan Endrullis
    11  * @version 1.3
     7 * SystemUtils includes some special system functions.
    128 */
    139public class SystemUtils {
     10  private static Pattern absoluteFile = Pattern.compile("^(?:/|[A-Z]:).*");
     11
    1412  /**
    15    * Executes a specified command.
     13   * Return the name of the operating system.
    1614   *
    17    * @param cmd specified command
    18    * @return output of the command
    19    * @throws IOException if an I/O error occurs
    20    * @throws InterruptedException if the current thread is interrupted by another thread
    21    *   while it is waiting for the completion of the command execution.
     15   * @return name of the operating system
    2216   */
    23   public static ArrayList<String> exec(String cmd) throws IOException, InterruptedException {
    24     Process process = Runtime.getRuntime().exec(cmd);
    25 
    26     return proceed(process, cmd);
     17  public static String getOSName() {
     18    return System.getProperty("os.name");
    2719  }
    2820
    2921  /**
    30    * Executes a specified command.
     22   * Returns if the operating system is a ms windows system.
    3123   *
    32    * @param cmd specified command
    33    * @return output of the command
    34    * @throws IOException if an I/O error occurs
    35    * @throws InterruptedException if the current thread is interrupted by another thread
    36    *   while it is waiting for the completion of the command execution.
     24   * @return true if os is windows
    3725   */
    38   public static ArrayList<String> exec(String[] cmd) throws IOException, InterruptedException {
    39     Process process = Runtime.getRuntime().exec(cmd);
    40 
    41     String cmdString = cmd[0];
    42     for (int i = 0; i < cmd.length; i++) {
    43       cmdString += " \"" + cmd[i] + "\"";
    44     }
    45 
    46     return proceed(process, cmdString);
     26  public static boolean isWinOS() {
     27    return System.getProperty("os.name").toLowerCase().startsWith("win");
    4728  }
    4829
    4930  /**
    50    * Waits for the completion of the command execution and checks the return code.
     31   * Returns if the simple operating system type (windows/unix).
    5132   *
    52    * @param process process running the command
    53    * @param cmd command string
    54    * @return output of the command
    55    * @throws IOException if an I/O error occurs
    56    * @throws InterruptedException if the current thread is interrupted by another thread
    57    *   while it is waiting for the completion of the command execution.
     33   * @return windows or unix
    5834   */
    59   private static ArrayList<String> proceed(Process process, String cmd) throws IOException, InterruptedException {
    60     InputStream in = process.getInputStream();
    61     BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    62 
    63     ArrayList<String> lines = new ArrayList<String>();
    64     String line;
    65     while((line = reader.readLine()) != null) lines.add(line);
    66 
    67     int returnCode = process.waitFor();
    68     if(returnCode != 0) {
    69       InputStream errIn = process.getErrorStream();
    70       BufferedReader errReader = new BufferedReader(new InputStreamReader(errIn));
    71 
    72       ArrayList<String> errLines = new ArrayList<String>();
    73       String errLine;
    74       while((errLine = errReader.readLine()) != null) errLines.add(errLine);
    75 
    76       throw new ExecutionFailedException(returnCode, "Execution of \"" + cmd + "\" failed!", errLines);
    77     }
    78 
    79     return lines;
     35  public static String getSimpleOSType() {
     36    return isWinOS() ? "windows" : "unix";
    8037  }
    8138
    82   /**
    83    * Return the name of the operating system.
    84    *
    85    * @return name of the operating system
    86    */
    87   public static String getOSName() {
    88     return System.getProperty("os.name");
    89   }
    90 
    91   /**
    92    * Returns if the operating system is a ms windows system.
    93    *
    94    * @return true if os is windows
    95    */
    96   public static boolean isWinOS() {
    97     return System.getProperty("os.name").toLowerCase().startsWith("win");
    98   }
    99 
    100   /**
    101    * Returns if the simple operating system type (windows/unix).
    102    *
    103    * @return windows or unix
    104    */
    105   public static String getSimpleOSType() {
    106     return isWinOS() ? "windows" : "unix";
    107   }
     39  public static File newFile(File parent, String fileName) {
     40    if (absoluteFile.matcher(fileName).matches()) return new File(fileName);
     41    return new File(parent, fileName);
     42  }
    10843}
Note: See TracChangeset for help on using the changeset viewer.