jdk/test/java/rmi/testlibrary/StreamPipe.java
author smarks
Thu, 20 Dec 2012 20:11:45 -0800
changeset 14919 cd751b98ef69
parent 13256 5886d7607acd
permissions -rw-r--r--
8005290: remove -showversion from RMI test library subprocess mechanism Reviewed-by: jgish, chegar, dmocek
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
13256
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 12692
diff changeset
     2
 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
14919
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    24
import java.io.InputStream;
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    25
import java.io.InterruptedIOException;
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    26
import java.io.IOException;
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    27
import java.io.OutputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * Pipe output of one stream into input of another.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
public class StreamPipe extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private InputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private OutputStream out;
14919
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    36
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    37
    private static Object countLock = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private static int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
14919
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    40
    /**
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    41
     * StreamPipe constructor : should only be called by plugTogether() method.
12692
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    42
     */
14919
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    43
    private StreamPipe(InputStream in, OutputStream out, String name) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        this.in  = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        this.out = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
14919
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    49
    /**
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    50
     * Creates a StreamPipe thread that copies in to out and returns
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    51
     * the created instance.
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    52
     */
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    53
    public static StreamPipe plugTogether(InputStream in, OutputStream out) {
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    54
        String name;
12692
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    55
14919
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    56
        synchronized (countLock) {
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    57
            name = "java.rmi.testlibrary.StreamPipe-" + (count++);
12692
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    58
        }
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    59
14919
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    60
        StreamPipe pipe = new StreamPipe(in, out, name);
12692
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    61
        pipe.setDaemon(true);
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    62
        pipe.start();
14919
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    63
        return pipe;
12692
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    64
    }
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    65
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    66
    // Starts redirection of streams.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        try {
14919
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    69
            byte[] buf = new byte[1024];
12692
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    70
14919
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    71
            while (true) {
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    72
                int nr = in.read(buf);
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    73
                if (nr == -1)
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    74
                    break;
cd751b98ef69 8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents: 13256
diff changeset
    75
                out.write(buf, 0, nr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
13256
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 12692
diff changeset
    77
        } catch (InterruptedIOException iioe) {
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 12692
diff changeset
    78
            // Thread interrupted during IO operation. Terminate StreamPipe.
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 12692
diff changeset
    79
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            System.err.println("*** IOException in StreamPipe.run:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
}