title: PrintWriter.print blocks when the string is too long
body: <p>I'm writing a wrapper program in java that's just supposed to pass arguments to other processes by writing to their standard in streams, and reading the response from their standard out streams. However, when the <code>String</code> I try to pass in is too large, <code>PrintWriter.print</code> simply blocks. No error, just freezes. Is there a good workaround for this?</p>
  <p>Relevant code</p>
  <pre><code>public class Wrapper {
      PrintWriter writer;
      
      public Wrapper(String command){
          start(command);
      }
  
      public void call(String args){
          writer.println(args); // Blocks here
          writer.flush();
          
          //Other code
      }
  
      public void start(String command) {
          try {
              ProcessBuilder pb = new ProcessBuilder(command.split(&quot; &quot;));
              pb.redirectErrorStream(true);
              process = pb.start();
              // STDIN of the process.
              writer = new PrintWriter(new OutputStreamWriter(process.getOutputStream(), &quot;UTF-8&quot;));
          } catch (Exception e) {
              e.printStackTrace();
              System.out.println(&quot;Process ended catastrophically.&quot;);
          }
      }
      
      
  }
  </code></pre>
  <p>If I try using</p>
  <pre><code>writer.print(args);
  writer.print(&quot;\n&quot;);
  </code></pre>
  <p>it can handle a larger string before freezing, but still ultimately locks up.
  Is there maybe a buffered stream way to fix this? Does <code>print</code> block on the processes stream having enough space or something?</p>
  <h2>Update</h2>
  <p>In response to some answers and comments, I've included more information.</p>
  <ul>
  <li>Operating System is Windows 7</li>
  <li>BufferedWriter slows the run time, but didn't stop it from blocking eventually.</li>
  <li>Strings could get very long, as large as 100,000 characters</li>
  <li>The Process input is consumed, but by line i.e Scanner.nextLine();</li>
  </ul>
  <p>Test code</p>
  <pre><code>import java.io.IOException;
  import java.util.concurrent.ExecutionException;
  import java.util.concurrent.TimeoutException;
  
  import ProcessRunner.Wrapper;
  
  
  public class test {
  
      public static void main(String[] args){
          System.out.println(&quot;Building...&quot;);
          Wrapper w = new Wrapper(&quot;java echo&quot;);
          System.out.println(&quot;Calling...&quot;);
          String market = &quot;aaaaaa&quot;;
          for(int i = 0; i &lt; 1000; i++){
              try {
                  System.out.println(w.call(market, 1000));
              } catch (InterruptedException | ExecutionException
                      | TimeoutException e) {
                  
                  System.out.println(&quot;Timed out&quot;);
              }
              market = market + market;
              System.out.println(&quot;Size = &quot; + market.length());
          }
          System.out.println(&quot;Stopping...&quot;);
          try {
              w.stop();
          } catch (IOException e) {
              
              e.printStackTrace();
              System.out.println(&quot;Stop failed :(&quot;);
          }
      }
  }
  </code></pre>
  <p>Test Process:</p>
  <p>You have to first compile this file, and make sure the .class is in the same folder as the test .class file</p>
  <pre><code>import java.util.Scanner;
  
  
  public class echo {
      
      public static void main(String[] args){
              while(true){
              Scanner stdIn = new Scanner(System.in);
              System.out.println(stdIn.nextLine());
              }
      }
  
  }
  </code></pre>