Android Open Source - LTM Shell Command






From Project

Back to project page LTM.

License

The source code is released under:

Apache License

If you think the Android project LTM listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/* (C) 2012 Pragmatic Software
   This Source Code Form is subject to the terms of the Mozilla Public
   License, v. 2.0. If a copy of the MPL was not distributed with this
   file, You can obtain one at http://mozilla.org/MPL/2.0/
 *//*from  ww  w  .j  a v  a  2s .co m*/

package com.chrisplus.ltm.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import android.util.Log;

public class ShellCommand {
    private static final String TAG = ShellCommand.class.getSimpleName();
    private Runtime rt;
    private String[] command;
    private Process process;
    private BufferedReader stdout;
    private String tagStr;
    public int exit;

    public ShellCommand(String[] command, String tag) {
        this(command);
        this.tagStr = tag;
    }

    public ShellCommand(String[] command) {
        this.command = command;
        rt = Runtime.getRuntime();
    }

    public String start(boolean waitForExit) {

        try {
            process = new ProcessBuilder()
                    .command(command)
                    .redirectErrorStream(true)
                    .start();

            stdout = new BufferedReader(new InputStreamReader(process.getInputStream()));
        } catch (Exception e) {
            Log.e(TAG, "Failure starting shell command [" + tagStr + "]", e);
            return e.getCause().getMessage();
        }

        if (waitForExit) {
            waitForExit();
        }
        return null;
    }

    public void waitForExit() {
        while (checkForExit() == false) {
            if (stdoutAvailable()) {

            } else {
                try {
                    Thread.sleep(100);
                } catch (Exception e) {
                    Log.d("NetworkLog", "waitForExit", e);
                }
            }
        }
    }

    public void finish() {

        try {
            if (stdout != null) {
                stdout.close();
            }
        } catch (Exception e) {
            Log.e(TAG, "Exception finishing [" + tagStr + "]", e);
        }

        process.destroy();
        process = null;
    }

    public boolean checkForExit() {
        try {
            exit = process.exitValue();
        } catch (Exception IllegalThreadStateException) {
            return false;
        }

        finish();
        return true;
    }

    public boolean stdoutAvailable() {
        try {
            /*
             * if(MyLog.enabled) { MyLog.d("stdoutAvailable [" + tag + "]: " +
             * stdout.ready()); }
             */
            return stdout.ready();
        } catch (java.io.IOException e) {
            Log.e("NetworkLog", "stdoutAvailable error", e);
            return false;
        }
    }

    public String readStdoutBlocking() {

        String line;

        if (stdout == null) {
            return null;
        }

        try {
            line = stdout.readLine();
        } catch (Exception e) {
            Log.e("NetworkLog", "readStdoutBlocking error", e);
            return null;
        }

        if (line == null) {
            return null;
        }
        else {
            return line + "\n";
        }
    }

    public String readStdout() {

        if (stdout == null) {
            return null;
        }

        try {
            if (stdout.ready()) {
                String line = stdout.readLine();

                if (line == null) {
                    return null;
                }
                else {
                    return line + "\n";
                }
            } else {

                return "";
            }
        } catch (Exception e) {
            Log.e("NetworkLog", "readStdout error", e);
            return null;
        }
    }
}




Java Source Code List

com.chrisplus.ltm.LogService.java
com.chrisplus.ltm.MainActivity.java
com.chrisplus.ltm.activies.ErrorDialogActivity.java
com.chrisplus.ltm.core.CoreLogger.java
com.chrisplus.ltm.core.CoreParser.java
com.chrisplus.ltm.utils.CharArrayStringAATree.java
com.chrisplus.ltm.utils.CharArray.java
com.chrisplus.ltm.utils.Constants.java
com.chrisplus.ltm.utils.ItemNotFoundException.java
com.chrisplus.ltm.utils.MD5Sum.java
com.chrisplus.ltm.utils.NetStat.java
com.chrisplus.ltm.utils.ShellCommand.java
com.chrisplus.ltm.utils.StringPool.java
com.chrisplus.ltm.utils.SysUtils.java