Class ShellUtils

java.lang.Object
com.topjohnwu.superuser.ShellUtils

public final class ShellUtils extends Object
Some handy utility methods that are used in libsu.

These methods are for internal use. I personally find them pretty handy, so I gathered them here. However, since these are meant to be used internally, they are not stable APIs. I would change them without too much consideration if needed. Also, these methods are not well tested for public usage, many might not handle some edge cases correctly. You have been warned!!

  • Method Details

    • isValidOutput

      public static boolean isValidOutput(List<String> out)
      Test whether the list is null or empty or all elements are empty strings.
      Parameters:
      out - the output of a shell command.
      Returns:
      false if the list is null or empty or all elements are empty strings.
    • fastCmd

      @NonNull public static String fastCmd(String... cmds)
      Run commands with the main shell and get a single line output.
      Parameters:
      cmds - the commands.
      Returns:
      the last line of the output of the command, empty string if no output is available.
    • fastCmd

      @NonNull public static String fastCmd(Shell shell, String... cmds)
      Run commands and get a single line output.
      Parameters:
      shell - a shell instance.
      cmds - the commands.
      Returns:
      the last line of the output of the command, empty string if no output is available.
    • fastCmdResult

      public static boolean fastCmdResult(String... cmds)
      Run commands with the main shell and return whether exits with 0 (success).
      Parameters:
      cmds - the commands.
      Returns:
      true if the commands succeed.
    • fastCmdResult

      public static boolean fastCmdResult(Shell shell, String... cmds)
      Run commands and return whether exits with 0 (success).
      Parameters:
      shell - a shell instance.
      cmds - the commands.
      Returns:
      true if the commands succeed.
    • onMainThread

      public static boolean onMainThread()
      Check if current thread is main thread.
      Returns:
      true if the current thread is the main thread.
    • cleanInputStream

      public static void cleanInputStream(InputStream in)
      Discard all data currently available in an InputStream.
      Parameters:
      in - the InputStream to be cleaned.
    • escapedString

      public static String escapedString(String s)
      Format string to quoted and escaped string suitable for shell commands.
      Parameters:
      s - the string to be formatted.
      Returns:
      the formatted string.
    • gcd

      public static long gcd(long u, long v)
      Get the greatest common divisor of 2 integers with binary algorithm.
      Parameters:
      u - an integer.
      v - an integer.
      Returns:
      the greatest common divisor.