Package 

Class ShellUtils


  • 
    public final class ShellUtils
    
                        

    Some handy utility methods that are used in {@code 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 Summary

      Modifier and Type Method Description
      static boolean isValidOutput(List<String> out) Test whether the list is {@code null} or empty or all elements are empty strings.
      static String fastCmd(Array<String> cmds) Run commands with the main shell and get a single line output.
      static String fastCmd(Shell shell, Array<String> cmds) Run commands and get a single line output.
      static boolean fastCmdResult(Array<String> cmds) Run commands with the main shell and return whether exits with 0 (success).
      static boolean fastCmdResult(Shell shell, Array<String> cmds) Run commands and return whether exits with 0 (success).
      static boolean onMainThread() Check if current thread is main thread.
      static void cleanInputStream(InputStream in) Discard all data currently available in an InputStream.
      static String escapedString(String s) Format string to quoted and escaped string suitable for shell commands.
      static long gcd(long u, long v) Get the greatest common divisor of 2 integers with binary algorithm.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • isValidOutput

         static boolean isValidOutput(List<String> out)

        Test whether the list is {@code null} or empty or all elements are empty strings.

        Parameters:
        out - the output of a shell command.
      • fastCmd

        @NonNull() static String fastCmd(Array<String> cmds)

        Run commands with the main shell and get a single line output.

        Parameters:
        cmds - the commands.
      • fastCmdResult

         static boolean fastCmdResult(Array<String> cmds)

        Run commands with the main shell and return whether exits with 0 (success).

        Parameters:
        cmds - the commands.
      • fastCmdResult

         static boolean fastCmdResult(Shell shell, Array<String> cmds)

        Run commands and return whether exits with 0 (success).

        Parameters:
        shell - a shell instance.
        cmds - the commands.
      • onMainThread

         static boolean onMainThread()

        Check if current thread is main thread.

      • escapedString

         static String escapedString(String s)

        Format string to quoted and escaped string suitable for shell commands.

        Parameters:
        s - the string to be formatted.
      • gcd

         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.