How can I write large output to Process getOutputStream?
<p>I am trying to execute a command (eg. ps -ef | grep apache) using ProcessBuilder and Process. The code works as long as the output of 'ps -ef' is small. But if the output is too big, the program hangs. Is there a way to fix this? Here is my code based on [<a href="http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html" rel="nofollow">http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html</a>]</p>

<pre><code>#### Program.java ####
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import java.util.Map;

    public class Program {

        private List&lt;String&gt; command;

        public Program(String commandString) throws IOException {
            this(commandString, null);
        }

        public List&lt;String&gt; getCommand() {
            return this.command;
        }

        private void setCommand(String filename, String location, String commandString, List&lt;String&gt; parameters) throws IOException {
            if(filename != null) {
             commandString = new File(location, filename).getCanonicalPath();
            }

            this.command = 
                Collections.synchronizedList(new ArrayList&lt;String&gt;());

            this.command.add(commandString);
            if (parameters != null) {
                for (String arg: parameters) {
                    command.add(arg);
                }
            }
        }

        public String[] run() throws IOException, InterruptedException {
            return this.run(null);
        }

        public String[] run(String input) throws IOException, InterruptedException {
            ProcessBuilder processBuilder = new ProcessBuilder(this.command);

            List&lt;String&gt; commandList = processBuilder.command();

            Process process = processBuilder.start();
            if(input != null) {
                PrintWriter writer = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(process.getOutputStream())), true);
                writer.println(input);
             writer.flush();
             writer.close();
            }
            process.getOutputStream().close();
            Gobbler outGobbler = new Gobbler(process.getInputStream());
            Gobbler errGobbler = new Gobbler(process.getErrorStream());

            Thread outThread = new Thread(outGobbler);
            Thread errThread  = new Thread(errGobbler);

            outThread.start();
            errThread.start();

            outThread.join();
            errThread.join();

            int exitVal = process.waitFor();
            System.out.println("PROCESS WAIT FOR: " + exitVal);

            List&lt;String&gt; output = outGobbler.getOuput();

            return output.toArray(new String[output.size()]);
        }
    }



#### CommandExecutor.java ####

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class CommandExecutor {

    public List&lt;List&lt;Object&gt;&gt; programs;

    public static void main(String[] args) {

     try {
      CommandExecutor ce = new CommandExecutor(args[0]);
      String output = ce.run();
      System.out.println("Command: " + args[0]);
      System.out.println("Output: " + output);
     } catch (IOException e) {
      // TODO Auto-generated catch block
      System.out.println(e.getLocalizedMessage());
      e.printStackTrace();
     } catch (InterruptedException ie) {
      // TODO Auto-generated catch block
      System.out.println(ie.getLocalizedMessage());
      ie.printStackTrace();
     }

    }

    public CommandExecutor(String command) throws IOException {
     this.setPrograms(command);
    }

    private void setPrograms(String command) throws IOException {
     this.programs = new ArrayList&lt;List&lt;Object&gt;&gt;();

     //String cmdstring = "";
     String[] commands = command.split("\\s*;\\s*");
     for(String c: commands) {
      //String subcmdstr = "";
      String file = null;
      String[] chainedCommands = c.split("\\s*\\|\\s*");
      String lastCmd = chainedCommands[chainedCommands.length-1];
      String[] fileCmd = lastCmd.split("\\s*&gt;\\s*");
      if(fileCmd.length &gt; 1) {
       chainedCommands[chainedCommands.length-1] = fileCmd[0];
       file = fileCmd[1];
      }
      List&lt;Object&gt; l = new ArrayList&lt;Object&gt;();
      for(String p: chainedCommands) {
       /*if(subcmdstr.equals("")) {
        subcmdstr = p;
       }
       else {
        subcmdstr += " redirects to " + p;
       }*/
       String[] cmdparams = p.split(" ");
       String cmd = cmdparams[0];
       List&lt;String&gt; params = new ArrayList&lt;String&gt;();
       for(int j = 1; j &lt; cmdparams.length; j++) {
        params.add(cmdparams[j]);
       }
       Program prog = new Program(cmd, params);
       l.add(prog);
      }
      if(file != null) {
       //subcmdstr += " redirects to file: " + file;
       l.add(file);
      }
      this.programs.add(l);
      //cmdstring += "new command: " + subcmdstr + "\n";
     }
     //System.out.println("Actual Command: " + command);
     //System.out.println("Command String:\n" + cmdstring);
    }

    public String run() throws IOException, InterruptedException {
     String output = "";

     for(List&lt;Object&gt; l: this.programs) {
      String[] out = new String[0];
      int count = 0;
      boolean filenotfound = true;
      for(Object o: l) {
       if(o instanceof Program) {
        Program p = (Program) o;
        if(count == 0) {
         out = p.run();
        }
        else {
         out = p.run(CommandExecutor.arrayToString(out));
        }
       }
       else if(o instanceof String) {
        PrintWriter f = new PrintWriter(new File((String)o));
        f.print(CommandExecutor.arrayToString(out));
        f.close();
        filenotfound = false;
       }
       count++;
      }
      if(filenotfound) {
       output += CommandExecutor.arrayToString(out);
      }
     }

     return output;
    }

    public static String arrayToString(String[] strArray) {
     String str = "";
     for(String s: strArray) {
      str += s;
     }
     return str;
    }
}
</code></pre>

<p>Thanks,</p>

<p>Quadir</p>
