Package com.topjohnwu.superuser
Class ShellUtils
java.lang.Object
com.topjohnwu.superuser.ShellUtils
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 Summary
Modifier and TypeMethodDescriptionstatic void
Discard all data currently available in anInputStream
.static String
Format string to quoted and escaped string suitable for shell commands.static String
Run commands and get a single line output.static String
Run commands with the main shell and get a single line output.static boolean
fastCmdResult
(Shell shell, String... cmds) Run commands and return whether exits with 0 (success).static boolean
fastCmdResult
(String... cmds) Run commands with the main shell and return whether exits with 0 (success).static long
gcd
(long u, long v) Get the greatest common divisor of 2 integers with binary algorithm.static boolean
isValidOutput
(List<String> out) Test whether the list isnull
or empty or all elements are empty strings.static boolean
Check if current thread is main thread.
-
Method Details
-
isValidOutput
Test whether the list isnull
or empty or all elements are empty strings.- Parameters:
out
- the output of a shell command.- Returns:
false
if the list isnull
or empty or all elements are empty strings.
-
fastCmd
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
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
Run commands with the main shell and return whether exits with 0 (success).- Parameters:
cmds
- the commands.- Returns:
true
if the commands succeed.
-
fastCmdResult
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
Discard all data currently available in anInputStream
.- Parameters:
in
- theInputStream
to be cleaned.
-
escapedString
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.
-