Java Code Examples for com.topjohnwu.superuser.ShellUtils

The following examples show how to use com.topjohnwu.superuser.ShellUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: SmartFlasher   Source File: RootUtils.java    License: GNU General Public License v3.0 6 votes vote down vote up
@NonNull
public static String runAndGetError(String command) {
    StringBuilder sb = new StringBuilder();
    List<String> outputs = new ArrayList<>();
    List<String> stderr = new ArrayList<>();
    try {
        Shell.su(command).to(outputs, stderr).exec();
        outputs.addAll(stderr);
        if (ShellUtils.isValidOutput(outputs)) {
            for (String output : outputs) {
                sb.append(output).append("\n");
            }
        }
        return removeSuffix(sb.toString()).trim();
    } catch (Exception e) {
        return "";
    }
}
 
Example 2
Source Project: libsu   Source File: ShellImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void execTask(@NonNull Task task) throws IOException {
    if (status < 0)
        throw new ShellTerminatedException();

    ShellUtils.cleanInputStream(STDOUT);
    ShellUtils.cleanInputStream(STDERR);
    try {
        STDIN.write('\n');
        STDIN.flush();
    } catch (IOException e) {
        // Shell is dead
        release();
        throw new ShellTerminatedException();
    }

    task.run(STDIN, STDOUT, STDERR);
}
 
Example 3
@NonNull
public static String runAndGetError(String command) {
    StringBuilder sb = new StringBuilder();
    List<String> outputs = new ArrayList<>();
    List<String> stderr = new ArrayList<>();
    try {
        Shell.su(command).to(outputs, stderr).exec();
        outputs.addAll(stderr);
        if (ShellUtils.isValidOutput(outputs)) {
            for (String output : outputs) {
                sb.append(output).append("\n");
            }
        }
        return Utils.removeSuffix(sb.toString(), "\n").trim();
    } catch (Exception e) {
        return "";
    }
}
 
Example 4
Source Project: SmartFlasher   Source File: RootUtils.java    License: GNU General Public License v3.0 5 votes vote down vote up
@NonNull
public static String runAndGetOutput(String command) {
    StringBuilder sb = new StringBuilder();
    try {
        List<String> outputs = Shell.su(command).exec().getOut();
        if (ShellUtils.isValidOutput(outputs)) {
            for (String output : outputs) {
                sb.append(output).append("\n");
            }
        }
        return removeSuffix(sb.toString()).trim();
    } catch (Exception e) {
        return "";
    }
}
 
Example 5
Source Project: libsu   Source File: InputHandler.java    License: Apache License 2.0 5 votes vote down vote up
static InputHandler newInstance(InputStream is) {
    return in -> {
        InternalUtils.log(TAG, "<InputStream>");
        ShellUtils.noFlushPump(is, in);
        is.close();
        // Make sure it ends properly
        in.write('\n');
    };
}
 
Example 6
Source Project: libsu   Source File: UiThreadHandler.java    License: Apache License 2.0 5 votes vote down vote up
public static void run(Runnable r) {
    if (ShellUtils.onMainThread()) {
        r.run();
    } else {
        handler.post(r);
    }
}
 
Example 7
Source Project: libsu   Source File: UiThreadHandler.java    License: Apache License 2.0 5 votes vote down vote up
public static void runWithLock(Object lock, Runnable r) {
    if (ShellUtils.onMainThread()) {
        synchronized (lock) { r.run(); }
    } else {
        handler.post(() -> { synchronized (lock) { r.run(); } });
    }
}
 
Example 8
Source Project: libsu   Source File: UiThreadHandler.java    License: Apache License 2.0 5 votes vote down vote up
public static void runAndWait(Runnable r) {
    if (ShellUtils.onMainThread()) {
        r.run();
    } else {
        WaitRunnable wr = new WaitRunnable(r);
        handler.post(wr);
        wr.waitUntilDone();
    }
}
 
Example 9
Source Project: libsu   Source File: ShellBlockIO.java    License: Apache License 2.0 5 votes vote down vote up
ShellBlockIO(SuFile file, String mode) throws FileNotFoundException {
    super(file, mode);
    if (Env.blockdev()) {
        try {
            blockSize = Long.parseLong(ShellUtils.fastCmd(
                    "blockdev --getsize64 '" + file.getAbsolutePath() + "'"));
        } catch (NumberFormatException e) {
            blockSize = Long.MAX_VALUE;
        }
    } else {
        // No blockdev available, no choice but to assume no boundary
        blockSize = Long.MAX_VALUE;
    }
    WRITE_CONV = "";
}
 
Example 10
Source Project: libsu   Source File: ShellIO.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int read(byte[] b, int off, int len) throws IOException {
    if (off < 0 || len < 0 || off + len > b.length)
        throw new IndexOutOfBoundsException();
    if (len == 0)
        return 0;
    if (eof)
        return -1;
    // Try to use as large block as possible
    long gcd = ShellUtils.gcd(fileOff, len);
    if (gcd >= 512) {
        // Aligned, directly process it
        len = read(b, off, len, fileOff, gcd);
    } else {
        /* Unaligned reading is too slow, try reading with 512-byte aligned
        * and copy those in interest (still faster than unaligned reading) */
        long start = fileOff / 512 * 512;
        long end = (fileOff + len + 511) / 512 * 512;
        byte[] buf = new byte[(int) (end - start)];
        len = Math.min(read(buf, 0, buf.length, start, 512), len);
        if (len > 0)
            System.arraycopy(buf, (int) (fileOff - start), b, off, len);
    }
    if (len > 0)
        fileOff += len;
    return len;
}
 
Example 11
@NonNull
public static String runAndGetOutput(String command) {
    StringBuilder sb = new StringBuilder();
    try {
        List<String> outputs = Shell.su(command).exec().getOut();
        if (ShellUtils.isValidOutput(outputs)) {
            for (String output : outputs) {
                sb.append(output).append("\n");
            }
        }
        return Utils.removeSuffix(sb.toString(), "\n").trim();
    } catch (Exception e) {
        return "";
    }
}
 
Example 12
Source Project: libsu   Source File: Env.java    License: Apache License 2.0 4 votes vote down vote up
public static boolean blockdev() {
    Shell shell = checkShell();
    if (blockdev == null)
        blockdev = ShellUtils.fastCmdResult(shell, "command -v blockdev");
    return blockdev;
}
 
Example 13
Source Project: libsu   Source File: Env.java    License: Apache License 2.0 4 votes vote down vote up
public static boolean stat() {
    Shell shell = checkShell();
    if (stat == null)
        stat = ShellUtils.fastCmdResult(shell, "command -v stat");
    return stat;
}
 
Example 14
Source Project: libsu   Source File: Env.java    License: Apache License 2.0 4 votes vote down vote up
public static boolean wc() {
    Shell shell = checkShell();
    if (wc == null)
        wc = ShellUtils.fastCmdResult(shell, "command -v wc");
    return wc;
}
 
Example 15
Source Project: libsu   Source File: SuFile.java    License: Apache License 2.0 4 votes vote down vote up
private String cmd(String c) {
    return ShellUtils.fastCmd(setCmd(c));
}
 
Example 16
Source Project: libsu   Source File: SuFile.java    License: Apache License 2.0 4 votes vote down vote up
private boolean cmdBool(String c) {
    return ShellUtils.fastCmdResult(setCmd(c));
}