1 /*
2  * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /**
25  * @test
26  * @bug  6219755
27  * @summary Write end loops infinitely when the
28  *          buffer is full and the read end has closed.
29  */
30 
31 import java.io.*;
32 
33 public class WriterLoop extends Thread {
34 
35     static PipedOutputStream out;
36     static PipedInputStream  in;
37 
run()38     public void run() {
39         try {
40             System.out.println("Writer started.");
41 
42             // without the fix, this test will hang at this point,
43             // i.e inside the call to write()
44             out.write(new byte[64*1024]);
45         } catch (Throwable e) {
46 
47             // with the fix an IOException is caught
48             System.out.println("Writer exception:");
49             e.printStackTrace();
50         } finally {
51             System.out.println("Writer done.");
52         }
53     }
54 
main(String args[])55     public static void main(String args[]) throws Exception {
56         in = new PipedInputStream();
57         out = new PipedOutputStream(in);
58         WriterLoop writer = new WriterLoop();
59         writer.start();
60 
61         try {
62             System.out.println("Reader reading...");
63             in.read(new byte[2048]);
64 
65             System.out.println("Reader closing stream...");
66             in.close();
67 
68             System.out.println("Reader sleeping 3 seconds...");
69             Thread.sleep(3000);
70         } catch (Throwable e) {
71             System.out.println("Reader exception:");
72             e.printStackTrace();
73         } finally {
74             System.out.println("Active threads:");
75             Thread[] threads = new Thread[Thread.activeCount()];
76             Thread.enumerate(threads);
77             for (int i = 0; i < threads.length; i++) {
78                 System.out.println("  " + threads[i]);
79             }
80             System.out.println("Waiting for writer...");
81             writer.join();
82             System.out.println("Done.");
83         }
84     }
85 }
86