cakoose.util.exec
Class WindowsArgProtector

java.lang.Object
  extended by cakoose.util.exec.WindowsArgProtector

public class WindowsArgProtector
extends Object

Windows' command-line argument processing is stupid. This class contains functions to encode arguments so that they come out right after being parsed by Windows.


Method Summary
static int calcEscapes(String arg)
          Determines whether the argument needs quotes and, if so, how many characters need to be escaped.
static void main(String[] args)
           
static String quote(String arg)
          Given a string X, this function returns a string that, when passed through the Windows implementation of Java's Runtime.exec(String[]) or ProcessBuilder, will appear to the spawned process as X.
static void quote(StringBuffer buf, String arg)
          A version of 'quote' that appends to a buffer instead of returning a string.
static String quoteMaybe(String arg)
          Given a string X, this function returns a string that, when passed through the Windows implementation of Java's Runtime.exec(String[]) or ProcessBuilder, will appear to the spawned process as X.
static void quoteMaybe(StringBuffer buf, String arg)
          A version of 'quoteMaybe' that appends to a buffer instead of returning a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

quote

public static String quote(String arg)
Given a string X, this function returns a string that, when passed through the Windows implementation of Java's Runtime.exec(String[]) or ProcessBuilder, will appear to the spawned process as X. This function will always quote and escape the argument, even if it isn't necessary. Use quoteMaybe(String) if you only want to quote when necessary.

Parameters:
arg - The argument to quote.
Returns:
The quote version of 'arg'.

quote

public static void quote(StringBuffer buf,
                         String arg)
A version of 'quote' that appends to a buffer instead of returning a string.


calcEscapes

public static int calcEscapes(String arg)
Determines whether the argument needs quotes and, if so, how many characters need to be escaped. A return value of -1 means that the string doesn't need to be quoted. A return value of N, where N is greater than -1, means that the string needs to be quoted and N characters need to be escaped.


quoteMaybe

public static String quoteMaybe(String arg)
Given a string X, this function returns a string that, when passed through the Windows implementation of Java's Runtime.exec(String[]) or ProcessBuilder, will appear to the spawned process as X. This function differs from quote(String) in that this function doesn't always add quotes around the argument. If the argument is fine as-is, it will be returned unmodified.

Parameters:
arg - The argument to check and protect.
Returns:
A safe version of 'arg' (may be equal to 'arg')

quoteMaybe

public static void quoteMaybe(StringBuffer buf,
                              String arg)
A version of 'quoteMaybe' that appends to a buffer instead of returning a string.


main

public static void main(String[] args)