Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Runtime/exec/ExecWithInput.java
47209 views
/*1* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 476338425* @summary Ensure that piped input always works with exec'd processes26*/2728import java.io.*;293031/**32* This class demonstrates a regression in java1.4.1 in the handling of the33* Process OutputStream (exec'd process stdin). The subprocess completes 100%34* of the time in 1.4, but about only about 50% of the time under 1.4.1. Issue35* exists for client JVM, Linux Redhat 6.2 not sure about other variants of36* Linux or other OSes, or server JVM.37*/3839public class ExecWithInput {4041private static final String CAT = "/bin/cat";42private static final int N = 200;4344static int go(int i) throws Exception {45/*46* Execute /bin/cat supplying two lines of input. cat should47* read the input lines and copy them to stdout. On completion,48* p.waitFor should return and the exit status is printed and this49* program exits. Under 1.4.1, cat sometimes gets stuck on a pipe50* read and never terminates.51*/52//Process p = Runtime.getRuntime().exec(new String[] { CAT } );53Process p = Runtime.getRuntime().exec(CAT);5455String input = i + ": line 1\n" + i + ": line 2\n";56StringBufferInputStream in = new StringBufferInputStream(input);57// create threads to handle I/O streams58IO ioIn = new IO("stdin", in, p.getOutputStream());59IO ioOut = new IO("stdout", p.getInputStream(), System.out);60IO ioErr = new IO("stderr", p.getErrorStream(), System.err);6162// wait for process to exit63return p.waitFor();64}6566public static void main(String[] args) throws Exception {67if (!System.getProperty("os.name").equals("Linux"))68return;69if (File.separatorChar == '\\') {70// no /bin/cat on windows71return;72}73for (int i = 0; i < N; i++)74go(i);75}7677/**78* Handle IO. Thread is started in constructor.79*/80static class IO extends Thread {8182private InputStream in;83private OutputStream out;8485IO(String name, InputStream in, OutputStream out)86{87this.in = in;88this.out = out;89setName(name);90start();91}9293public void run() {94try {95int c;96byte[] buf = new byte[8192];97int n;98while ((n = in.read(buf)) != -1) {99out.write(buf, 0, n);100out.flush();101}102/*103while ((c = in.read()) != -1) {104out.write(c);105if (c == '\n')106out.flush();107}108out.flush();109*/110} catch (IOException e) {111e.printStackTrace();112} finally {113if (!System.out.equals(out) && !System.err.equals(out)) {114// Note: in order to get an exec'd java process to115// see EOF on input, it is necessary to close stdin116if (out != null) {117try { out.close(); } catch (Exception e) {118e.printStackTrace();119}120}121}122}123}124}125126}127128129