2
|
1 |
/*
|
|
2 |
* Copyright 2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @bug 6266377
|
|
27 |
* @summary Test to ensure that BufferedWriter releases
|
|
28 |
* resources if flushing the buffer results in an
|
|
29 |
* exception during a call to close().
|
|
30 |
*/
|
|
31 |
|
|
32 |
import java.io.*;
|
|
33 |
|
|
34 |
public class Cleanup extends Thread {
|
|
35 |
|
|
36 |
static PipedWriter w;
|
|
37 |
static PipedReader r;
|
|
38 |
static boolean isWriterClosed = false;
|
|
39 |
|
|
40 |
public void run() {
|
|
41 |
try {
|
|
42 |
System.out.println("Reader reading...");
|
|
43 |
r.read(new char[2048], 0, 2048);
|
|
44 |
|
|
45 |
// Close abruptly before reading all the bytes
|
|
46 |
System.out.println("Reader closing stream...");
|
|
47 |
r.close();
|
|
48 |
|
|
49 |
Thread.sleep(3000);
|
|
50 |
} catch (Throwable e) {
|
|
51 |
System.out.println("Reader exception:");
|
|
52 |
e.printStackTrace();
|
|
53 |
}
|
|
54 |
}
|
|
55 |
|
|
56 |
public static void main(String args[]) throws Exception {
|
|
57 |
r = new PipedReader();
|
|
58 |
w = new PipedWriter(r);
|
|
59 |
|
|
60 |
Cleanup reader = new Cleanup();
|
|
61 |
reader.start();
|
|
62 |
|
|
63 |
BufferedWriter bw = new BufferedWriter(w);
|
|
64 |
|
|
65 |
boolean isWriterClosed = false;
|
|
66 |
|
|
67 |
try {
|
|
68 |
System.out.println("Writer started.");
|
|
69 |
|
|
70 |
for (int i = 0; i < 3; i++) {
|
|
71 |
bw.write(new char[1024], 0, (1024));
|
|
72 |
}
|
|
73 |
|
|
74 |
// This call to close() will raise an exception during
|
|
75 |
// flush() since the reader is already closed
|
|
76 |
bw.close();
|
|
77 |
|
|
78 |
} catch (Throwable e) {
|
|
79 |
try {
|
|
80 |
e.printStackTrace();
|
|
81 |
|
|
82 |
// Make sure that the BufferedWriter releases the resources
|
|
83 |
// A write to a properly closed Writer should raise an exception
|
|
84 |
bw.write('a');
|
|
85 |
|
|
86 |
} catch (IOException ex) {
|
|
87 |
ex.printStackTrace();
|
|
88 |
isWriterClosed = true;
|
|
89 |
}
|
|
90 |
} finally {
|
|
91 |
System.out.println("Writer done.");
|
|
92 |
reader.join();
|
|
93 |
if (!isWriterClosed) {
|
|
94 |
throw new Exception("BufferedWriter is not closed properly");
|
|
95 |
}
|
|
96 |
}
|
|
97 |
}
|
|
98 |
}
|