0N/A/*
2362N/A * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 4763384
0N/A * @summary Ensure that piped input always works with exec'd processes
0N/A */
0N/A
0N/Aimport java.io.*;
0N/A
0N/A
0N/A/**
0N/A * This class demonstrates a regression in java1.4.1 in the handling of the
0N/A * Process OutputStream (exec'd process stdin). The subprocess completes 100%
0N/A * of the time in 1.4, but about only about 50% of the time under 1.4.1. Issue
0N/A * exists for client JVM, Linux Redhat 6.2 not sure about other variants of
0N/A * Linux or other OSes, or server JVM.
0N/A */
0N/A
0N/Apublic class ExecWithInput {
0N/A
0N/A private static final String CAT = "/bin/cat";
0N/A private static final int N = 200;
0N/A
0N/A static int go(int i) throws Exception {
0N/A /*
0N/A * Execute /bin/cat supplying two lines of input. cat should
0N/A * read the input lines and copy them to stdout. On completion,
0N/A * p.waitFor should return and the exit status is printed and this
0N/A * program exits. Under 1.4.1, cat sometimes gets stuck on a pipe
0N/A * read and never terminates.
0N/A */
0N/A //Process p = Runtime.getRuntime().exec(new String[] { CAT } );
0N/A Process p = Runtime.getRuntime().exec(CAT);
0N/A
0N/A String input = i + ": line 1\n" + i + ": line 2\n";
0N/A StringBufferInputStream in = new StringBufferInputStream(input);
0N/A // create threads to handle I/O streams
0N/A IO ioIn = new IO("stdin", in, p.getOutputStream());
0N/A IO ioOut = new IO("stdout", p.getInputStream(), System.out);
0N/A IO ioErr = new IO("stderr", p.getErrorStream(), System.err);
0N/A
0N/A // wait for process to exit
0N/A return p.waitFor();
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A if (!System.getProperty("os.name").equals("Linux"))
0N/A return;
0N/A if (File.separatorChar == '\\') {
0N/A // no /bin/cat on windows
0N/A return;
0N/A }
0N/A for (int i = 0; i < N; i++)
0N/A go(i);
0N/A }
0N/A
0N/A /**
0N/A * Handle IO. Thread is started in constructor.
0N/A */
0N/A static class IO extends Thread {
0N/A
0N/A private InputStream in;
0N/A private OutputStream out;
0N/A
0N/A IO(String name, InputStream in, OutputStream out)
0N/A {
0N/A this.in = in;
0N/A this.out = out;
0N/A setName(name);
0N/A start();
0N/A }
0N/A
0N/A public void run() {
0N/A try {
0N/A int c;
0N/A byte[] buf = new byte[8192];
0N/A int n;
0N/A while ((n = in.read(buf)) != -1) {
0N/A out.write(buf, 0, n);
0N/A out.flush();
0N/A }
0N/A /*
0N/A while ((c = in.read()) != -1) {
0N/A out.write(c);
0N/A if (c == '\n')
0N/A out.flush();
0N/A }
0N/A out.flush();
0N/A */
0N/A } catch (IOException e) {
0N/A e.printStackTrace();
0N/A } finally {
0N/A if (!System.out.equals(out) && !System.err.equals(out)) {
0N/A // Note: in order to get an exec'd java process to
0N/A // see EOF on input, it is necessary to close stdin
0N/A if (out != null) {
0N/A try { out.close(); } catch (Exception e) {
0N/A e.printStackTrace();
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A}