Java exec execute(String command, Process p, String[] output, boolean cmdStatus)

Here you can find the source of execute(String command, Process p, String[] output, boolean cmdStatus)

Description

execute

License

Apache License

Declaration

public static void execute(String command, Process p, String[] output,
            boolean cmdStatus) throws IOException 

Method Source Code

//package com.java2s;
//  Licensed under the Apache License, Version 2.0 (the "License");

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class Main {
    static BufferedWriter bw = null;
    static boolean doneflag = false;
    static StringBuffer outputStr = null;
    static StringBuffer errStr = null;
    static boolean nextCommand = false;
    static boolean errorFlag = false;
    static boolean statusFlag = false;

    public static void execute(String command, Process p, String[] output,
            boolean cmdStatus) throws IOException {

        outputStr = new StringBuffer();
        errStr = new StringBuffer();
        output[0] = "";
        statusFlag = false;//  w  ww.  ja v a 2 s . c  o m

        InputStream is = p.getInputStream();
        OutputStream os = p.getOutputStream();
        InputStream es = p.getErrorStream();

        InputStreamReader isr = new InputStreamReader(is);
        InputStreamReader iser = new InputStreamReader(es);
        OutputStreamWriter osw = new OutputStreamWriter(os);

        final BufferedReader br = new BufferedReader(isr);
        final BufferedReader ber = new BufferedReader(iser);

        bw = new BufferedWriter(osw);

        // output thread
        class OutputThread extends Thread {

            StringBuffer outputBuf = null;

            OutputThread(StringBuffer outputBuf) {
                this.outputBuf = outputBuf;
            }

            public void run() {
                int i = 0;
                try {
                    while ((i = br.read()) != -1) {
                        statusFlag = !statusFlag ? true : statusFlag;
                        if (errorFlag) {
                            Thread.yield();
                            errorFlag = false;
                            try {
                                sleep(100);
                            } catch (InterruptedException ie) {
                            }
                        }
                        if (nextCommand) {
                            br.readLine();
                            nextCommand = false;
                        } else {
                            outputBuf.append((char) i);
                        }
                    }
                    doneflag = true;
                } catch (IOException ote) {
                    System.out.println("Error occurred in output Thread "
                            + ote);
                }
            }
        }
        ;

        OutputThread outputt = new OutputThread(outputStr);
        outputt.start();

        // error thread
        class ErrorThread extends Thread {

            StringBuffer outputBuf = null;

            ErrorThread(StringBuffer outputBuf) {
                this.outputBuf = outputBuf;
            }

            public void run() {
                int i = 0;
                try {
                    while ((i = ber.read()) != -1) {
                        errorFlag = true;
                        outputBuf.append((char) i);
                    }
                } catch (IOException ete) {
                    System.out.println(" Error occurred in error thread "
                            + ete);
                }
            }
        }
        ;

        ErrorThread errort = new ErrorThread(errStr);
        errort.start();

        // input thread
        try {
            p.waitFor();
            outputt.join();
            errort.join();
            if (!cmdStatus) {
                if (errStr.length() > 0) {
                    errStr.delete(0, errStr.length());
                    //     errStr.append("An internal server error has occurred. Please contact support.");
                }
            }
            int count = errStr.indexOf("/sh:");
            if (count > 0)
                errStr.delete(0, count + 5);

            outputStr.append(errStr);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            //.printStackTrace();
        }
        isr = null;
        iser = null;
        osw = null;
        bw = null;
        outputt = null;
        errort = null;
        cmdStatus = false;

        output[0] = outputStr.toString();
        outputStr = null;
        errStr = null;
    }
}

Related

  1. execute(ProcessBuilder builder, String errorMsg)
  2. execute(String cmd, String input)
  3. execute(String command)
  4. execute(String command)
  5. execute(String command)
  6. execute(String url)
  7. execute(String... cmdarray)
  8. execute(String... command)
  9. execute(String... commands)