Path: blob/master/test/jdk/java/lang/RuntimeTests/exec/ExecWithInput.java
41153 views
/*1* Copyright (c) 2002, 2019, 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 int N = 200;4243static int go(int i) throws Exception {44/*45* Execute /bin/cat supplying two lines of input. cat should46* read the input lines and copy them to stdout. On completion,47* p.waitFor should return and the exit status is printed and this48* program exits. Under 1.4.1, cat sometimes gets stuck on a pipe49* read and never terminates.50*/51Process p = Runtime.getRuntime().exec(UnixCommands.cat());5253String input = i + ": line 1\n" + i + ": line 2\n";54StringBufferInputStream in = new StringBufferInputStream(input);55// create threads to handle I/O streams56IO ioIn = new IO("stdin", in, p.getOutputStream());57IO ioOut = new IO("stdout", p.getInputStream(), System.out);58IO ioErr = new IO("stderr", p.getErrorStream(), System.err);5960// wait for process to exit61return p.waitFor();62}6364public static void main(String[] args) throws Exception {65if (! UnixCommands.isLinux) {66System.out.println("For Linux only");67return;68}69UnixCommands.ensureCommandsAvailable("cat");7071for (int i = 0; i < N; i++)72go(i);73}7475/**76* Handle IO. Thread is started in constructor.77*/78static class IO extends Thread {7980private InputStream in;81private OutputStream out;8283IO(String name, InputStream in, OutputStream out)84{85this.in = in;86this.out = out;87setName(name);88start();89}9091public void run() {92try {93byte[] buf = new byte[8192];94int n;95while ((n = in.read(buf)) != -1) {96out.write(buf, 0, n);97out.flush();98}99/*100while ((c = in.read()) != -1) {101out.write(c);102if (c == '\n')103out.flush();104}105out.flush();106*/107} catch (IOException e) {108e.printStackTrace();109} finally {110if (!System.out.equals(out) && !System.err.equals(out)) {111// Note: in order to get an exec'd java process to112// see EOF on input, it is necessary to close stdin113if (out != null) {114try { out.close(); } catch (Exception e) {115e.printStackTrace();116}117}118}119}120}121}122123}124125126