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

Print this page

        

*** 37,59 **** * Linux or other OSes, or server JVM. */ public class ExecWithInput { - private static final String CAT = "/bin/cat"; private static final int N = 200; static int go(int i) throws Exception { /* * Execute /bin/cat supplying two lines of input. cat should * read the input lines and copy them to stdout. On completion, * p.waitFor should return and the exit status is printed and this * program exits. Under 1.4.1, cat sometimes gets stuck on a pipe * read and never terminates. */ ! //Process p = Runtime.getRuntime().exec(new String[] { CAT } ); ! Process p = Runtime.getRuntime().exec(CAT); String input = i + ": line 1\n" + i + ": line 2\n"; StringBufferInputStream in = new StringBufferInputStream(input); // create threads to handle I/O streams IO ioIn = new IO("stdin", in, p.getOutputStream()); --- 37,57 ---- * Linux or other OSes, or server JVM. */ public class ExecWithInput { private static final int N = 200; static int go(int i) throws Exception { /* * Execute /bin/cat supplying two lines of input. cat should * read the input lines and copy them to stdout. On completion, * p.waitFor should return and the exit status is printed and this * program exits. Under 1.4.1, cat sometimes gets stuck on a pipe * read and never terminates. */ ! Process p = Runtime.getRuntime().exec(UnixCommands.cat()); String input = i + ": line 1\n" + i + ": line 2\n"; StringBufferInputStream in = new StringBufferInputStream(input); // create threads to handle I/O streams IO ioIn = new IO("stdin", in, p.getOutputStream());
*** 63,76 **** // wait for process to exit return p.waitFor(); } public static void main(String[] args) throws Exception { ! if (!System.getProperty("os.name").equals("Linux")) ! return; ! if (File.separatorChar == '\\') { ! // no /bin/cat on windows return; } for (int i = 0; i < N; i++) go(i); } --- 61,72 ---- // wait for process to exit return p.waitFor(); } public static void main(String[] args) throws Exception { ! if (! System.getProperty("os.name").startsWith("Linux")) { ! System.err.println("Only for Linux"); return; } for (int i = 0; i < N; i++) go(i); }
*** 91,101 **** start(); } public void run() { try { - int c; byte[] buf = new byte[8192]; int n; while ((n = in.read(buf)) != -1) { out.write(buf, 0, n); out.flush(); --- 87,96 ----