Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/io/PushbackReader/ReadCloseRaceNPE.java
41152 views
1
/*
2
* Copyright (c) 2015, 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 8143394
27
* @summary Check for NullPointerException in race between read() and close().
28
*/
29
import java.io.CharArrayReader;
30
import java.io.IOException;
31
import java.io.PushbackReader;
32
import java.io.Reader;
33
import java.io.StringReader;
34
import java.util.ArrayList;
35
import java.util.List;
36
import java.util.concurrent.atomic.AtomicBoolean;
37
import java.util.concurrent.atomic.AtomicReference;
38
import java.util.function.Supplier;
39
40
public class ReadCloseRaceNPE {
41
42
private static final int BUF_SIZE = 1000;
43
private static final long TIMEOUT_MS = 3000;
44
45
private static final List<Exception> failures = new ArrayList<>();
46
47
private static void testReader(final Supplier<Reader> readerSupplier)
48
throws InterruptedException {
49
AtomicReference<Reader> readerRef =
50
new AtomicReference<>(readerSupplier.get());
51
52
AtomicBoolean isFinished = new AtomicBoolean();
53
54
Runnable readTask = () -> {
55
long startTime = System.currentTimeMillis();
56
while (System.currentTimeMillis() - startTime < TIMEOUT_MS) {
57
try {
58
readerRef.get().read();
59
} catch (Exception e) {
60
if (!(e instanceof IOException)) {
61
failures.add(e);
62
break;
63
}
64
readerRef.set(readerSupplier.get());
65
}
66
}
67
isFinished.set(true);
68
};
69
70
Runnable closeTask = () -> {
71
while (!isFinished.get()) {
72
try {
73
readerRef.get().close();
74
} catch (Exception e) {
75
if (!(e instanceof IOException)) {
76
e.printStackTrace();
77
}
78
}
79
}
80
};
81
82
Thread readThread = new Thread(readTask);
83
Thread closeThread = new Thread(closeTask);
84
85
readThread.start();
86
closeThread.start();
87
readThread.join();
88
closeThread.join();
89
}
90
91
public static void main(String[] args) throws Throwable {
92
final String s = "Two riders were approaching.\\n";
93
94
Supplier<Reader> charPushbackReaderSupplier = () -> {
95
char buf[] = new char[s.length()];
96
s.getChars(0, s.length(), buf, 0);
97
CharArrayReader in = new CharArrayReader(buf);
98
return new PushbackReader(in, BUF_SIZE);
99
};
100
101
testReader(charPushbackReaderSupplier);
102
103
Supplier<Reader> stringPushbackReaderSupplier = () -> {
104
StringReader in = new StringReader(s);
105
return new PushbackReader(in, BUF_SIZE);
106
};
107
108
testReader(stringPushbackReaderSupplier);
109
110
if (!failures.isEmpty()) {
111
failures.stream().forEach((x) -> ((Exception) x).printStackTrace());
112
throw new RuntimeException("PushbackReaderNPE failed");
113
}
114
}
115
}
116
117