test/java/lang/Runtime/exec/ExecWithInput.java

Print this page




  22  */
  23 
  24 /* @test
  25  * @bug 4763384
  26  * @summary Ensure that piped input always works with exec'd processes
  27  */
  28 
  29 import java.io.*;
  30 
  31 
  32 /**
  33  * This class demonstrates a regression in java1.4.1 in the handling of the
  34  * Process OutputStream (exec'd process stdin).  The subprocess completes 100%
  35  * of the time in 1.4, but about only about 50% of the time under 1.4.1.  Issue
  36  * exists for client JVM, Linux Redhat 6.2 not sure about other variants of
  37  * Linux or other OSes, or server JVM.
  38  */
  39 
  40 public class ExecWithInput {
  41 
  42     private static final String CAT = "/bin/cat";
  43     private static final int N = 200;
  44 
  45     static int go(int i) throws Exception {
  46         /*
  47          * Execute /bin/cat supplying two lines of input. cat should
  48          * read the input lines and copy them to stdout. On completion,
  49          * p.waitFor should return and the exit status is printed and this
  50          * program exits. Under 1.4.1, cat sometimes gets stuck on a pipe
  51          * read and never terminates.
  52          */
  53         //Process p = Runtime.getRuntime().exec(new String[] { CAT } );
  54         Process p = Runtime.getRuntime().exec(CAT);
  55 
  56         String input = i + ": line 1\n" + i + ": line 2\n";
  57         StringBufferInputStream in = new StringBufferInputStream(input);
  58         // create threads to handle I/O streams
  59         IO ioIn = new IO("stdin", in, p.getOutputStream());
  60         IO ioOut = new IO("stdout", p.getInputStream(), System.out);
  61         IO ioErr = new IO("stderr", p.getErrorStream(), System.err);
  62 
  63         // wait for process to exit
  64         return p.waitFor();
  65     }
  66 
  67     public static void main(String[] args) throws Exception {
  68         if (!System.getProperty("os.name").equals("Linux"))
  69             return;
  70         if (File.separatorChar == '\\') {
  71             // no /bin/cat on windows
  72             return;
  73         }


  74         for (int i = 0; i < N; i++)
  75             go(i);
  76     }
  77 
  78     /**
  79      * Handle IO. Thread is started in constructor.
  80      */
  81     static class IO extends Thread {
  82 
  83         private InputStream in;
  84         private OutputStream out;
  85 
  86         IO(String name, InputStream in, OutputStream out)
  87         {
  88             this.in = in;
  89             this.out = out;
  90             setName(name);
  91             start();
  92         }
  93 
  94         public void run() {
  95             try {
  96                 int c;
  97                 byte[] buf = new byte[8192];
  98                 int n;
  99                 while ((n = in.read(buf)) != -1) {
 100                     out.write(buf, 0, n);
 101                     out.flush();
 102                 }
 103                 /*
 104                 while ((c = in.read()) != -1) {
 105                     out.write(c);
 106                     if (c == '\n')
 107                         out.flush();
 108                 }
 109                 out.flush();
 110                 */
 111             } catch (IOException e) {
 112                 e.printStackTrace();
 113             } finally {
 114                 if (!System.out.equals(out) && !System.err.equals(out)) {
 115                     // Note: in order to get an exec'd java process to
 116                     // see EOF on input, it is necessary to close stdin


  22  */
  23 
  24 /* @test
  25  * @bug 4763384
  26  * @summary Ensure that piped input always works with exec'd processes
  27  */
  28 
  29 import java.io.*;
  30 
  31 
  32 /**
  33  * This class demonstrates a regression in java1.4.1 in the handling of the
  34  * Process OutputStream (exec'd process stdin).  The subprocess completes 100%
  35  * of the time in 1.4, but about only about 50% of the time under 1.4.1.  Issue
  36  * exists for client JVM, Linux Redhat 6.2 not sure about other variants of
  37  * Linux or other OSes, or server JVM.
  38  */
  39 
  40 public class ExecWithInput {
  41 

  42     private static final int N = 200;
  43 
  44     static int go(int i) throws Exception {
  45         /*
  46          * Execute /bin/cat supplying two lines of input. cat should
  47          * read the input lines and copy them to stdout. On completion,
  48          * p.waitFor should return and the exit status is printed and this
  49          * program exits. Under 1.4.1, cat sometimes gets stuck on a pipe
  50          * read and never terminates.
  51          */
  52         Process p = Runtime.getRuntime().exec(UnixCommands.cat());

  53 
  54         String input = i + ": line 1\n" + i + ": line 2\n";
  55         StringBufferInputStream in = new StringBufferInputStream(input);
  56         // create threads to handle I/O streams
  57         IO ioIn = new IO("stdin", in, p.getOutputStream());
  58         IO ioOut = new IO("stdout", p.getInputStream(), System.out);
  59         IO ioErr = new IO("stderr", p.getErrorStream(), System.err);
  60 
  61         // wait for process to exit
  62         return p.waitFor();
  63     }
  64 
  65     public static void main(String[] args) throws Exception {
  66         if (! UnixCommands.isLinux) {
  67             System.out.println("For Linux only");


  68             return;
  69         }
  70         UnixCommands.ensureCommandsAvailable("cat");
  71 
  72         for (int i = 0; i < N; i++)
  73             go(i);
  74     }
  75 
  76     /**
  77      * Handle IO. Thread is started in constructor.
  78      */
  79     static class IO extends Thread {
  80 
  81         private InputStream in;
  82         private OutputStream out;
  83 
  84         IO(String name, InputStream in, OutputStream out)
  85         {
  86             this.in = in;
  87             this.out = out;
  88             setName(name);
  89             start();
  90         }
  91 
  92         public void run() {
  93             try {

  94                 byte[] buf = new byte[8192];
  95                 int n;
  96                 while ((n = in.read(buf)) != -1) {
  97                     out.write(buf, 0, n);
  98                     out.flush();
  99                 }
 100                 /*
 101                 while ((c = in.read()) != -1) {
 102                     out.write(c);
 103                     if (c == '\n')
 104                         out.flush();
 105                 }
 106                 out.flush();
 107                 */
 108             } catch (IOException e) {
 109                 e.printStackTrace();
 110             } finally {
 111                 if (!System.out.equals(out) && !System.err.equals(out)) {
 112                     // Note: in order to get an exec'd java process to
 113                     // see EOF on input, it is necessary to close stdin