Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/io/Reader/ReaderBulkReadContract.java
41149 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
import java.io.BufferedReader;
25
import java.io.ByteArrayInputStream;
26
import java.io.CharArrayReader;
27
import java.io.File;
28
import java.io.FileReader;
29
import java.io.FileWriter;
30
import java.io.IOException;
31
import java.io.InputStreamReader;
32
import java.io.LineNumberReader;
33
import java.io.PipedReader;
34
import java.io.PipedWriter;
35
import java.io.PushbackReader;
36
import java.io.Reader;
37
import java.io.StringReader;
38
import java.io.UncheckedIOException;
39
import java.util.Arrays;
40
import java.util.Collections;
41
import java.util.Iterator;
42
import java.util.LinkedList;
43
import java.util.List;
44
import java.util.concurrent.ConcurrentHashMap;
45
import java.util.function.Function;
46
47
import static java.lang.String.format;
48
49
/*
50
* @test
51
* @bug 8029689
52
* @summary checks the bounds part of the contract of java.io.Reader.read(char[], int, int):
53
*
54
* 0 <= off <= off+len <= cbuf.length
55
*
56
* for publicly exported subtypes of java.io.Reader
57
*/
58
public class ReaderBulkReadContract {
59
60
public static void main(String[] args) throws IOException {
61
ReaderBulkReadContract t = new ReaderBulkReadContract();
62
t.test();
63
}
64
65
private void test() throws IOException {
66
Iterator<Object[]> args = args();
67
while (args.hasNext()) {
68
Object[] a = args.next();
69
Reader r = (Reader) a[0];
70
int size = (int) a[1];
71
int off = (int) a[2];
72
int len = (int) a[3];
73
try {
74
read(r, size, off, len);
75
} finally {
76
r.close();
77
}
78
}
79
}
80
81
private Iterator<Object[]> args() {
82
83
Integer[] lens = {Integer.MIN_VALUE, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, Integer.MAX_VALUE};
84
Integer[] offs = {Integer.MIN_VALUE, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, Integer.MAX_VALUE};
85
Integer[] sizes = {0, 1, 2, 3, 4, 5};
86
String[] contents = {"", "a", "ab"};
87
88
List<Function<String, Reader>> fs = Arrays.asList(
89
(String s) -> new BufferedReader(new StringReader(s)),
90
(String s) -> new LineNumberReader(new StringReader(s)),
91
(String s) -> new CharArrayReader(s.toCharArray()),
92
(String s) -> new InputStreamReader(new ByteArrayInputStream(s.getBytes())),
93
(String s) -> newFileReader(s),
94
(String s) -> new PushbackReader(new StringReader(s)),
95
(String s) -> newPipedReader(s),
96
(String s) -> new StringReader(s)
97
);
98
99
// The easiest way to produce a cartesian product from a small fixed number of sets
100
List<Object[]> tuples = Collections.synchronizedList(new LinkedList<>());
101
for (Integer len : lens)
102
for (Integer off : offs)
103
for (String s : contents)
104
for (Integer size : sizes)
105
for (Function<String, Reader> f : fs)
106
tuples.add(new Object[]{f.apply(s), size, off, len});
107
108
return tuples.iterator();
109
}
110
111
private void read(Reader r, int size, int off, int len) throws IOException {
112
IndexOutOfBoundsException ex = null;
113
try {
114
r.read(new char[size], off, len);
115
} catch (IndexOutOfBoundsException e) {
116
ex = e;
117
}
118
119
boolean incorrectBounds = off < 0 || len < 0 || len > size - off;
120
boolean exceptionThrown = ex != null;
121
122
if (incorrectBounds != exceptionThrown) { // incorrectBounds iff exceptionThrown
123
throw new AssertionError(format("r=%s, size=%s, off=%s, len=%s, incorrectBounds=%s, exceptionThrown=%s",
124
r, size, off, len, incorrectBounds, exceptionThrown));
125
}
126
}
127
128
private static PipedReader newPipedReader(String contents) {
129
try (PipedWriter w = new PipedWriter()) {
130
PipedReader r = new PipedReader(w);
131
w.write(contents);
132
return r;
133
} catch (IOException e) {
134
throw new UncheckedIOException(e);
135
}
136
}
137
138
private FileReader newFileReader(String contents) {
139
try {
140
// To not create an enormous amount of files
141
File f = cache.computeIfAbsent(contents,
142
ReaderBulkReadContract::createTempFileWithContents);
143
return new FileReader(f);
144
} catch (IOException e) {
145
throw new UncheckedIOException(e);
146
}
147
}
148
149
private static File createTempFileWithContents(String contents) {
150
try {
151
File testDir = new File(System.getProperty("test.dir", "."));
152
File file = File.createTempFile("ReaderContract", "", testDir);
153
try (FileWriter w = new FileWriter(file)) {
154
w.write(contents);
155
}
156
return file;
157
} catch (IOException e) {
158
throw new UncheckedIOException(e);
159
}
160
}
161
162
//
163
// To avoid myriads of tiny files a cache is used.
164
// ConcurrentHashMap.computeIfAbsent promises a crucial thing:
165
//
166
// ...The entire method invocation is performed atomically, so the
167
// function is applied at most once per key...
168
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
169
//
170
private final ConcurrentHashMap<String, File> cache = new ConcurrentHashMap<>();
171
}
172
173